抽象工程模式实例
---恢复内容开始---
用户需求:
用程序模拟以下产品线并代码实现
设计思路:
1.UML图
2.采用抽象工厂模式,将具体产品从客户代码中分离,容易改变产品的系列,能将一个系列的产品族统一到一起创建。
具体代码实现:
1.抽象工厂模式实现类
using 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(); } } } }
2.KFC生产线实现
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(); } } }
3.McDonalds生产线实现
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
我的感悟:
1、 通过本次实验,加深了对抽象工厂模式的理解。抽象工厂提供一个创建一系列相关或相互依赖对象的接口,而不需指定他们具体的类。抽象工厂同 样是存在缺点的,以支持新种类的产品。
2、 由于以前对C++不太了解,本次实验加深了对C++的了解,强化了编程能力。理解了构造函数,虚函数,纯虚函数,成员函数实现,类之间的继承 等含义。
3、 但对于函数的调用问题常常出错,这在以后的学习中应多加注意和练习。
---恢复内容结束---