CSDN博客

img 2002pine

Design Pattern 2-Factory

发表于2004/9/25 17:07:00  634人阅读

using System;

namespace Pattern
{
 /// <summary>
 /// Summary description for Class1.
 /// </summary>
 public class Factory
 {
  public Factory()
  {
   
  }
  public static Fruit made(int flag)
  {
   if (flag==1) return new apple();
   if (flag==2) return new Orange();
   else return null;
  }

 }

 public interface Fruit
 {
  string grow();
  string water();

 }

 public sealed class apple:Fruit
 {
  public apple(){}
  public string grow()
  {
   return "this is apple grow";
  }
  public string water()
  {
   return "this is apple water";
  }
  
 }

 public sealed class Orange:Fruit
 {
  public Orange(){}
  public string grow()
  {
   return "this is Orange grow";
  }
  public string water()
  {
   return "this is Orange water";
  } 

 }
}

 //简单工厂模式
      Console.WriteLine(Factory.made(1).grow());
     Console.WriteLine(Factory.made(2).grow());

阅读全文
0 0

相关文章推荐

img
取 消
img