设计模式:生产者模式
应用场景
- 相同的方法,不同的执行顺序,产生不同的事件结果时;
- 多个部件或零件,都可以装配到一个对象中,但是产生的运行结果又不相同时
- 产品类非常复杂,或者产品类中的调用顺序不同产生了不同的效能,这个时候使用建造者模式非常合适;
- 当初始化一个对象特别复杂,如参数多,且很多参数都具有默认值时。
意图
- 将复杂的对象的构建过程与它的表示分离,这样就可以灵活控制对象的构建过程,使得同样的构建过程,因为具体的生成器不同,创建不同的表示。
参与者
生产者:Builder
职责
- 本身是一个抽象类
- 它对产品的各个组成部分,指定抽象的行为接口
- 返回一个空的产品对象,具体什么产品对象在ConcreteBuilder实现
具体生产者:ConcreteBuilder
职责
- 本身是一个实现Builder的实体类
- 提供获取一个具体的产品对象的方法
引导者:Director
职责
- 本身是一个类,类中具有Builder的引用
- 它提供一个方法,参数Builder,通过操作Builder接口,用Builder按照其指定的顺序去完成Produt的构造。
产品:Product
职责
- 本身是包含定义组成,部件的类,也就是最终组装的产品。
结构
协作
- 客户创建一个引导器Diretior,引用Builder接口进行配置
- 生成器处理引导器的请求,按照规定的次序,将部件组装到产品中。
- 客户引用生成器对象,获取组装配置后的产品
交互图
实例
场景:一个公司开发产品,参与者:客户,产品经理,技术主管,程序员。
需求:客户给产品经理需求,产品经理分析需求,告诉技术主管,或者程序员开发产品给客户。
(1)产品类
/*** 产品类* Created by shixinshan on 2018/10/25.*/public class Product {private String appName;private String appFuction;private String appSystem;public String getAppName() {return appName;}public void setAppName(String appName) {this.appName = appName;}public String getAppFuction() {return appFuction;}public void setAppFuction(String appFuction) {this.appFuction = appFuction;}public String getAppSystem() {return appSystem;}public void setAppSystem(String appSystem) {this.appSystem = appSystem;}@Overridepublic String toString() {return "Product [appName=" + appName + ", appFuction=" + appFuction+ ", appSystem=" + appSystem + "]";}
}
(2)抽象建造者类——技术主管(可省略)
/*** 抽象建造者类——技术主管* Created by shixinshan on 2018/10/25.*/public abstract class Builder {public abstract Builder setAppName(String appName);public abstract Builder setAppFuction(String appFuction);public abstract Builder setAppSystem(String appSystem);public Product build(){return null;}
}
(3)具体建造者类——程序员
/*** 具体建造者类——程序员* Created by shixinshan on 2018/10/25.*/public class WorkBuilder extends Builder {private Product product;// 产品类private InnerProduct innerProduct = new InnerProduct();// 产品缓冲类@Overridepublic Builder setAppName (String appName){innerProduct.setAppName(appName);return this;}@Overridepublic Builder setAppFuction (String appFuction){innerProduct.setAppFuction(appFuction);return this;}@Overridepublic Builder setAppSystem (String appSystem){innerProduct.setAppSystem(appSystem);return this;}@Overridepublic Product build () {product = new Product();product.setAppName(innerProduct.getAppName());product.setAppFuction(innerProduct.getAppFuction());product.setAppSystem(innerProduct.getAppSystem());return product;}/*** 产品缓冲类*/private class InnerProduct {private String appName;private String appFuction;private String appSystem;public String getAppName() {return appName;}public void setAppName(String appName) {this.appName = appName;}public String getAppFuction() {return appFuction;}public void setAppFuction(String appFuction) {this.appFuction = appFuction;}public String getAppSystem() {return appSystem;}public void setAppSystem(String appSystem) {this.appSystem = appSystem;}}}
(4)指挥者类——产品经理(可省略)
/*** 指挥者类——产品经理* Created by shixinshan on 2018/10/25.*/public class Director {public Product create(Builder builder,String system,String appName,String appFunction) {Product product = builder.setAppSystem(system).setAppName(appName).setAppFuction(appFunction).build();return product;}}
(4)客户
/*** 客户* Created by shixinshan on 2018/10/25.*/public class Client {public Product getProduct(String system,String appName,String appFuntion){Director director = new Director();//产品经理WorkBuilder workBuilder = new WorkBuilder();director.create(workBuilder,system,appName,appFuntion);Product build = workBuilder.build();return build;}
}
测试
@Testpublic void builder() throws Exception {Client client = new Client();Product product = client.getProduct("android", "QQ", "chat");System.out.println("product = "+product.toString());}
结果:
product = Product [appName=QQ, appFuction=chat, appSystem=android]
Process finished with exit code 0
Builder模式变种(常用)
public class Person {private String name;private int age;private double height;private double weight;private Person(Builder builder){this.name=builder.name;this.age=builder.age;this.height=builder.height;this.weight=builder.weight;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public double getHeight() {return height;}public void setHeight(double height) {this.height = height;}public double getWeight() {return weight;}public void setWeight(double weight) {this.weight = weight;}static class Builder{private String name;private int age;private double height;private double weight;public Builder setName(String name) {this.name=name;return this;}public Builder setAge(int age) {this.age=age;return this;}public Builder setHeight(double height) {this.height=height;return this;}public Builder setWeight(double weight) {this.weight=weight;return this;}public Person build(){return new Person(this);// build()返回Person对象}}}
参考资料
- 创建型设计模式之Builder模式
- 设计模式—建造者模式(Builder Pattern)
- 设计模式总结篇系列:建造者模式(Builder)
- 23种设计模式
- 细数JDK里的设计模式
设计模式:生产者模式相关推荐
- Python设计模式-建造者模式
Python设计模式-建造者模式 代码基于3.5.2,代码如下; #coding:utf-8 #建造者模式 class Burger():name = ""price = 0.0d ...
- Python设计模式-状态模式
Python设计模式-状态模式 代码基于3.5.2,代码如下; #coding:utf-8 #状态模式class state():def writeProgram(self,work):raise N ...
- Python设计模式-备忘录模式
Python设计模式-备忘录模式 代码基于3.5.2,代码如下; #coding:utf-8 #备忘录模式 import randomclass gameCharacter():vitality = ...
- Python设计模式-解释器模式
Python设计模式-解释器模式 代码基于3.5.2,代码如下; #coding:utf-8 #解释器模式class PlayContext():play_text = Noneclass Expre ...
- Python设计模式-命令模式
Python设计模式-命令模式 代码基于3.5.2,代码如下; #coding:utf-8 #命令模式class barbecuer():def bakeButton(self):print(&quo ...
- Python设计模式-策略模式
Python设计模式-策略模式 代码基于3.5.2,代码如下; #coding:utf-8 #策略模式class sendInterface():def send(self,value):raise ...
- Python设计模式-外观模式
Python设计模式-外观模式 代码基于3.5.2,代码如下; #coding:utf-8 # 外观模式class AlarmSensor:def run(self):print("Alar ...
- Python设计模式-桥接模式
Python设计模式-桥接模式 基于Python3.5.2,代码如下 #coding:utf-8class Shape():name = ""param = "" ...
- Python设计模式-代理模式
Python设计模式-代理模式 基于Python3.5.2,代码如下 #coding:utf-8info_struct = dict() info_struct["addr"] = ...
最新文章
- inotify之文件系统事件监控使用入门
- 为什么你的数据分析成果总是难以落地?
- Html前端基础(这些基础标签你必须知道!)
- 文件管理搜不到Android 里的文件,Android:在原始文件夹中添加文件后窗口找不到内容容器视图...
- 2008,人力资源软件是否还依然
- 洛谷P2147[SDOI2008]洞穴勘测
- 模仿c#Func和Action的函数指针模板
- usb转ttl测试软件,usb转ttl驱动
- 多边形裁剪(Polygon Clipping) 2
- 关于屏保设置不生效时要了解的几个小技巧!
- oracle 01405 提取的值为null,ORA-01405 : fetched column value is NULL
- android 自定义圆形头像,android自定义圆形头像
- 《重构 改善既有代码的设计 1》重构原则
- NFT 的 10 种实际用途
- 数据爬取遇到EventStream是个什么东西?EventSource与websocket有何区别?Java后台如何获取爬取数据并入库?EventStream后台服务怎么写?
- andy the android ppt,剑桥国际少儿英语KB1Unit课件.ppt
- 关于su和sudo以及vi sudo 的权限讨论
- HTTP 304状态码讲解
- mysql deadlock6_MYSQL:1213 Deadlock问题排查历程
- 产品网站建设-企业产品网站建设基本流程及工作内容
热门文章
- 就个人对待行业的看法—之—扯淡
- Emoji表情符号兼容方案(转)
- Mac下暗黑服务器搭建(完全攻略,一个菜鸟的搭建之路)
- java url参数加密_针对url参数的加密解密算法(原创)
- 安卓开发学习11-1:Android程序调试:DDMS工具使用
- js点一个不行时,点两个:
- 成像中的倏逝波与衍射极限
- 云堡垒机的作用_堡垒机作用
- 百度地图发布《2019年春运出行预测报告》,返乡、出行、出游“早知道”...
- ScaleAnimation类:尺寸变化动画类