本文主要是介绍设计模式---Builder模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言:
网络上有人将它定义为:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的对象。
所以建造者模式适合于一个具有较多的零件(属性)的产品(对象)的创建过程。
构成
由五个部分构成:
1. Product:ConcreateBuilder创建该产品的内部表示并定义它的装配过程。
2. Builder接口:抽象接口,定义创建Product对象内部各个组件的操作
3. ConcreteBuilder:实现Builder的接口,以构造和装配该产品的各个部件。定义并明确它所创建的表示,同时提供一个返回Product的接口
4. Director:构造一个使用Builder接口的对象
实现
举个具体的例子吧:
现在我们需要生产一台电脑,有CPU,硬盘,内存,显卡,声卡(只是用来举例),我们可以这样写:
一:经典Builder模式
1.Product:
public class Computer {private CPU mCPU; //CPUprivate HardDisk mHardDisk; //硬盘private Memory mMemory; //内存private Graphics mGraphics; //显卡private SoundCard mSoundCard; //声卡public HardDisk getmHardDisk() {return mHardDisk;}public void setmHardDisk(HardDisk mHardDisk) {this.mHardDisk = mHardDisk;}public CPU getmCPU() {return mCPU;}public void setmCPU(CPU mCPU) {this.mCPU = mCPU;}public Memory getmMemory() {return mMemory;}public void setmMemory(Memory mMemory) {this.mMemory = mMemory;}public Graphics getmGraphics() {return mGraphics;}public void setmGraphics(Graphics mGraphics) {this.mGraphics = mGraphics;}public SoundCard getmSoundCard() {return mSoundCard;}public void setmSoundCard(SoundCard mSoundCard) {this.mSoundCard = mSoundCard;}
}
2.Builder接口:
public interface Builder {public void createCPU();public void createHardDisk();public void createMemory();public void createGraphics();public void createSoundCard();public Computer getComputer();}
3.ConcreteBuilder:
public class ComputerBuilder implements Builder {private Computer mComputer = new Computer();@Overridepublic void createCPU() {mComputer.setmCPU(new CPU());}@Overridepublic void createHardDisk() {mComputer.setmHardDisk(new HardDisk());}@Overridepublic void createMemory() {mComputer.setmMemory(new Memory()); }@Overridepublic void createGraphics() {mComputer.setmGraphics(new Graphics());}@Overridepublic void createSoundCard() {mComputer.setmSoundCard(new SoundCard());}@Overridepublic Computer getComputer() {return mComputer;}
}
4.Director:
public class Director {private Builder mBuilder;public Director (Builder builder){this.mBuilder = builder;}public void CreateComputer(){this.mBuilder.createCPU();this.mBuilder.createHardDisk();this.mBuilder.createMemory();this.mBuilder.createGraphics();this.mBuilder.createSoundCard();}public Computer getComputer() {return this.mBuilder.getComputer();}
}
这种方式有一个缺点就是可能引入多个重载构造函数以及setters过度使用导致不必要的复杂性
二,通过静态内部类等方式实现的零件无序化构造
public class Computer {private final CPU mCPU;private final HardDisk mHardDisk;private final Memory mMemory;private final Graphics mGraphics;private final SoundCard mSoundCard;public Computer(ComputerBuilder builder) {mCPU = builder.mCPU;mHardDisk = builder.mHardDisk;mMemory = builder.mMemory;mGraphics = builder.mGraphics;mSoundCard = builder.mSoundCard;}public HardDisk getmHardDisk() {return mHardDisk;}public CPU getmCPU() {return mCPU;}public Memory getmMemory() {return mMemory;}public Graphics getmGraphics() {return mGraphics;}public SoundCard getmSoundCard() {return mSoundCard;}public static class ComputerBuilder {private final CPU mCPU;private final HardDisk mHardDisk;private final Memory mMemory;private Graphics mGraphics;private SoundCard mSoundCard;public ComputerBuilder(CPU cpu, HardDisk harddisk, Memory memory) {this.mCPU = cpu;this.mHardDisk = harddisk;this.mMemory = memory;}public ComputerBuilder graphics(Graphics graphics) {this.mGraphics = graphics;return this;}public ComputerBuilder soundCard(SoundCard soundCard) {this.mSoundCard = soundCard;return this;}public Computer build() {return new Computer(this);}}}
使用方法:
public Computer CreateComputer() {return new Computer.ComputerBuilder(CPU, HardDisk, Memory).graphics(Graphics).soundCard(SoundCard).build();}
用这种方式方式看起来会非常直观,另外对话框AlertDialog也是用的这种形式
这篇关于设计模式---Builder模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!