标签:
用户需求:
用程序模拟以下产品线并代码实现
UML类图:
抽象工厂模式有具体产品从客户代码中分离、容易改变产品的系列以及将一个系列的产品族统一到一起创建三个优点。
具体代码如下:
sing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace 设计模式作业
{
public class MyClass
{
public interface Burger {
string getBurger();
}
public class KFCBurger :Burger{
public string getBurger() {
return "我是KFC的巨无霸汉堡";
}
}
public class MCBurger : Burger {
public string getBurger()
{
return "我是McDonalds的巨无霸汉堡";
}
}
public interface Chicken {
string getChicken();
}
public class KFCChicken : Chicken {
public string getChicken()
{
return "我是来自KFC的炸鸡";
}
}
public class MCChicken : Chicken {
public string getChicken()
{
return "我是来自McDonalds的炸鸡";
}
}
public interface Drink {
string getDrink();
}
public class KFCDrink : Drink {
public string getDrink()
{
return "我是来自KFC的咖啡";
}
}
public class MCDrink : Drink {
public string getDrink()
{
return "我是来自McDonalds的橙汁";
}
}
public interface AbstractFactory {
Burger createBurger();
Chicken createChicken();
Drink createDrink();
}
public class KFCFactory : AbstractFactory {
public Burger createBurger() {
return new KFCBurger();
}
public Chicken createChicken() {
return new KFCChicken();
}
public Drink createDrink() {
return new KFCDrink();
}
}
public class MCFactory : AbstractFactory
{
public Burger createBurger()
{
return new MCBurger();
}
public Chicken createChicken()
{
return new MCChicken();
}
public Drink createDrink()
{
return new MCDrink();
}
}
}
}
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace 设计模式作业 { public partial class WebForm1 : System.Web.UI.Page { MyClass.AbstractFactory af = new MyClass.KFCFactory(); protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { MyClass.Burger kfcburger = af.createBurger(); Label1.Text = kfcburger.getBurger(); } protected void Button2_Click(object sender, EventArgs e) { MyClass.Chicken kfcckicken = af.createChicken(); Label1.Text = kfcckicken.getChicken(); } protected void Button3_Click(object sender, EventArgs e) { MyClass.Drink kfcdrink = af.createDrink(); Label1.Text = kfcdrink.getDrink(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace 设计模式作业 { public partial class WebForm2 : System.Web.UI.Page { MyClass.AbstractFactory af = new MyClass.MCFactory(); protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { MyClass.Burger mcburger = af.createBurger(); Label1.Text = mcburger.getBurger(); } protected void Button2_Click(object sender, EventArgs e) { MyClass.Chicken mcchicken = af.createChicken(); Label1.Text = mcchicken.getChicken(); } protected void Button3_Click(object sender, EventArgs e) { MyClass.Drink mcdrink = af.createDrink(); Label1.Text = mcdrink.getDrink(); } } }
运行结果:
点击KFC按钮
创建KFC的Burger
创建KFC的Chicken
创建KFC的Drink
点击McDonalds按钮
创建McDonalds的Burger
创建McDonalds的Chicken
创建McDonalds的Drink
标签:
原文地址:http://www.cnblogs.com/zjl931223/p/5096921.html