• 创造者设计模式


     

     Bike

    1. package com.jmj.pattern.builder.demo01;
    2. public class Bike {
    3. private String frame;//车架
    4. private String seat;//车座
    5. public String getFrame() {
    6. return frame;
    7. }
    8. public void setFrame(String frame) {
    9. this.frame = frame;
    10. }
    11. public String getSeat() {
    12. return seat;
    13. }
    14. public void setSeat(String seat) {
    15. this.seat = seat;
    16. }
    17. @Override
    18. public String toString() {
    19. return "Bike{" +
    20. "frame='" + frame + '\'' +
    21. ", seat='" + seat + '\'' +
    22. '}';
    23. }
    24. }

    Builder

    1. package com.jmj.pattern.builder.demo01;
    2. public abstract class Builder {
    3. //声明bike类型的变量,并进行复制
    4. protected Bike bike=new Bike();
    5. public abstract void buildFrame();
    6. public abstract void buildSeat();
    7. public abstract Bike createBike();
    8. }

    MobileBuilder

    1. package com.jmj.pattern.builder.demo01;
    2. //具体的构建者,用来构建摩拜单车对象
    3. public class MobileBuilder extends Builder{
    4. @Override
    5. public void buildFrame() {
    6. bike.setFrame("碳纤维车架");
    7. }
    8. @Override
    9. public void buildSeat() {
    10. bike.setSeat("真皮车座");
    11. }
    12. @Override
    13. public Bike createBike() {
    14. return bike;
    15. }
    16. }

    OfoBuilder

    1. package com.jmj.pattern.builder.demo01;
    2. public class OfoBuilder extends Builder{
    3. @Override
    4. public void buildFrame() {
    5. bike.setFrame("铝合金车架");
    6. }
    7. @Override
    8. public void buildSeat() {
    9. bike.setSeat("橡胶车座");
    10. }
    11. @Override
    12. public Bike createBike() {
    13. return bike;
    14. }
    15. }

    Director

    1. package com.jmj.pattern.builder.demo01;
    2. public class Director {
    3. //声明builder类型的变量
    4. private Builder builder;
    5. public Director(Builder builder) {
    6. this.builder = builder;
    7. }
    8. //组装自行车
    9. public Bike construct(){
    10. builder.buildFrame();
    11. builder.buildSeat();
    12. return builder.createBike();
    13. }
    14. }

    Client

    1. package com.jmj.pattern.builder.demo01;
    2. public class Client {
    3. public static void main(String[] args) {
    4. //创建指挥者对象
    5. Director director = new Director(new OfoBuilder());
    6. //让指挥者指挥组装自行车
    7. Bike construct = director.construct();
    8. System.out.println(construct);
    9. }
    10. }

    另一种建造者模式

    1. package com.jmj.pattern.builder.demo02;
    2. public class Phone {
    3. private String cpu;
    4. private String screen;
    5. private String memory;
    6. private String mainboard;
    7. private Phone(Builder builder){
    8. this.cpu = builder.cpu;
    9. this.screen = builder.screen;
    10. this.memory = builder.memory;
    11. this.mainboard = builder.mainboard;
    12. };
    13. @Override
    14. public String toString() {
    15. return "Phone{" +
    16. "cpu='" + cpu + '\'' +
    17. ", screen='" + screen + '\'' +
    18. ", memory='" + memory + '\'' +
    19. ", mainboard='" + mainboard + '\'' +
    20. '}';
    21. }
    22. public static final class Builder{
    23. private String cpu;
    24. private String screen;
    25. private String memory;
    26. private String mainboard;
    27. public Builder cpu(String cpu){
    28. this.cpu=cpu;
    29. return this;
    30. }
    31. public Builder screen(String screen){
    32. this.screen=screen;
    33. return this;
    34. }
    35. public Builder memory(String memory){
    36. this.memory=memory;
    37. return this;
    38. }
    39. public Builder mainboard(String mainboard){
    40. this.mainboard=mainboard;
    41. return this;
    42. }
    43. //使用构建者创建Phone对象
    44. public Phone build(){
    45. return new Phone(this);
    46. }
    47. }
    48. }
    1. package com.jmj.pattern.builder.demo02;
    2. public class Client {
    3. public static void main(String[] args) {
    4. //创建手机对象
    5. Phone.Builder builder = new Phone.Builder();
    6. Phone build = builder.cpu("英特尔").mainboard("华硕").memory("金士顿").screen("hkc").build();
    7. System.out.println(build);
    8. }
    9. }

  • 相关阅读:
    相机通用类之LMI激光三角相机(3D),软触发硬触发(飞拍),并输出halcon格式对象
    RustDay06------Exercise[81-90]
    VS Code使用node.js编译,代码自动补全方法
    利用python进行视频下载并界面播放快速下载素材
    Hadoop学习记录5--YARN学习1
    信息安全基础
    Jmeter接口测试教程
    TCP/IP 测试题(四)
    一款超实用的AI漫画生成器,支持9种漫画风格,无限免费使用
    瑞利衰落条件下扩频通信系统误码率仿真
  • 原文地址:https://blog.csdn.net/qq_53374893/article/details/134400354