定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类。
1、简单工厂模式
public class Product
{
//定义产品属性
public double Attribute1
{
get;
set;
}
public double Attribute2
{
get;
set;
}
//定义虚方法GetResult
public virtual double GetResult()
{
double result = 0;
return result;
}
}
{
//定义产品属性
public double Attribute1
{
get;
set;
}
public double Attribute2
{
get;
set;
}
//定义虚方法GetResult
public virtual double GetResult()
{
double result = 0;
return result;
}
}
//具体产品的实现方法
public class ConcreteProduct1:Product
{
//重写Product的GetResult方法
public override double GetResult()
{
double result;
result = Attribute1 - Attribute2*Attribute2;
return result;
}
}
public class ConcreteProduct2 : Product
{
//重写Product的GetResult方法
public override double GetResult()
{
double result;
result = Attribute1 * Attribute2+Attribute1;
return result;
}
}
public class ConcreteProduct1:Product
{
//重写Product的GetResult方法
public override double GetResult()
{
double result;
result = Attribute1 - Attribute2*Attribute2;
return result;
}
}
public class ConcreteProduct2 : Product
{
//重写Product的GetResult方法
public override double GetResult()
{
double result;
result = Attribute1 * Attribute2+Attribute1;
return result;
}
}
public class Factory
{
//Factory用来确定到底实例化哪个运算方法
public static Product CreateProduct(string strCondition)
{
Product producer = null;
switch (strCondition)
{
case "1":
producer = new ConcreteProduct1();
break;
case "2":
producer = new ConcreteProduct2();
break;
}
return producer;
}
}
{
//Factory用来确定到底实例化哪个运算方法
public static Product CreateProduct(string strCondition)
{
Product producer = null;
switch (strCondition)
{
case "1":
producer = new ConcreteProduct1();
break;
case "2":
producer = new ConcreteProduct2();
break;
}
return producer;
}
}
static void Main(string[] args)
{
Product producer;
producer = Factory.CreateProduct("1");
producer.Attribute1 = 2;
producer.Attribute2 = 1.5;
double result = producer.GetResult();
Console.WriteLine(result.ToString());
Console.Read();
}
{
Product producer;
producer = Factory.CreateProduct("1");
producer.Attribute1 = 2;
producer.Attribute2 = 1.5;
double result = producer.GetResult();
Console.WriteLine(result.ToString());
Console.Read();
}
2、基本工厂方法模式
简单工厂模式用一个工厂类直接确定了实例化的方法,使得客户端仅仅需要负责“消费”对象就可以了。而不必管这些对象究竟如何创建及如何组织的.明确了各自的职责和权利。但当系统中的具体产品类不断增多时候,工厂类的条件会不断增加,因此它一般只适合工厂类负责创建的对象比较少时使用。当工厂类负责的对象较多时就要用到工厂方法模式了。
工厂方法模式的Product和ConcreteProduct类和简单方法相同,不同的只有工厂的实现
//工厂接口(Creator)
public interface IFactory
{
//声明CreateFactory方法,返回Product类型对象
Product CreateFactory();
}
public interface IFactory
{
//声明CreateFactory方法,返回Product类型对象
Product CreateFactory();
}
//ConcreteCreator
//A工厂
public class AFactory : IFactory
{
public Product CreateFactory()
{
return new ConcreteProduct1();
}
}
//B工厂
public class BFactory : IFactory
{
public Product CreateFactory()
{
return new ConcreteProduct2();
}
}
//A工厂
public class AFactory : IFactory
{
public Product CreateFactory()
{
return new ConcreteProduct1();
}
}
//B工厂
public class BFactory : IFactory
{
public Product CreateFactory()
{
return new ConcreteProduct2();
}
}
static void Main(string[] args)
{
//客户端通过工厂接口判断使用哪个工厂
IFactory operFactory = new AFactory();
Product producer;
producer = operFactory.CreateFactory();
producer.Attribute1 = 2;
producer.Attribute2 = 1.5;
double result = producer.GetResult();
Console.WriteLine(result.ToString());
Console.Read();
}
{
//客户端通过工厂接口判断使用哪个工厂
IFactory operFactory = new AFactory();
Product producer;
producer = operFactory.CreateFactory();
producer.Attribute1 = 2;
producer.Attribute2 = 1.5;
double result = producer.GetResult();
Console.WriteLine(result.ToString());
Console.Read();
}
工厂方法模式把简单工厂的内部逻辑判断移到了客户端代码来进行,当想要加功能时,本来是改工厂类,现在只需修改客户端。