工厂方法

以计算器为例:

public class Operation

{

  public double numbera;

  public double numberb;

  public virtual double getresult()

  {

    return 0;

  }

}

//加法

public class OperationAdd : Operation

{

  public overidde double getresult()

  {

    return numbera + numberb;

  }

}



//创建操作工厂内

public interface IFactory

{

  Operation CreateOperation();

}



//加法工厂

public class FactoryAdd : IFactory

{

  public Operation CreateOperation()

  {

    return new OperationAdd();

  }

}

//使用

public class Program
{
    public static void Main()
    {
       IFactory fac= new FactoryAdd();
       Operation op = fac.CreateOperation();
       op.numbera = 1;
       op.numberb = 2;
      Console.WriteLine(op.getresult());
     }          
}

 

 

 

 

 

posted on 2013-02-03 16:08  fishyk  阅读(94)  评论(0)    收藏  举报

导航