CSDN博客

img 2002pine

Design Pattern 1-builder

发表于2004/9/25 17:05:00  752人阅读

using System;

namespace Pattern
{
 /// <summary>
 /// Summary description for Class1.
 /// </summary>
 public interface builder
 {
  void buildPartA();
  void buildPartB();
  product getProduct();
 }
 public class product
 {
  public string name;
  public string date;
  public product()
  {
  } 
 }
 public class director
 {
  private builder myb;
  public director(builder b)
  {
   this.myb =b;
  }
  public product construct()
  {

   myb.buildPartA();
   myb.buildPartB();
   return myb.getProduct();

  }

 }
 public class ConcreateBuilder1:builder
 {
  private product p=new product();
  public ConcreateBuilder1()
  {
  }
  public void buildPartA()
  {
   p.name ="A";
   System.Console.WriteLine("build1 part name a");
  }
  public void buildPartB()
  {
   p.date ="b";
   System.Console.WriteLine("build1 part date b");
  }
  public product getProduct()
  {
   System.Console.WriteLine("build1 product");
   return p;
  }
 }

 public class ConcreateBuilder2:builder
 {
  private product p=new product();
  public ConcreateBuilder2()
  {
  }
  public void buildPartA()
  {
   p.name ="A";
   System.Console.WriteLine("build2 part name a");
  }
  public void buildPartB()
  {
   p.date ="b";
   System.Console.WriteLine("build2 part date b");
  }
  public product getProduct()
  {
   System.Console.WriteLine("build 2 product");
   return p;
  }
 }
}

//构建模式builder
   
            director d=new director(new ConcreateBuilder1());
   d.construct();

   d=new director(new ConcreateBuilder2());
   d.construct();

阅读全文
0 0

相关文章推荐

img
取 消
img