实体类

  1. package factorymode.simplefactory.builder;
  2. public class MobilePhone {
  3. //手机屏幕
  4. private String screen;
  5. //电池
  6. private String battery;
  7. //话筒
  8. private String microphone;
  9. //听筒
  10. private String phoneReceiver;
  11. //机身
  12. private String phoneBody;
  13. public String getScreen() {
  14. return screen;
  15. }
  16. public void setScreen(String screen) {
  17. this.screen = screen;
  18. }
  19. public String getBattery() {
  20. return battery;
  21. }
  22. public void setBattery(String battery) {
  23. this.battery = battery;
  24. }
  25. public String getMicrophone() {
  26. return microphone;
  27. }
  28. public void setMicrophone(String microphone) {
  29. this.microphone = microphone;
  30. }
  31. public String getPhoneReceiver() {
  32. return phoneReceiver;
  33. }
  34. public void setPhoneReceiver(String phoneReceiver) {
  35. this.phoneReceiver = phoneReceiver;
  36. }
  37. public String getPhoneBody() {
  38. return phoneBody;
  39. }
  40. public void setPhoneBody(String phoneBody) {
  41. this.phoneBody = phoneBody;
  42. }
  43. @Override
  44. public String toString() {
  45. return "MobilePhone{" +
  46. "screen='" + screen + '\'' +
  47. ", battery='" + battery + '\'' +
  48. ", microphone='" + microphone + '\'' +
  49. ", phoneReceiver='" + phoneReceiver + '\'' +
  50. ", phoneBody='" + phoneBody + '\'' +
  51. '}';
  52. }
  53. }

建造者接口

  1. package factorymode.simplefactory.builder;
  2. public interface IBuildPhone {
  3. /**
  4. * 建造手机屏幕
  5. */
  6. void buildScreen();
  7. /**
  8. * 建造手机电池
  9. */
  10. void buildBattery();
  11. /**
  12. * 建造手机听筒
  13. */
  14. void buildMicrophone();
  15. /**
  16. * 建造手机话筒
  17. */
  18. void buildPhoneReceiver();
  19. /**
  20. * 建造手机机身
  21. */
  22. void buildPhoneBody();
  23. public MobilePhone build();
  24. }

IPhoneX 建造者

  1. package factorymode.simplefactory.builder;
  2. public class IPhoneX implements IBuildPhone {
  3. private MobilePhone mobilePhone;
  4. public IPhoneX() {
  5. mobilePhone = new MobilePhone();
  6. }
  7. @Override
  8. public void buildScreen() {
  9. mobilePhone.setScreen("OLED");
  10. }
  11. @Override
  12. public void buildBattery() {
  13. mobilePhone.setBattery("2700");
  14. }
  15. @Override
  16. public void buildMicrophone() {
  17. mobilePhone.setMicrophone("listen");
  18. }
  19. @Override
  20. public void buildPhoneReceiver() {
  21. mobilePhone.setPhoneReceiver("receiver");
  22. }
  23. @Override
  24. public void buildPhoneBody() {
  25. mobilePhone.setPhoneBody("body");
  26. }
  27. @Override
  28. public MobilePhone build() {
  29. return mobilePhone;
  30. }
  31. }

指导者

  1. package factorymode.simplefactory.builder;
  2. public class Director {
  3. public MobilePhone createMobilePhone(IBuildPhone iBuildPhone) {
  4. iBuildPhone.buildBattery();
  5. iBuildPhone.buildMicrophone();
  6. iBuildPhone.buildPhoneBody();
  7. iBuildPhone.buildPhoneReceiver();
  8. iBuildPhone.buildScreen();
  9. return iBuildPhone.build();
  10. }
  11. }

测试类

  1. package factorymode.simplefactory.builder;
  2. public class Builder1 {
  3. public static void main(String[] args) {
  4. Director director = new Director();
  5. MobilePhone phone = director.createMobilePhone(new IPhoneX());
  6. System.out.println(phone);
  7. }
  8. }