using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Builder { //套餐建造类 abstract class MealBuilder { public abstract void BuildName(); //业务名称 public abstract void BuildExpense(); //固定费用 public abstract void BuildVoice(); //赠送语音 public abstract void BuildFlow(); //赠送流量 public abstract void BuildMessage(); //赠送短信 } //具体套餐建造类(18) class Meal_18Builder : MealBuilder { public override void BuildName() { Console.WriteLine("动感地带上网套餐(18元)"); } public override void BuildExpense() { Console.WriteLine("固定费用:18/月"); } public override void BuildVoice() { Console.WriteLine("赠送语音:30分钟"); } public override void BuildFlow() { Console.WriteLine("赠送流量:50M"); } public override void BuildMessage() { Console.WriteLine("赠送短信:100条"); } } //具体套餐建造类(28) class Meal_28Builder : MealBuilder { public override void BuildName() { Console.WriteLine("动感地带上网套餐(28元)"); } public override void BuildExpense() { Console.WriteLine("固定费用:28/月"); } public override void BuildVoice() { Console.WriteLine("赠送语音:50分钟"); } public override void BuildFlow() { Console.WriteLine("赠送流量:100M"); } public override void BuildMessage() { Console.WriteLine("赠送短信:150条"); } } //具体套餐建造类(38) class Meal_38Builder : MealBuilder { public override void BuildName() { Console.WriteLine("动感地带上网套餐(38元)"); } public override void BuildExpense() { Console.WriteLine("固定费用:38/月"); } public override void BuildVoice() { Console.WriteLine("赠送语音:80分钟"); } public override void BuildFlow() { Console.WriteLine("赠送流量:200M"); } public override void BuildMessage() { Console.WriteLine("赠送短信:200条"); } } //指挥者类 class Director { private MealBuilder meal; public Director(MealBuilder meal) { this.meal = meal; } public void CreateMeal() { meal.BuildName(); meal.BuildExpense(); meal.BuildVoice(); meal.BuildFlow(); meal.BuildMessage(); } } class Program { static void Main(string[] args) { MealBuilder meal_28builder = new Meal_28Builder(); Director director_28 = new Director(meal_28builder); director_28.CreateMeal(); Console.WriteLine(); MealBuilder meal_38builder = new Meal_38Builder(); Director director_38 = new Director(meal_38builder); director_38.CreateMeal(); } } }
Builder(抽象建造者):它为创建一个产品Product对象的各个部件指定抽象接口,在该接口中一般声明两类方法,一类方法是buildPartX(),它们用于创建复杂对象的各个部件;另一类方法是getResult(),它们用于返回复杂对象。Builder既可以是抽象类,也可以是接口。
ConcreteBuilder(具体建造者):它实现了Builder接口,实现各个部件的具体构造和装配方法,定义并明确它所创建的复杂对象,也可以提供一个方法返回创建好的复杂产品对象。
Product(产品角色):它是被构建的复杂对象,包含多个组成部件,具体建造者创建该产品的内部表示并定义它的装配过程。
原文地址:http://blog.csdn.net/ry513705618/article/details/34106863