码迷,mamicode.com
首页 > 其他好文 > 详细

抽象工厂模式

时间:2016-01-03 19:39:30      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:

 

用户需求:

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

         技术分享

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!