抽象铸币厂(生产money)
public abstract class MoneyMaker
{
public abstract Ten MakeTen();
public abstract Hundred MakeHundred();
}
中国的铸币厂(能生产RMB){
public abstract Ten MakeTen();
public abstract Hundred MakeHundred();
}
public class ChinaMoneyMaker : MoneyMaker
{
public override Ten MakeTen()
{
return new RMBTen();
}
public override Hundred MakeHundred()
{
return new RMBHundred();
}
}
美国的铸币厂(能生产美元){
public override Ten MakeTen()
{
return new RMBTen();
}
public override Hundred MakeHundred()
{
return new RMBHundred();
}
}
public class UsMoneyMaker : MoneyMaker
{
public override Ten MakeTen()
{
return new DollarTen();
}
public override Hundred MakeHundred()
{
return new DollarHundred();
}
}
十元的货币(抽象基类,有支付的功能){
public override Ten MakeTen()
{
return new DollarTen();
}
public override Hundred MakeHundred()
{
return new DollarHundred();
}
}
public abstract class Ten
{
public abstract void Pay();
}
百元的货币{
public abstract void Pay();
}
public abstract class Hundred
{
public abstract void Pay();
}
人民币的十元{
public abstract void Pay();
}
public class RMBTen : Ten
{
public override void Pay()
{
Console.WriteLine("10 RMB have been pay.");
}
}
人民币的百元, 美元的十元, 美元的百元 (略...){
public override void Pay()
{
Console.WriteLine("10 RMB have been pay.");
}
}
测试程序
public class App
{
public static void Main()
{
MoneyMaker mm = new ChinaMoneyMaker();
Ten t = mm.MakeTen();
t.Pay();
Hundred h = mm.MakeHundred();
h.Pay();
mm = new UsMoneyMaker();
t = mm.MakeTen();
t.Pay();
h = mm.MakeHundred();
h.Pay();
Console.ReadLine();
}
}
{
public static void Main()
{
MoneyMaker mm = new ChinaMoneyMaker();
Ten t = mm.MakeTen();
t.Pay();
Hundred h = mm.MakeHundred();
h.Pay();
mm = new UsMoneyMaker();
t = mm.MakeTen();
t.Pay();
h = mm.MakeHundred();
h.Pay();
Console.ReadLine();
}
}
源程序下载 AbstractFactory Demo