我试图了解建筑模式,但主任遇到了麻烦

时间:2019-10-14 10:51:36

标签: java design-patterns builder

为了更好地理解,我尝试编写一个构建器模式。通常,我依靠GOF和Wikipedia。 因此,我的对象是一栋具有必需属性区域和一些可选属性(如窗户,门,房间等)的房屋。 我将向您显示代码。现在,我不确定它是否正确,我想我没有导演吗?我不知道在什么情况下您需要一种及其工作方式。

这是我的班级房屋和内部类房屋建筑工人

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);

     }
}

现在,我刚刚获得了一个课堂演示,可以在其中建造一所房子:

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

但这不是书中的导演。我的想法正确吗?为什么这比仅使用二传手更好?

谢谢!

1 个答案:

答案 0 :(得分:0)

您的示例说明了经典的生成器。 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。