zoukankan      html  css  js  c++  java
  • (三)生成器模式-代码实现

    介绍

    概念:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

    例如:衣服制造厂(实体)有制造衣服的功能,每种衣服的制造流程都不相同,因此把制造衣服这个复杂且可变的功能分离出来.

    使用场景:实体的某个功能复杂且不只有一种

    好处:功能的扩展不影响实体,实体更加内聚,与可变的部分耦合度低

    代码:

    //生产过程接口

    package note.com.builder;
    
    public interface Builder {
        public String build(Factory factory);
        
    }

    //生产1过程

    package note.com.builder;
    
    public class Builder1 implements Builder{
    
        public String build(Factory factory) {
            String clothes = factory.getOne1()+factory.getTwo2()+factory.getThree1()+factory.getFour1();
            return clothes;
        }
    
    }

    //生产2过程

    package note.com.builder;
    
    public class Builder2 implements Builder{
    
        public String build(Factory factory) {
            String clothes = factory.getOne2()+factory.getTwo1()+factory.getFour1();
            return clothes;
        }
    
    }

    //衣服工厂实体

    package note.com.builder;
    
    public class Factory {
        private String one1 = "纺织布";
        private String one2 = "尼龙布";
        private String two1 = "裁剪";
        private String two2 = "上色";
        private String three1 = "加领";
        private String four1 = "缝合";
        
        public String build(Builder builder){
            String clothes = builder.build(this);
            System.out.println("生产过程:"+clothes);
            return clothes;
        }
        
        
        public String getOne1() {
            return one1;
        }
        public void setOne1(String one1) {
            this.one1 = one1;
        }
        public String getOne2() {
            return one2;
        }
        public void setOne2(String one2) {
            this.one2 = one2;
        }
        public String getTwo1() {
            return two1;
        }
        public void setTwo1(String two1) {
            this.two1 = two1;
        }
        public String getTwo2() {
            return two2;
        }
        public void setTwo2(String two2) {
            this.two2 = two2;
        }
        public String getThree1() {
            return three1;
        }
        public void setThree1(String three1) {
            this.three1 = three1;
        }
        public String getFour1() {
            return four1;
        }
        public void setFour1(String four1) {
            this.four1 = four1;
        }
        
        
    }

    //测试类

    package note.com.builder;
    
    public class BuilderTest {
        public static void main(String[] args) {
            Factory factory = new Factory();
            Builder builder1 = new Builder1();
            Builder builder2 = new Builder2();
            factory.build(builder1);
            factory.build(builder2);
        }
    }

    //结果

    生产过程:纺织布上色加领缝合
    生产过程:尼龙布裁剪缝合

     

    由此,工厂的生产过程与工厂进行分离,并且每次增加新衣服的生产都只需要增加Builder的实现就可以了.

  • 相关阅读:
    运算符、流程控制
    python数据类型
    Python入门基础
    pandas教程1:pandas数据结构入门
    使用matplotlib绘制多轴图
    使用matplotlib快速绘图
    浙江省新高中信息技术教材,将围绕Python进行并增加编程相关知识点
    [转]ubuntu搭建LAMP环境
    [转]字体精简
    [转]安装PIL时注册表中找不到python2.7
  • 原文地址:https://www.cnblogs.com/qinggege/p/5230718.html
Copyright © 2011-2022 走看看