工厂方法 学习手记
概述
简单工厂存在着不少的缺点,工厂类就违反了“对扩展开放、对修改关闭”的原则。而工厂方法可以克服简单工厂的缺点。
工厂方法通过定义一个用户创建对象的接口,让子类决定实例化哪一个类。Factory Method使一个类的实例化延迟到其子类。
实现例子:
产品接口:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleFactoryMethodDemo
{
internal interface FemaleClothes
{
String Name { get; set; }
String Code { get; set; }
void Display();
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleFactoryMethodDemo
{
internal interface FemaleClothes
{
String Name { get; set; }
String Code { get; set; }
void Display();
}
}
具体产品类A:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleFactoryMethodDemo
{
internal class FemaleShirtClothes : FemaleClothes
{
private String name="衬衫";
public String Name
{
get { return name="衬衫"; }
set { name="衬衫"; }
}
private String code="ChenShan";
public String Code
{
get { return code="ChenShan"; }
set { code="ChenShan"; }
}
public void Display()
{
Console.WriteLine("商品名称是:"+Name+" 拼音是:"+Code);
}
}
}
using System.Linq;
using System.Text;
namespace ConsoleFactoryMethodDemo
{
internal class FemaleShirtClothes : FemaleClothes
{
private String name="衬衫";
public String Name
{
get { return name="衬衫"; }
set { name="衬衫"; }
}
private String code="ChenShan";
public String Code
{
get { return code="ChenShan"; }
set { code="ChenShan"; }
}
public void Display()
{
Console.WriteLine("商品名称是:"+Name+" 拼音是:"+Code);
}
}
}
具体产品类B:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleFactoryMethodDemo
{
internal class FemaleTshirtsClothes : FemaleClothes
{
private String name="T恤";
public String Name
{
get { return name="T恤"; }
set { name="T恤"; }
}
private String code="T-Shirts";
public String Code
{
get { return code ="T-Shirts"; }
set { code = "T-Shirts"; }
}
public void Display()
{
Console.WriteLine("商品名称是:" + Name + " 拼音是:" + Code);
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleFactoryMethodDemo
{
internal class FemaleTshirtsClothes : FemaleClothes
{
private String name="T恤";
public String Name
{
get { return name="T恤"; }
set { name="T恤"; }
}
private String code="T-Shirts";
public String Code
{
get { return code ="T-Shirts"; }
set { code = "T-Shirts"; }
}
public void Display()
{
Console.WriteLine("商品名称是:" + Name + " 拼音是:" + Code);
}
}
}
抽象工厂:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleFactoryMethodDemo
{
internal interface FemaleClothesFactory
{
FemaleClothes GetInstance();
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleFactoryMethodDemo
{
internal interface FemaleClothesFactory
{
FemaleClothes GetInstance();
}
}
具体工厂A:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleFactoryMethodDemo
{
internal class FemaleShirtClothesFactory : FemaleClothesFactory
{
FemaleShirtClothes clothes;
public FemaleClothes GetInstance()
{
clothes= new FemaleShirtClothes();
return clothes;
}
}
}
using System.Linq;
using System.Text;
namespace ConsoleFactoryMethodDemo
{
internal class FemaleShirtClothesFactory : FemaleClothesFactory
{
FemaleShirtClothes clothes;
public FemaleClothes GetInstance()
{
clothes= new FemaleShirtClothes();
return clothes;
}
}
}
具体工厂B:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleFactoryMethodDemo
{
internal class FemaleTshirtsClothesFactory:FemaleClothesFactory
{
FemaleTshirtsClothes clothes;
public FemaleClothes GetInstance()
{
clothes = new FemaleTshirtsClothes();
return clothes;
}
}
}
using System.Linq;
using System.Text;
namespace ConsoleFactoryMethodDemo
{
internal class FemaleTshirtsClothesFactory:FemaleClothesFactory
{
FemaleTshirtsClothes clothes;
public FemaleClothes GetInstance()
{
clothes = new FemaleTshirtsClothes();
return clothes;
}
}
}
客户端调用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleFactoryMethodDemo
{
class Program
{
static void Main(string[] args)
{
FemaleClothesFactory factory = new FemaleShirtClothesFactory();
FemaleClothes clothes = factory.GetInstance();
clothes.Display();
Console.ReadLine();
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleFactoryMethodDemo
{
class Program
{
static void Main(string[] args)
{
FemaleClothesFactory factory = new FemaleShirtClothesFactory();
FemaleClothes clothes = factory.GetInstance();
clothes.Display();
Console.ReadLine();
}
}
}
工厂方法,克服了简单工厂的“开放-封闭”原则,同时将具体对象的创建工作延迟到子类,更具体扩展能力,较好的解决了这种紧耦合的关系。