gpt4 book ai didi

java - 我正在尝试理解建筑模式,但我与导演之间存在问题

转载 作者:太空宇宙 更新时间:2023-11-04 09:20:31 25 4
gpt4 key购买 nike

我尝试编写构建器模式以更好地理解。我主要依赖 GOF 和维基百科。所以我的对象是一个具有必需属性区域和一些可选属性(如 window 、门、房间等)的房子我将向您展示代码。现在,我不太确定它是否正确,我想我没有导演?我不明白在哪些情况下您需要一个以及它是如何工作的。

这是我的类(class)房屋和内部类(class) HouseBuilder

public class House {

//required
private final String area;

//optional
private int windows;
private int doors;
private int rooms;



//constructor with HouseBuilder
private House(HouseBuilder builder) {
this.windows = builder.windows;
this.doors = builder.doors;
this.rooms = builder.rooms;


}



public static class HouseBuilder {

//required
private String area;

//optional
private int windows;
private int doors;
private int rooms;

//constructor with required attributes
HouseBuilder(String area) {
this.area = area;
}
//optional attributes
public HouseBuilder windows(int windows) {
this.windows = windows;
return this;
}

public HouseBuilder doors (int doors) {
this.doors = doors;
return this;
}
//function for building
public Housebuild() {
return new House(this);

}
}

现在,我刚刚得到了一个类演示,我可以在其中 build 这样的房子:

House house = new House.HouseBuilder("Downtown")
.doors(3).windows(2).build();

但这并不是书中那样的导演。我的想法是否正确?为什么这比仅仅使用 setter 更好?

谢谢!

最佳答案

您的示例说明了经典构建器。 Director 类似于一个抽象构建器,在实践中很少使用它,因为客户端类可以很好地处理它。您的案例中的董事示例如下:

public class House
{
public final String area;

public windows;
public int doors;
public int rooms;
}

interface HouseBuilder
{
public HouseBuilder area();

public HouseBuilder windows();

public HouseBuilder doorsors();

public HouseBuilder rooms();

public House build();
}

public static class DowntownHouseBuilder implements HouseBuilder
{
House downtownHouse = new House();

public HouseBuilder area()
{
downtownHouse.area = "Downtown";
}

public HouseBuilder windows()
{
downtownHouse.windows = 3;
return this;
}

public HouseBuilder doors()
{
downtownHouse.doors = 2;
return this;
}

public HouseBuilder rooms()
{
downtownHouse.rooms = 2;
return this;
}

public House build()
{
return downtownHouse;
}
}

public static class VilaBuilder implements HouseBuilder
{
House vila new House();

public HouseBuilder area()
{
vila.area = "Downtown";
}

public HouseBuilder windows()
{
vila.windows = 24;
return this;
}

public HouseBuilder doors()
{
vila.doors = 5;
return this;
}

public HouseBuilder rooms()
{
downtownHouse.rooms = 10;
return this;
}

public House build()
{
return vila;
}
}

class Driector
{
private HouseBuilder houseBuilder;

public Driector(HouseBuilder houseBuilder)
{
this.houseBuilder = houseBuilder;
}

public House buildHouse()
{
return this.houseBuilder.area()
.windows()
.doors()
.rooms()
.buid();
}
}

class HouseConstruction
{
public static void main(String[] args)
{
Director director = new Director(new VilaBuilder());

House house = director.buildHouse();

System.out.println("Builder constructed: "+ house);
}
}

希望这有助于澄清什么是 Builder 模式中的 Director。

关于java - 我正在尝试理解建筑模式,但我与导演之间存在问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58375238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com