侧边栏壁纸
博主头像
春潮带雨晚来急博主等级

行动起来,活在当下

  • 累计撰写 32 篇文章
  • 累计创建 1 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

建造者模式

Administrator
2023-12-26 / 0 评论 / 0 点赞 / 36 阅读 / 6815 字

> Build

---

定义:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

特征:用户只需指定需要建造的类型就可以得到他们,建造过程及细节不需要知道。

类型:创建型[如果一步一步构建包含多个组件的过程,相同的构建对象可以构建出不同的产品]

适用于:流程固定,而顺序不一定固定的业务。[如果一个对象有非常复杂的内部结构(很多属性)、想把复杂对象的创建和使用分离]

优点:封装性好,创建和使用分离。扩展性好,建造类之间独立,一定程度上解耦

缺点:产生多余的Builder对象。产品内部发生变化,建造者都要修改,成本较大

- Demo1

```

public class CourseSecondVersion {

private String courseQA;

private String coursePPT;

private String courseName;

private String courseVideo;

private String courseArticle;

@Override

public String toString() {

return "CourseSecondVersion{" +

"courseQA='" + courseQA + '\'' +

", coursePPT='" + coursePPT + '\'' +

", courseName='" + courseName + '\'' +

", courseVideo='" + courseVideo + '\'' +

", courseArticle='" + courseArticle + '\'' +

'}';

}

public CourseSecondVersion(CourseBuild courseBuild) {

this.courseQA = courseBuild.courseQA;

this.coursePPT = courseBuild.coursePPT;

this.courseName = courseBuild.courseName;

this.courseVideo = courseBuild.courseVideo;

this.courseArticle = courseBuild.courseArticle;

}

public static class CourseBuild {

private String courseQA;

private String coursePPT;

private String courseName;

private String courseVideo;

private String courseArticle;

public CourseBuild buildCourseName(String courseName) {

this.courseName = courseName + "----------this is name-------" + courseName;

return this;

}

public CourseBuild buildCourseQA(String courseQA) {

this.courseQA = courseQA;

return this;

}

public CourseBuild buildCoursePPT(String coursePPT) {

this.coursePPT = coursePPT;

return this;

}

public CourseBuild buildCourseVideo(String courseVideo) {

this.courseVideo = courseVideo;

return this;

}

public CourseBuild buildCourseArticle(String courseArticle) {

this.courseArticle = courseArticle;

return this;

}

public CourseSecondVersion build() {

return new CourseSecondVersion(this);

}

}

}

```

- Demo2

```

// 封装组件

public class KillPeopleBuilder {

private String makeGun;

private String buyGunpowder;

private String makeBullet;

private String AssemblySomeGuns;

private String Aim;

private String killHead;

@Override

public String toString() {

return "KillPeopleBuilder{" +

"makeGun='" + makeGun + '\'' +

", buyGunpowder='" + buyGunpowder + '\'' +

", makeBullet='" + makeBullet + '\'' +

", AssemblySomeGuns='" + AssemblySomeGuns + '\'' +

", Aim='" + Aim + '\'' +

", killHead='" + killHead + '\'' +

'}';

}

public KillPeopleBuilder(GoBuild build) {

this.Aim = build.aim;

this.makeGun = build.makeGun;

this.killHead = build.killHead;

this.makeBullet = build.makeBullet;

this.buyGunpowder = build.buyGunPowder;

this.AssemblySomeGuns = build.assemblySomeGuns;

}

public static class GoBuild {

private String makeGun;

private String buyGunPowder;

private String makeBullet;

private String assemblySomeGuns;

private String aim;

private String killHead;

public GoBuild buildMakeGun(String makeGun) {

this.makeGun = makeGun;

return this;

}

public GoBuild buildBuyGunPower(String buyGunPowder) {

this.buyGunPowder = buyGunPowder;

return this;

}

public GoBuild buildMakeBullet(String makeBullet) {

this.makeBullet = makeBullet;

return this;

}

public GoBuild buildAssemblySomeGuns(String assemblySomeGuns) {

this.assemblySomeGuns = assemblySomeGuns;

return this;

}

public GoBuild buildAim(String aim) {

this.aim = aim;

return this;

}

public GoBuild buildKillHead(String killHead) {

this.killHead = killHead;

return this;

}

public KillPeopleBuilder build() {

return new KillPeopleBuilder(this);

}

}

}

```

```

// 调用组件

public class IWannaKillPeoples {

public static void main(String[] args) {

KillPeopleBuilder build = new KillPeopleBuilder.GoBuild().buildMakeGun("开始造枪的步骤").buildMakeBullet("买好火药和子弹").buildAim("瞄准敌人").buildKillHead("爆头").build();

System.out.println(build.toString());

}

}

```

0

评论区