美文网首页设计模式
建造者模式-链式赋值

建造者模式-链式赋值

作者: 念䋛 | 来源:发表于2020-08-29 18:41 被阅读0次

    当参数多的时候可以使用建造者模式做链式赋值

    public class House {
        private String basics;
        private String wall;
        private String roof;
        private House(HouseBulider houseBulider){
            basics = houseBulider.basics;
            wall = houseBulider.wall;
            roof = houseBulider.roof;
        }
       private static class HouseBulider{
           private String basics;
           private String wall;
           private String roof;
    
           public HouseBulider setBasics(String basics) {
               this.basics = basics;
               return this;
           }
    
           public HouseBulider setWall(String wall) {
               this.wall = wall;
               return this;
           }
    
           public HouseBulider setRoof(String roof) {
               this.roof = roof;
               return this;
           }
           public House bulider(){
               return new House (this);
           }
       }
    
        @Override
        public String toString() {
            return "House{" +
                    "basics='" + basics + '\'' +
                    ", wall='" + wall + '\'' +
                    ", roof='" + roof + '\'' +
                    '}';
        }
    
        public static void main(String[] args) {
            House bulider = new HouseBulider ().setBasics ("地基10米").setWall ("砌墙0.5米").setRoof ("屋顶5米").bulider ();
            System.out.println (bulider.toString ());
        }
    }
    

    相关文章

      网友评论

        本文标题:建造者模式-链式赋值

        本文链接:https://www.haomeiwen.com/subject/uvpvghtx.html