你的位置:首页 > ASP.net教程

[ASP.net教程]抽象工厂模式实例


用户需求:

        用程序模拟以下产品线并代码实现

        

设计思路:

        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

体会和感悟:

         抽象工厂模式提供了用于创建一系列相关的对象的接口,而无需定义它们的具体的类,将具体产品从客户代码中分离,实现了程序的松耦合。采用OOP和分层的思想,更好的实现程序的分离,提高程序的易扩展性。抽象工厂在产品族中增加新的产品类型较为困难,因为需要修改已有的抽象工厂类,在一定程度上违反了"开放封闭原则"。通过本次实践,更好的理解了抽象工厂模式的实现和好处。