分类:
2008-09-23 09:27:24
设计模式-建造模式
参考资料:
《java与模式》
《设计模式-可复用面向对象软件的基础》
《C#技术揭密》
摘要:介绍Gof设计模式创建模式中的建造模式,结合一个生活中的一个,来说明建造模式的使用场合和使用方法
工厂模式中,创建对象是直接创建的,而对一个复杂的对象来说,用建造模式更适合,它一步一步的创建一个复杂对象。
基本的模式是这样的
interface Builder
{
//建造“零件”
void BuildPart1();
void BuildPart2();
//返回产品
Product GetProduct();
}
这样从此类继承的建造者,可以根据上面定义的两种建造零件方法,构造任意复杂的建造者。
/*
* created by sharpdevelop.
* user: administrator
* date: 2004-12-02
* time: 19:57
*
* to change this template use tools | options | coding | edit standard headers.
*/
using System;
namespace builder
{
class mainclass
{
public static void Main(string[] args)
{
SnackShop snackShop=new SnackShop();
NoodleBuilder bcnwc=new BeefCutNoodleWithCarawayBuilder();
Noodle noodle=snackShop.CreateBeefCutNoodleWithCaraway(bcnwc);
Console.ReadLine();
}
}
//定义快餐店
class SnackShop
{
public SnackShop()
{}
//客户端创建加香菜牛肉刀削面的方法
public Noodle CreateBeefCutNoodleWithCaraway(NoodleBuilder noodleBuilder)
{
noodleBuilder.BuildNoodle();
noodleBuilder.BuildMeat();
noodleBuilder.BuildPureNoodle();
noodleBuilder.BuildSoup();
noodleBuilder.BuildVegetable();
Console.WriteLine("一碗香喷喷的牛肉刀削面加香菜做好啦!\n");
return noodleBuilder.GetNoodle();
}
}
//定义面条接口
interface NoodleBuilder
{
void BuildNoodle();
void BuildPureNoodle();
void BuildMeat();
void BuildVegetable();
void BuildSoup();
Noodle GetNoodle();
}
//肉类
public class Meat
{
public Meat()
{
}
void Make()
{
}
}
//纯面条类
public class PureNoodle
{
void Make()
{
}
}
//蔬菜类
public class Vegetable
{
void Make()
{
}
}
//汤类
public class Soup
{
public Soup()
{
}
void Make()
{
}
}
//定义拉面,它从纯面条类继承
public class PullPureNoodle:PureNoodle
{
public PullPureNoodle()
{
Console.WriteLine("拉面做好了\n");
}
public void Make()
{
}
}
//定义刀削面,它从纯面条类继承
public class CutPureNoodle:PureNoodle
{
public CutPureNoodle()
{
}
public void Make()
{
}
}
//定义青菜
public class Greengrocery:Vegetable
{
public Greengrocery()
{
Console.WriteLine("青菜买好了\n");
}
public void Make()
{
}
}
//定义香菜
public class Caraway :Vegetable
{
public Caraway()
{
}
public void Make()
{
}
}
//定义羊肉
public class Mutton:Meat
{
public Mutton()
{
Console.WriteLine("羊肉做好了\n");
}
public void Make()
{
}
}
//定义牛肉
public class Beef:Meat
{
public Beef()
{
}
public void Make()
{
}
}
//定义面条类
public class Noodle
{
public PureNoodle pureNoodle;
public Vegetable vegetable;
public Soup soup;
public Meat meat;
public Noodle()
{
pureNoodle=new PureNoodle();
vegetable=new Vegetable();
soup=new Soup();
meat=new Meat();
}
}
//定义牛肉刀削面的建造者逻辑
class BeefCutNoodleWithCarawayBuilder :NoodleBuilder
{
public BeefCutNoodleWithCarawayBuilder()
{
}
private Noodle noodle;
public void BuildPureNoodle()
{
noodle.pureNoodle=new CutPureNoodle();
Console.WriteLine("2.刀削面做好了\n");
}
public void BuildVegetable()
{
noodle.vegetable=new Caraway();
Console.WriteLine("4.香菜洗好了\n");
}
public void BuildMeat()
{
noodle.meat=new Beef();
Console.WriteLine("1.牛肉做好了 \n");
}
public void BuildSoup()
{
noodle.soup=new Soup();
Console.WriteLine("3.面汤做好了\n");
}
public void BuildNoodle()
{
Console.WriteLine("开始做牛肉刀削面啦 \n");
noodle=new Noodle();
}
public Noodle GetNoodle()
{
return noodle;
}
}
}
/*运行结果:
*
开始做牛肉面条啦
1.牛肉做好了
2.刀削面做好了
3.面汤做好了
4.香菜洗好了
一碗香喷喷的牛肉刀削面做好啦!
*/