1.传统方式完成盖房项目
1.需求说明
2.类图
3.目录结构
4.代码实现
1.AbstractHouse.java
package com.sun.tradition;
/**
* Description: 抽象的建造房屋的类
* @Author sun
* @Create 2024/5/28 21:12
* @Version 1.0
*/
public abstract class AbstractHouse {
// 打地基
public abstract void buildBasic();
// 砌墙
public abstract void buildwalls();
// 封顶
public abstract void roofed();
// 造房子
public void build() {
buildBasic();
buildwalls();
roofed();
}
}
2.CommonHouse.java
package com.sun.tradition;
/**
* Description: 普通房子
* @Author sun
* @Create 2024/5/28 21:16
* @Version 1.0
*/
public class CommonHouse extends AbstractHouse{
@Override
public void buildBasic() {
System.out.println("普通房子打地基");
}
@Override
public void buildwalls() {
System.out.println("普通房子砌墙");
}
@Override
public void roofed() {
System.out.println("普通房子封顶");
}
}
3.Client.java
package com.sun.tradition;
/**
* Description:
* @Author sun
* @Create 2024/5/28 21:17
* @Version 1.0
*/
public class Client {
public static void main(String[] args) {
CommonHouse commonHouse = new CommonHouse();
commonHouse.build();
}
}
4.结果
5.优缺点分析
2.建造者模式
1.基本介绍
2.建造者模式的四个角色
3.类图
4.代码实现
1.House.java
package com.sun.impove;
/**
* Description: 产品
* @Author sun
* @Create 2024/5/29 19:47
* @Version 1.0
*/
public class House {
private String basic;
private String wall;
private String roofed;
public String getBaise() {
return basic;
}
public void setBaise(String basic) {
this.basic = basic;
}
public String getWall() {
return wall;
}
public void setWall(String wall) {
this.wall = wall;
}
public String getRoofed() {
return roofed;
}
public void setRoofed(String roofed) {
this.roofed = roofed;
}
}
2.HouseBuilder.java
package com.sun.impove;
/**
* Description: 抽象建造者
* @Author sun
* @Create 2024/5/29 19:48
* @Version 1.0
*/
public abstract class HouseBuilder {
// 组合一个产品
protected House house = new House();
// 对产品进行一定的操作
// 打地基
public abstract void buildBasic();
// 砌墙
public abstract void buildwalls();
// 封顶
public abstract void roofed();
// 将建造之后的产品返回
public House buildHouse() {
return house;
}
}
3.HighHouse.java
package com.sun.impove;
/**
* Description: 高房子
* @Author sun
* @Create 2024/5/29 20:01
* @Version 1.0
*/
public class HighHouse extends HouseBuilder{
@Override
public void buildBasic() {
System.out.println("高房子打地基");
}
@Override
public void buildwalls() {
System.out.println("高房子砌墙");
}
@Override
public void roofed() {
System.out.println("高房子封顶");
}
}
4.CommonHouse.java
package com.sun.impove;
/**
* Description: 普通房子
* @Author sun
* @Create 2024/5/29 19:58
* @Version 1.0
*/
public class CommonHouse extends HouseBuilder{
@Override
public void buildBasic() {
System.out.println("普通房子打地基");
}
@Override
public void buildwalls() {
System.out.println("普通房子砌墙");
}
@Override
public void roofed() {
System.out.println("普通房子封顶");
}
}
5.HouseDirector.java
package com.sun.impove;
/**
* Description: 指挥者
* @Author sun
* @Create 2024/5/29 20:04
* @Version 1.0
*/
public class HouseDirector {
// 聚合一个抽象建造者
private HouseBuilder houseBuilder;
// 通过构造器聚合
public HouseDirector(HouseBuilder houseBuilder) {
this.houseBuilder = houseBuilder;
}
// 或者通过setter方法聚合
public void setHouseBuilder(HouseBuilder houseBuilder) {
this.houseBuilder = houseBuilder;
}
// 通过抽象建造者指挥具体的建造流程
public House constructHouse() {
houseBuilder.buildwalls();
houseBuilder.buildBasic();
houseBuilder.roofed();
// 建造完成后调用抽象建造者的构建房子方法,返回一个房子
return houseBuilder.buildHouse();
}
}
6.Client.java
package com.sun.impove;
/**
* Description:
* @Author sun
* @Create 2024/5/29 20:11
* @Version 1.0
*/
public class Client {
public static void main(String[] args) {
// 让指挥者盖一个高房子
HouseDirector houseDirector = new HouseDirector(new HighHouse());
House house = houseDirector.constructHouse();
}
}