使用建造者模式来组装家具; 定义一个家具套装类,包含椅子、桌子和沙发的实例; 定义一个抽象的家具套装建造者类,用于组装家具; 为现代风格和古典风格分别实现具体的家具套装建造者类; 编写一个测试程序,创建不同风格的家具套装,并展示其风格。
时间: 2024-05-07 09:17:23 浏览: 88
好的,让我来回答你的问题。使用建造者模式来组装家具主要是为了将家具的组装过程和具体部件的创建过程解耦,从而使得创建和组装过程可以独立进行,有利于代码的重用和扩展。具体步骤如下:
1. 定义一个家具套装类,包含椅子、桌子和沙发的实例。
```java
public class FurnitureSet {
private Chair chair;
private Table table;
private Sofa sofa;
public Chair getChair() {
return chair;
}
public void setChair(Chair chair) {
this.chair = chair;
}
public Table getTable() {
return table;
}
public void setTable(Table table) {
this.table = table;
}
public Sofa getSofa() {
return sofa;
}
public void setSofa(Sofa sofa) {
this.sofa = sofa;
}
}
```
2. 定义一个抽象的家具套装建造者类,用于组装家具。
```java
public abstract class FurnitureSetBuilder {
protected FurnitureSet furnitureSet;
public FurnitureSet getFurnitureSet() {
return furnitureSet;
}
public void createNewFurnitureSet() {
furnitureSet = new FurnitureSet();
}
public abstract void buildChair();
public abstract void buildTable();
public abstract void buildSofa();
}
```
3. 为现代风格和古典风格分别实现具体的家具套装建造者类。
现代风格的家具套装建造者类:
```java
public class ModernFurnitureSetBuilder extends FurnitureSetBuilder {
@Override
public void buildChair() {
Chair chair = new Chair("现代风格的椅子");
furnitureSet.setChair(chair);
}
@Override
public void buildTable() {
Table table = new Table("现代风格的桌子");
furnitureSet.setTable(table);
}
@Override
public void buildSofa() {
Sofa sofa = new Sofa("现代风格的沙发");
furnitureSet.setSofa(sofa);
}
}
```
古典风格的家具套装建造者类:
```java
public class ClassicalFurnitureSetBuilder extends FurnitureSetBuilder {
@Override
public void buildChair() {
Chair chair = new Chair("古典风格的椅子");
furnitureSet.setChair(chair);
}
@Override
public void buildTable() {
Table table = new Table("古典风格的桌子");
furnitureSet.setTable(table);
}
@Override
public void buildSofa() {
Sofa sofa = new Sofa("古典风格的沙发");
furnitureSet.setSofa(sofa);
}
}
```
4. 编写一个测试程序,创建不同风格的家具套装,并展示其风格。
```java
public class Test {
public static void main(String[] args) {
FurnitureSetBuilder modernBuilder = new ModernFurnitureSetBuilder();
Director director = new Director(modernBuilder);
director.constructFurnitureSet();
FurnitureSet modernFurnitureSet = modernBuilder.getFurnitureSet();
FurnitureSetBuilder classicalBuilder = new ClassicalFurnitureSetBuilder();
director = new Director(classicalBuilder);
director.constructFurnitureSet();
FurnitureSet classicalFurnitureSet = classicalBuilder.getFurnitureSet();
System.out.println("现代风格的家具套装包括:");
System.out.println("椅子:" + modernFurnitureSet.getChair().getName());
System.out.println("桌子:" + modernFurnitureSet.getTable().getName());
System.out.println("沙发:" + modernFurnitureSet.getSofa().getName());
System.out.println("古典风格的家具套装包括:");
System.out.println("椅子:" + classicalFurnitureSet.getChair().getName());
System.out.println("桌子:" + classicalFurnitureSet.getTable().getName());
System.out.println("沙发:" + classicalFurnitureSet.getSofa().getName());
}
}
```
输出结果为:
```
现代风格的家具套装包括:
椅子:现代风格的椅子
桌子:现代风格的桌子
沙发:现代风格的沙发
古典风格的家具套装包括:
椅子:古典风格的椅子
桌子:古典风格的桌子
沙发:古典风格的沙发
```
以上就是使用建造者模式来组装家具的示例代码,希望对你有所帮助。
阅读全文