• 建造者模式


    ---恢复内容开始---

    用户需求:

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

             

     

    设计思路:

         1.UML图

                

    具体代码实现:

    1.创建一个创造套餐的建造者接口IMealBuilder

    public interface IMealBuilder {
    
        public void BuildBurger();
        public void BuildChips();
        public void BuildDrink();
        Meal GetMeal();
    }

    2.编写Meal类用来存放建造者需要的建造的食物。

    public class Meal
    {
        private  List<IFood> foodList=new ArrayList<IFood>();
        public void addFood( IFood food )
        {
            foodList.add(food);
        }
        public void Show()
        {
            for (int i=0; i<foodList.size(); i++)
            {
                foodList.get(i).Make();
            }
        } 
    }

    3.分别创建肯德基、麦当劳两家店的建造者KFCBuilder、McDonaldBuilder,这俩家店都有相似套餐:汉堡+薯条+饮料,但是产品不同。所以都继承同样的建造过程IMealBuilder

    以下是肯德基的建造者KFCBuilder:

    public class KFCBuilder implements IMealBuilder
    {
        protected Meal meal=new Meal();
        @Override
        public void BuildBurger() {
            // TODO Auto-generated method stub
            IFood food = new KFCBurger();
            meal.addFood(food);
        }
    
        @Override
        public void BuildChips() {
            // TODO Auto-generated method stub
            IFood food = new KFCChips();
            meal.addFood(food);
        }
    
        @Override
        public void BuildDrink() {
            // TODO Auto-generated method stub
            IFood food = new KFCDrink();
            meal.addFood(food);
        }
    
        @Override
        public Meal GetMeal() {
            // TODO Auto-generated method stub
            return meal;
        }
    }

    以下是麦当劳的建造者McDonaldBuilder:

    public class McDonaldBuilder implements IMealBuilder{
    
        protected Meal meal=new Meal();
        @Override
        public void BuildBurger() {
            // TODO Auto-generated method stub
            IFood food = new McDonaldBurger();
            meal.addFood(food);
        }
    
        @Override
        public void BuildChips() {
            // TODO Auto-generated method stub
            IFood food = new McDonaldChips();
            meal.addFood(food);
        }
    
        @Override
        public void BuildDrink() {
            // TODO Auto-generated method stub
            IFood food = new McDonaldDrink();
            meal.addFood(food);
        }
    
        @Override
        public Meal GetMeal() {
            // TODO Auto-generated method stub
            return meal;
        }
    }

    4.创建一个指挥者MealDirector用来负责控制产品的创建过程。

    public class MealDirector{
        
        private IMealBuilder _builder;
        public MealDirector( IMealBuilder builder )
        {
            _builder = builder;
        }
        public Meal CreateMeal()
        {
            _builder.BuildBurger();
            _builder.BuildChips();
            _builder.BuildDrink();
            
            return _builder.GetMeal(); 
        }
    }

    5.创建一个食物的接口IFood,具有制作事物的方法Make()。

    public interface IFood{
    2    
    3     public abstract void Make();
    4 }

    6.分别编写肯德基、麦当劳各种食品的制作方法,以输出为反馈。

    public class KFCBurger implements IFood {
    
        @Override
        public void Make() {
            // TODO Auto-generated method stub
            System.out.println("KFC汉堡做好了!");        
        }
        
    }
    
    KFCBurger
    KFCBurger
    public class KFCChips implements IFood {
    
        @Override
        public void Make() {
            // TODO Auto-generated method stub
            System.out.println("KFC薯条做好了!");
        }
    
    }
    
    KFCChips
    KFCChips
    public class KFCDrink implements IFood {
    
        @Override
        public void Make() {
            // TODO Auto-generated method stub
            System.out.println("KFC饮料做好了!");
        }
    
    }
    
    KFCDrink
    KFCDrink
    public class McDonaldBurger implements IFood {
    
        @Override
        public void Make() {
            // TODO Auto-generated method stub
            System.out.println("McDonald汉堡做好了!");
        }
    
    }
    
    McDonaldBurger
    McDonaldBurger
    public class McDonaldChips implements IFood {
    
        @Override
        public void Make() {
            // TODO Auto-generated method stub
            System.out.println("McDonald薯条做好了!");
        }
    
    }
    
    McDonaldChips
    McDonaldChips
    public class McDonaldDrink implements IFood {
    
        @Override
        public void Make() {
            // TODO Auto-generated method stub
            System.out.println("McDonald饮料做好了!");
        }
    
    }
    
    McDonaldDrink
    McDonaldDrink

    7.书写主函数Main来分别创建肯德基和麦当劳的建造者和指挥者,来制作各自的套餐。

    public class Main {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            IMealBuilder builder1 = new KFCBuilder();
            MealDirector director1 = new MealDirector(builder1);
            director1.CreateMeal().Show();;
    
            IMealBuilder builder2 = new McDonaldBuilder();
            MealDirector director2 = new MealDirector(builder2);
            director2.CreateMeal().Show();
        }
    
    }

    输出结果看是否符合要求:

    ---恢复内容结束---

  • 相关阅读:
    kvm克隆虚拟机
    vSAN添加license
    KVM虚拟机快照
    第二次作业及总结——数据类型和运算符
    第二次作业心得
    做完c语言作业的心得
    介绍自己
    awk数组 Jazz
    Java入门第一阶段总结
    区间dp入门+例题
  • 原文地址:https://www.cnblogs.com/leaderchen/p/5083417.html
Copyright © 2020-2023  润新知