上一次学习了工厂模式,这一学习了建造者模式,对于建造者来说,抽象出来的建筑流程是确定的,往往建筑一座楼房包括下面的步骤:(1)打桩,建立基础(2)建立框架等。建造者模式的本质和建造楼房是一致的:即流程不变,但每个流程实现的具体细节则是经常变化的。建造者模式的好处就是保证了流程不会变化,流程即不会增加、也不会遗漏或者产生流程次序错误,这是非常重要的。
这次代码构思如下:
接下来是代码展示:
Client:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 建造者模式作业
{
class Client
{
static void Main(string[] args)
{
MealKFCBuilder kfcbuilder = new MealKFCBuilder();
MealDirector kfcdirector = new MealDirector(kfcbuilder);
kfcdirector.CreateMeal().Make();
Console.Read();
}
}
}
definitePart:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 建造者模式作业
{
/// <summary>
/// 薯条部分
/// </summary>
class definitePart
{
}
public class KFCChips : part
{
public override void Make()
{
Console.WriteLine("KFC薯条制作完成");
}
}
public class MChips : part
{
public override void Make()
{
Console.WriteLine("麦当劳薯条制作完成");
}
}
public class KFCBuger : part
{
public override void Make()
{
Console.WriteLine("KFC汉堡制作完成");
}
}
public class MBuger : part
{
public override void Make()
{
Console.WriteLine("麦当劳汉堡制作完成");
}
}
public class KFCDrink : part
{
public override void Make()
{
Console.WriteLine("KFC饮料制作完成");
}
}
public class MDrink : part
{
public override void Make()
{
Console.WriteLine("麦当劳饮料制作完成");
}
}
}
Meal:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 建造者模式作业
{
public class Meal
{
protected List<part> _parts = new List<part>();
public void AddPart(part part)
{
_parts.Add(part);
}
public void Make()
{
foreach (part part in _parts)
{
part.Make();
}
}
}
}
MealBuilder:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 建造者模式作业
{
public abstract class MealBuilder
{
protected Meal _meal = new Meal();
public abstract void BuildBuger();
public abstract void BuildChips();
public abstract void buildDrink();
public Meal GetMeal()
{
return _meal;
}
}
}
MealDirector:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 建造者模式作业
{
public class MealDirector
{
private MealBuilder _mealbuilder;
public MealDirector(MealBuilder mealbuilder)
{
_mealbuilder = mealbuilder;
}
public Meal CreateMeal()
{
_mealbuilder.BuildBuger();
_mealbuilder.BuildChips();
_mealbuilder.buildDrink();
return _mealbuilder.GetMeal();
}
}
}
MealKFCBuilder:MealBuilder
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 建造者模式作业
{
public class MealKFCBuilder:MealBuilder
{
public override void BuildBuger()
{
_meal.AddPart(new KFCBuger());
}
public override void BuildChips()
{
_meal.AddPart(new KFCChips());
}
public override void buildDrink()
{
_meal.AddPart(new KFCDrink());
}
}
}
MealMBuilder:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 建造者模式作业
{
public class MealMBuilder:MealBuilder
{
public override void BuildBuger()
{
_meal.AddPart(new MBuger());
}
public override void BuildChips()
{
_meal.AddPart(new MChips());
}
public override void buildDrink()
{
_meal.AddPart(new MDrink());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 建造者模式作业
{
public abstract class part
{
public abstract void Make();
}
}
通过学习,我明白了:
1.使用建造者模式可以使客户端不必知道产品内部组成的细节。
2.具体的建造者类之间是相互独立的,对系统的扩展非常有利。
3.由于具体的建造者是独立的,因此可以对建造过程逐步细化,而不对其他的模块产生任何影响。