1. 简单工厂
特点:根据条件来创建不同的具体产品
2. 工厂方法
特点:有抽象的产品和抽象的工厂,但是这里的工厂只负责生产一样产品,如需多个产品,则需要多个工厂来生产
3. 抽象工厂
特点:一个工厂负责生产一系列不同的产品
4. Builder
特点:使用同样的建造流程来创建不同的产品
示例代码
- class Director {
- public Device Construct(DeviceBuilder builder) {
- string xml = builder.GetDescriptorFile();
- builder.LoadDescriptorFile();
- return builder.Simulate();
- }
- }
- /// <summary>
- /// This is the product (would be built by builder)
- /// </summary>
- class Device {
- }
- /// <summary>
- /// abstract builder: 与标准的builder不太一致的地方是,这里的buildpart都用了abstract,实际代码可以不用abstract
- /// </summary>
- class DeviceBuilder {
- public abstract string GetDescriptorFile();
- public abstract void LoadDescriptorFile();
- public abstract Device Simulate();
- }
- /// <summary>
- /// Concrete builder
- /// </summary>
- class CameraBuilder : DeviceBuilder {
- public override string GetDescriptorFile() {
- throw new NotImplementedException();
- }
- public override void LoadDescriptorFile() {
- throw new NotImplementedException();
- }
- public override Device Simulate() {
- throw new NotImplementedException();
- }
- }