码迷,mamicode.com
首页 > 其他好文 > 详细

建造者模式

时间:2015-12-28 20:25:56      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

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

用户需求:

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

         

 技术分享

设计思路:

     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();
    }

}

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

技术分享

 

 

 

 

 

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

建造者模式

标签:

原文地址:http://www.cnblogs.com/leaderchen/p/5083417.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!