博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Builder(建造者)模式
阅读量:6710 次
发布时间:2019-06-25

本文共 6358 字,大约阅读时间需要 21 分钟。

建造者模式可以将一个产品的内部表象产品的生成过程分割开来,从而可以使一个建造过程生成具有不同的内部表象的产品对象。

Director:担任这个角色的类调用具体建造者角色以创建产品对象。

Builder:给出一个抽象接口,以规范产品对象的各个组成成分的建造。如汽车由车轮、方向盘、车厢等等组成。

     public void BuildPart()方法给与其子类去实现,就是由子类去创建车轮、方向盘、车厢等等。

ConcreteBuilder:用于实例化操作的子类,继承自Builder。

    public void BuildPart()实例化各个部件

    public Product GetResult()返回结果

Product:对象。车轮、方向盘、车厢

using System; using System.Collections;  // "Director" class Shop  {   // Methods   public void Construct( VehicleBuilder vehicleBuilder )    {     vehicleBuilder.BuildFrame();     vehicleBuilder.BuildEngine();     vehicleBuilder.BuildWheels();     vehicleBuilder.BuildDoors();   } }  // "Builder" abstract class VehicleBuilder  {   // Fields   protected Vehicle vehicle;    // Properties   public Vehicle Vehicle    {      get{ return vehicle; }   }    // Methods   abstract public void BuildFrame();   abstract public void BuildEngine();   abstract public void BuildWheels();   abstract public void BuildDoors(); }  // "ConcreteBuilder1" class MotorCycleBuilder : VehicleBuilder  {   // Methods   override public void BuildFrame()    {     vehicle = new Vehicle( "MotorCycle" );     vehicle[ "frame" ] = "MotorCycle Frame";   }    override public void BuildEngine()    {     vehicle[ "engine" ] = "500 cc";   }    override public void BuildWheels()    {     vehicle[ "wheels" ] = "2";   }    override public void BuildDoors()    {     vehicle[ "doors" ] = "0";   } }  // "ConcreteBuilder2" class CarBuilder : VehicleBuilder  {   // Methods   override public void BuildFrame()    {     vehicle = new Vehicle( "Car" );     vehicle[ "frame" ] = "Car Frame";   }    override public void BuildEngine()    {     vehicle[ "engine" ] = "2500 cc";   }    override public void BuildWheels()    {     vehicle[ "wheels" ] = "4";   }    override public void BuildDoors()    {     vehicle[ "doors" ] = "4";   } }  // "ConcreteBuilder3" class ScooterBuilder : VehicleBuilder  {   // Methods   override public void BuildFrame()    {     vehicle = new Vehicle( "Scooter" );     vehicle[ "frame" ] = "Scooter Frame";   }    override public void BuildEngine()    {     vehicle[ "engine" ] = "none";   }    override public void BuildWheels()    {     vehicle[ "wheels" ] = "2";   }    override public void BuildDoors()    {     vehicle[ "doors" ] = "0";   } }  // "Product" class Vehicle  {   // Fields   private string type;   private Hashtable parts = new Hashtable();    // Constructors   public Vehicle( string type )    {     this.type = type;   }    // Indexers   public object this[ string key ]    {      get{ return parts[ key ]; }      set{ parts[ key ] = value; }   }    // Methods   public void Show()    {     Console.WriteLine( " ---------------------------");     Console.WriteLine( "Vehicle Type: "+ type );     Console.WriteLine( " Frame : " + parts[ "frame" ] );     Console.WriteLine( " Engine : "+ parts[ "engine"] );     Console.WriteLine( " #Wheels: "+ parts[ "wheels"] );     Console.WriteLine( " #Doors : "+ parts[ "doors" ] );   } }   ///  /// BuilderApp test ///  public class BuilderApp  {   public static void Main( string[] args )    {     // Create shop and vehicle builders     Shop shop = new Shop();     VehicleBuilder b1 = new ScooterBuilder();     VehicleBuilder b2 = new CarBuilder();     VehicleBuilder b3 = new MotorCycleBuilder();      // Construct and display vehicles     shop.Construct( b1 );     b1.Vehicle.Show();      shop.Construct( b2 );     b2.Vehicle.Show();      shop.Construct( b3 );     b3.Vehicle.Show();   } }

 

using System;

using System.Collections;
// "Director"
class Shop
{
  
// Methods
  public void Construct( VehicleBuilder vehicleBuilder )
  
{
    vehicleBuilder.BuildFrame();
    vehicleBuilder.BuildEngine();
    vehicleBuilder.BuildWheels();
    vehicleBuilder.BuildDoors();
  }
}
// "Builder"
abstract class VehicleBuilder
{
  
// Fields
  protected Vehicle vehicle;
  
// Properties
  public Vehicle Vehicle
  
{
    
getreturn vehicle; }
  }
  
// Methods
  abstract public void BuildFrame();
  
abstract public void BuildEngine();
  
abstract public void BuildWheels();
  
abstract public void BuildDoors();
}
// "ConcreteBuilder1"
class MotorCycleBuilder : VehicleBuilder
{
  
// Methods
  override public void BuildFrame()
  
{
    vehicle = 
new Vehicle( "MotorCycle" );
    vehicle[ "frame" ] = "MotorCycle Frame";
  }
  
override public void BuildEngine()
  
{
    vehicle[ "engine" ] = "500 cc";
  }
  
override public void BuildWheels()
  
{
    vehicle[ "wheels" ] = "2";
  }
  
override public void BuildDoors()
  
{
    vehicle[ "doors" ] = "0";
  }
}
// "ConcreteBuilder2"
class CarBuilder : VehicleBuilder
{
  
// Methods
  override public void BuildFrame()
  
{
    vehicle = 
new Vehicle( "Car" );
    vehicle[ "frame" ] = "Car Frame";
  }
  
override public void BuildEngine()
  
{
    vehicle[ "engine" ] = "2500 cc";
  }
  
override public void BuildWheels()
  
{
    vehicle[ "wheels" ] = "4";
  }
  
override public void BuildDoors()
  
{
    vehicle[ "doors" ] = "4";
  }
}
// "ConcreteBuilder3"
class ScooterBuilder : VehicleBuilder
{
  
// Methods
  override public void BuildFrame()
  
{
    vehicle = 
new Vehicle( "Scooter" );
    vehicle[ "frame" ] = "Scooter Frame";
  }
  
override public void BuildEngine()
  
{
    vehicle[ "engine" ] = "none";
  }
  
override public void BuildWheels()
  
{
    vehicle[ "wheels" ] = "2";
  }
  
override public void BuildDoors()
  
{
    vehicle[ "doors" ] = "0";
  }
}
// "Product"
class Vehicle
{
  
// Fields
  private string type;
  
private Hashtable parts = new Hashtable();
  
// Constructors
  public Vehicle( string type )
  
{
    
this.type = type;
  }
  
// Indexers
  public object thisstring key ]
  
{
    
getreturn parts[ key ]; }
    
set{ parts[ key ] = value; }
  }
  
// Methods
  public void Show()
  
{
    Console.WriteLine( " ---------------------------");
    Console.WriteLine( "Vehicle Type: "+ type );
    Console.WriteLine( " Frame : " + parts[ "frame" ] );
    Console.WriteLine( " Engine : "+ parts[ "engine"] );
    Console.WriteLine( " #Wheels: "+ parts[ "wheels"] );
    Console.WriteLine( " #Doors : "+ parts[ "doors" ] );
  }
}
/**//// <summary>
/// BuilderApp test
/// </summary>
public class BuilderApp
{
  
public static void Main( string[] args )
  
{
    
// Create shop and vehicle builders
    Shop shop = new Shop();
    VehicleBuilder b1 = 
new ScooterBuilder();
    VehicleBuilder b2 = 
new CarBuilder();
    VehicleBuilder b3 = 
new MotorCycleBuilder();
    
// Construct and display vehicles
    shop.Construct( b1 );
    b1.Vehicle.Show();
    shop.Construct( b2 );
    b2.Vehicle.Show();
    shop.Construct( b3 );
    b3.Vehicle.Show();
  }
}

转载于:https://www.cnblogs.com/Tim_Liu/archive/2010/09/27/1836973.html

你可能感兴趣的文章
嵌入式系统 Boot Loader 技术内幕【转】
查看>>
(windows)一台电脑上安装两个Mysql服务
查看>>
教你如何在Kali Linux 环境下设置蜜罐?
查看>>
微信公众号开发之公众号支付
查看>>
主域控角色迁移和夺取(转载)
查看>>
HDFS High Availability Using the Quorum Journal Manager
查看>>
Sql日期时间格式转换
查看>>
mesos+marathon+zookeeper的docker管理集群亲手搭建实例(环境Centos6.8)
查看>>
你应了解的4种JS设计模式
查看>>
垃圾收集器Serial 、Parallel、CMS、G1
查看>>
mongodb基本概念解析
查看>>
前端学HTTP之网关、隧道和中继
查看>>
OpenCV【2】---读取png图片显示到QT label上的问题
查看>>
Fedora 25-64位操作系统中安装配置Hyperledger Fabric过程
查看>>
Azure China (12) 域名备案问题
查看>>
ActiveX组件与JavaScript交互
查看>>
2013第52周六当前用到的一些软件及网站
查看>>
DrawDibDraw函数的使用方法
查看>>
两种将字符串转换成浮点数的方法
查看>>
Xcode 调试技巧-b
查看>>