程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#設計模式之建造者設計模式(Builder)(3)

C#設計模式之建造者設計模式(Builder)(3)

編輯:關於C語言

五、建造者模式的實現:

下面的程序代碼演示了Shop對象使用VehicleBuilders來建造不同的交通工具。該例子使用了Builder模式順序建造交通工具的不同部分。

// Builder pattern -- Real World example 
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" ] );
 }
}
/**//// <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();
 }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved