标签:
简单工厂:
public class SendFactory {
public Sender produce(String type) {
if ("mail".equals(type)) {
return new MailSender();
} else if ("sms".equals(type)) {
return new SmsSender();
} else {
System.out.println("请输入正确的类型!");
return null;
}
}
}
public class SendFactory {
public Sender produceMail(){
return new MailSender();
}
public Sender produceSms(){
return new SmsSender();
}
}
public class SendFactory {
public static Sender produceMail(){
return new MailSender();
}
public static Sender produceSms(){
return new SmsSender();
}
}
工厂方法:
public enum type
{
SMS, Mail
}
public abstract class sender
{
public static string[] typeArray = new string[] { "SMS", "Mail" };
public type t;
public string senderName;
}
class Mail : sender
{
public Mail()
{
this.senderName = "张飞";
this.t = type.Mail;
}
}
//抽象类
abstract class SendOneMessage
{
abstract protected sender CreateSender(type t);
public void send(type t)
{
sender s = CreateSender(t);
if( s != null){
Console.Write(s.senderName + "发送一条" + sender.typeArray[(int)t] + "消息");
}
else{
Console.Write("wrong");
}
}
}
/// <summary>
/// 工厂方法(通过继承实现)
/// </summary>
class SendOneMail : SendOneMessage
{
public SendOneMail()
{
}
protected override sender CreateSender(type t)
{
if (t == type.Mail)
{
return new Mail();
}
else
{
return null;
}
}
}
static int Main()//string[] args
{
SendOneMessage a = new SendOneMessage();
a.send(type.Mail);
Console.ReadLine();
return 0;
}
抽象工厂:
public abstract class weapon
{
public string Name;
public void Description()
{
Console.Write("我是一把" + this.Name + "\n");
}
}
class Sword1 : weapon
{
public Sword1()
{
this.Name = "波刃剑";
}
}
class Axe1 : weapon
{
public Axe1()
{
this.Name = "手斧";
}
}
class Sword2 : weapon
{
public Sword2()
{
this.Name = "极光剑";
}
}
class Axe2 : weapon
{
public Axe2()
{
this.Name = "水晶流星";
}
}
//抽象工厂
interface Factory
{
weapon CreateSword();
weapon CreateAxe();
}
class weapon1 : Factory
{
public weapon CreateSword()
{
return new Sword1();
}
public weapon CreateAxe()
{
return new Axe1();
}
}
class weapon2 : Factory
{
public weapon CreateSword()
{
return new Sword2();
}
public weapon CreateAxe()
{
return new Axe2();
}
}
static int Main()//string[] args
{
weapon1 w1 = new weapon1();
weapon2 w2 = new weapon2();
w1.CreateSword().Description();
w1.CreateAxe().Description();
w2.CreateSword().Description();
w2.CreateAxe().Description();
Console.ReadLine();
return 0;
}
抽象工厂 + 工厂方法
public abstract class equip
{
public string Name;
public void Description()
{
Console.Write("我是一把/件" + this.Name + "\n");
}
}
class Sword1 : equip
{
public Sword1()
{
this.Name = "波刃剑";
}
}
class Axe1 : equip
{
public Axe1()
{
this.Name = "手斧";
}
}
class Sword2 : equip
{
public Sword2()
{
this.Name = "极光剑";
}
}
class Axe2 : equip
{
public Axe2()
{
this.Name = "水晶流星";
}
}
class armour1 : equip
{
public armour1()
{
this.Name = "皮铠";
}
}
class armour2 : equip
{
public armour2()
{
this.Name = "白金铠";
}
}
class gaiter1 : equip
{
public gaiter1()
{
this.Name = "皮护腿";
}
}
class gaiter2 : equip
{
public gaiter2()
{
this.Name = "白金护腿";
}
}
//抽象工厂
interface Factory
{
equip CreateSword(string type);
equip CreateArmour(string type);
}
class equip1 : Factory
{
public equip CreateSword(string type)
{
if (type == "Sword")
{
return new Sword1();
}
else
{
return new Axe1();
}
}
public equip CreateArmour(string type)
{
if (type == "armour")
{
return new armour1();
}
else
{
return new gaiter1();
}
}
}
class equip2 : Factory
{
public equip CreateSword(string type)
{
if (type == "Sword")
{
return new Sword2();
}
else
{
return new Axe2();
}
}
public equip CreateArmour(string type)
{
if (type == "armour")
{
return new armour2();
}
else
{
return new gaiter2();
}
}
}
static int Main()//string[] args
{
equip1 e1 = new equip1();
equip2 e2 = new equip2();
e1.CreateSword("Sword").Description();
e1.CreateSword("Axe").Description();
e1.CreateArmour("armour").Description();
e1.CreateArmour("gaiter").Description();
e2.CreateSword("Sword").Description();
e2.CreateSword("Axe").Description();
e2.CreateArmour("armour").Description();
e2.CreateArmour("gaiter").Description();
Console.ReadLine();
return 0;
}
1 工厂方法模式(Factory Method)和 2 抽象工厂模式(Abstract Factory)
标签:
原文地址:http://www.cnblogs.com/wj033/p/4485499.html