概述:
简单工厂,通过具体工厂类、抽象产品、具体产品类设计,封装具体产品。
实现如下
具体代码实现:
接口(产品):
internal interface Clothes
{
String Name { get; set; }
String Code { get; set; }
void Display();
{
String Name { get; set; }
String Code { get; set; }
void Display();
}
具体产品A:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleSimpleFactoryDemo
{
internal class FemaleShirtClothes : Clothes
{
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 ConsoleSimpleFactoryDemo
{
internal class FemaleShirtClothes : Clothes
{
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 ConsoleSimpleFactoryDemo
{
internal class FemaleTshirtsClothes : Clothes
{
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.Linq;
using System.Text;
namespace ConsoleSimpleFactoryDemo
{
internal class FemaleTshirtsClothes : Clothes
{
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 ConsoleSimpleFactoryDemo
{
internal class FemaleClothesFactory
{
public static Clothes GetInstance(String clothes)
{
switch (clothes.Trim().ToLower())
{
case "shirt":
return new FemaleShirtClothes();
case "tshirts":
return new FemaleTshirtsClothes();
default:
throw new Exception("没有你要的衣服");
}
}
}
using System.Linq;
using System.Text;
namespace ConsoleSimpleFactoryDemo
{
internal class FemaleClothesFactory
{
public static Clothes GetInstance(String clothes)
{
switch (clothes.Trim().ToLower())
{
case "shirt":
return new FemaleShirtClothes();
case "tshirts":
return new FemaleTshirtsClothes();
default:
throw new Exception("没有你要的衣服");
}
}
}
}
客户端调用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleSimpleFactoryDemo
{
class Program
{
static void Main(string[] args)
{
Clothes clothes;
clothes=FemaleClothesFactory.GetInstance("Shirt");
clothes.Display();
Console.ReadLine();
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleSimpleFactoryDemo
{
class Program
{
static void Main(string[] args)
{
Clothes clothes;
clothes=FemaleClothesFactory.GetInstance("Shirt");
clothes.Display();
Console.ReadLine();
}
}
}
简单工厂,分离了具体的类,通过控制具体工厂来实现对产品的控制。不过有个缺点就是简单工厂的switch实现比较麻烦。每次添加一个新的具体产品都需要去修改。