设计模式的理解:构造器模式(builder pattern) -ag凯发k8国际
构造器模式,将一个复杂对象的构建与其表示分离,使得同样的构建过程可以创建不同的表示。
通俗的话来讲,就是把成员变量的初始化与构造函数进行分离。
考虑到当一个类的构造函数参数个数超过4个,而且这些参数有些是可选的参数,有些参数需要she'zhi考虑使用构造者模式。
解决的问题
当一个类的构造函数参数超过4个,而且这些参数有些是可选的时,我们通常有两种办法来构建它的对象。 例如我们现在有如下一个类计算机类computer,其中cpu与ram是必填参数,而其他3个是可选参数,那么我们如何构造这个类的实例呢,通常有两种常用的方式:
第一:折叠构造函数模式(telescoping constructor pattern )
class computer {string cpu;//必须string ram;//必须int usbcount;//可选string keyboard;//可选string display;//可选public:computer(string cpu, string ram) {this(cpu, ram, 0);}computer(string cpu, string ram, int usbcount) {this(cpu, ram, usbcount, "罗技键盘");}computer(string cpu, string ram, int usbcount, string keyboard) {this(cpu, ram, usbcount, keyboard, "三星显示器");}computer(string cpu, string ram, int usbcount, string keyboard, string display) {this->cpu = cpu;this->ram = ram;this->usbcount = usbcount;this->keyboard = keyboard;this->display = display;} }虽然,在c 语言中,可以在形参中赋默认值,但是在其他语言中,上面的表达方式弊端就十分的明显,阅读起来不方便,操作起来也不方便,当构造函数参数超过10个以上时,你在穿参初始化时很容易把值传错给其他的参数。所以builder模式就产生了
一、优化的builder模式:
两个主要的实现思路,将必须初始化的数据设置成构造函数,且构造函数不能太长,将可选的成员变量另外去赋值。
#include "iostream" using namespace std; class computer { private:computer() {}string cpu;//必须string ram;//必须int usbcount;//可选string keyboard;//可选string display;//可选 public: class builder{string cpu;//必须string ram;//必须int usbcount;//可选string keyboard;//可选string display;//可选public:builder(string cup,string ram){this->cpu=cup;this->ram=ram;}const string &getcpu() const {return cpu;}const string &getram() const {return ram;}int getusbcount() const {return usbcount;}const string &getkeyboard() const {return keyboard;}const string &getdisplay() const {return display;}builder& setusbcount(int usbcount) {this->usbcount = usbcount;return *this;}builder& setkeyboard(string keyboard) {this->keyboard = keyboard;return *this;}builder& setdisplay(string display) {this->display = display;return *this;}computer * build(){return new computer(*this);}};//省略getter方法computer(builder builder){this->cpu=builder.getcpu();this->ram=builder.getram();this->usbcount=builder.getusbcount();this->keyboard=builder.getkeyboard();this->display=builder.getdisplay();}}; int main(){computer * computerptr = computer::builder("因特尔","三星").setdisplay("三星24寸").setkeyboard("罗技").setusbcount(2).build();return 0; }
优化的builder省去了director角色,而把builder嵌套在实体类中,充当一个内部类。
二、传统builder 模式
最原先的builder 模式有一个director类,
1.在builder抽象类中,将实体类product的可选成员参数的赋值设置为抽象函数,必选成员变量设置为product实体类和 builder派生类的构造函数。
2.procuct 为 builder派生类 的子对象
3.director 负责初始化 builder。
1) 定义实体类
public class computer {private string cpu;//必须private string ram;//必须private int usbcount;//可选private string keyboard;//可选private string display;//可选public computer(string cpu, string ram) {this.cpu = cpu;this.ram = ram;}public void setusbcount(int usbcount) {this.usbcount = usbcount;}public void setkeyboard(string keyboard) {this.keyboard = keyboard;}public void setdisplay(string display) {this.display = display;}@overridepublic string tostring() {return "computer{" "cpu='" cpu '\'' ", ram='" ram '\'' ", usbcount=" usbcount ", keyboard='" keyboard '\'' ", display='" display '\'' '}';} }2)抽象builder类 和 派生类
public abstract class computerbuilder {public abstract void setusbcount();public abstract void setkeyboard();public abstract void setdisplay();public abstract computer getcomputer(); }//苹果电脑构建者 public class maccomputerbuilder extends computerbuilder {private computer computer;public maccomputerbuilder(string cpu, string ram) {computer = new computer(cpu, ram);}@overridepublic void setusbcount() {computer.setusbcount(2);}@overridepublic void setkeyboard() {computer.setkeyboard("苹果键盘");}@overridepublic void setdisplay() {computer.setdisplay("苹果显示器");}@overridepublic computer getcomputer() {return computer;} }
//联想电脑构建者类 public class lenovocomputerbuilder extends computerbuilder {private computer computer;public lenovocomputerbuilder(string cpu, string ram) {computer=new computer(cpu,ram);}@overridepublic void setusbcount() {computer.setusbcount(4);}@overridepublic void setkeyboard() {computer.setkeyboard("联想键盘");}@overridepublic void setdisplay() {computer.setdisplay("联想显示器");}@overridepublic computer getcomputer() {return computer;} }
3) director 类
public class computerdirector {public void makecomputer(computerbuilder builder){builder.setusbcount();builder.setdisplay();builder.setkeyboard();} }
使用案例:
public static void main(string[] args) {computerdirector director=new computerdirector();//1computerbuilder builder=new maccomputerbuilder("i5处理器","三星125");//2director.makecomputer(builder);//3computer maccomputer=builder.getcomputer();//4system.out.println("mac computer:" maccomputer.tostring());computerbuilder lenovobuilder=new lenovocomputerbuilder("i7处理器","海力士222");director.makecomputer(lenovobuilder);computer lenovocomputer=lenovobuilder.getcomputer();system.out.println("lenovo computer:" lenovocomputer.tostring()); }
与50位技术专家面对面20年技术见证,附赠技术全景图
总结
以上是ag凯发k8国际为你收集整理的设计模式的理解:构造器模式(builder pattern)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 设计模式理解:工厂模式,抽象工厂,原型方
- 下一篇: 设计模式的理解:单例模式(singlet