根据上述要求来用建造者模式设计程序。我将程序放在了https://github.com/RoyalBob/Builder-Mode-Practice.git 欢迎访问。
以下是本程序的UML图以及代码:
1、创建一个创造套餐的建造者接口IMealBuilder
1 public interface IMealBuilder { 2 3 public void BuildBurger(); 4 public void BuildChips(); 5 public void BuildDrink(); 6 Meal GetMeal(); 7 }
2、编写Meal类用来存放建造者需要建造的食物。
1 public class Meal 2 { 3 private List<IFood> foodList=new ArrayList<IFood>(); 4 public void addFood( IFood food ) 5 { 6 foodList.add(food); 7 } 8 public void Show() 9 { 10 for (int i=0; i<foodList.size(); i++) 11 { 12 foodList.get(i).Make(); 13 } 14 } 15 }
3、分别创建肯德基、麦当劳两家店的建造者KFCBuilder、McDonaldBuilder,这两家店都有相似套餐:汉堡+薯条+饮料,但是产品不同。所以都继承同样的建造过程IMealBuilder
以下是肯德基的建造者KFCBuilder:
1 public class KFCBuilder implements IMealBuilder 2 { 3 protected Meal meal=new Meal(); 4 @Override 5 public void BuildBurger() { 6 // TODO Auto-generated method stub 7 IFood food = new KFCBurger(); 8 meal.addFood(food); 9 } 10 11 @Override 12 public void BuildChips() { 13 // TODO Auto-generated method stub 14 IFood food = new KFCChips(); 15 meal.addFood(food); 16 } 17 18 @Override 19 public void BuildDrink() { 20 // TODO Auto-generated method stub 21 IFood food = new KFCDrink(); 22 meal.addFood(food); 23 } 24 25 @Override 26 public Meal GetMeal() { 27 // TODO Auto-generated method stub 28 return meal; 29 } 30 }
以下是麦当劳的建造者McDonaldBuilder:
1 public class McDonaldBuilder implements IMealBuilder{ 2 3 protected Meal meal=new Meal(); 4 @Override 5 public void BuildBurger() { 6 // TODO Auto-generated method stub 7 IFood food = new McDonaldBurger(); 8 meal.addFood(food); 9 } 10 11 @Override 12 public void BuildChips() { 13 // TODO Auto-generated method stub 14 IFood food = new McDonaldChips(); 15 meal.addFood(food); 16 } 17 18 @Override 19 public void BuildDrink() { 20 // TODO Auto-generated method stub 21 IFood food = new McDonaldDrink(); 22 meal.addFood(food); 23 } 24 25 @Override 26 public Meal GetMeal() { 27 // TODO Auto-generated method stub 28 return meal; 29 } 30 }
4、创建一个指挥者MealDirector用来负责控制产品的创建过程。
1 public class MealDirector{ 2 3 private IMealBuilder _builder; 4 public MealDirector( IMealBuilder builder ) 5 { 6 _builder = builder; 7 } 8 public Meal CreateMeal() 9 { 10 _builder.BuildBurger(); 11 _builder.BuildChips(); 12 _builder.BuildDrink(); 13 14 return _builder.GetMeal(); 15 } 16 }
5、创建一个食物的接口IFood,具有制作食物的方法Make()。
1 public interface IFood{ 2 3 public abstract void Make(); 4 }
6、分别编写肯德基、麦当劳各种食品的制作方法,以输出为反馈。
1 public class KFCBurger implements IFood { 2 3 @Override 4 public void Make() { 5 // TODO Auto-generated method stub 6 System.out.println("KFC汉堡做好了!"); 7 } 8 9 }
KFCBurger
1 public class KFCChips implements IFood { 2 3 @Override 4 public void Make() { 5 // TODO Auto-generated method stub 6 System.out.println("KFC薯条做好了!"); 7 } 8 9 }
KFCChips
1 public class KFCDrink implements IFood { 2 3 @Override 4 public void Make() { 5 // TODO Auto-generated method stub 6 System.out.println("KFC饮料做好了!"); 7 } 8 9 }
KFCDrink
1 public class McDonaldBurger implements IFood { 2 3 @Override 4 public void Make() { 5 // TODO Auto-generated method stub 6 System.out.println("McDonald汉堡做好了!"); 7 } 8 9 }
McDonaldBurger
1 public class McDonaldChips implements IFood { 2 3 @Override 4 public void Make() { 5 // TODO Auto-generated method stub 6 System.out.println("McDonald薯条做好了!"); 7 } 8 9 }
McDonaldChips
1 public class McDonaldDrink implements IFood { 2 3 @Override 4 public void Make() { 5 // TODO Auto-generated method stub 6 System.out.println("McDonald饮料做好了!"); 7 } 8 9 }
McDonaldDrink
7、书写主函数Main来分别创建肯德基和麦当劳的建造者和指挥者,来制作各自的套餐。
1 public class Main { 2 3 public static void main(String[] args) { 4 // TODO Auto-generated method stub 5 IMealBuilder builder1 = new KFCBuilder(); 6 MealDirector director1 = new MealDirector(builder1); 7 director1.CreateMeal().Show();; 8 9 IMealBuilder builder2 = new McDonaldBuilder(); 10 MealDirector director2 = new MealDirector(builder2); 11 director2.CreateMeal().Show(); 12 } 13 14 }
输出结果看是否符合要求:
时间: 2024-11-03 22:18:38