zoukankan      html  css  js  c++  java
  • 设计模式 之 建造者模式

    Waiter并不亲自沏茶泡咖啡,而是在内部使用builder来做。

    package com.test.pattern.builder;
    
    //待构建产品
    class StarBucks {
        private String size;
        private String drink;
        
        public void setSize(String size) {
            this.size=size;
        }
        
        public void setDrink(String drink) {
            this.drink=drink;
        }
    }
    
    //抽象builder
    abstract class StarBucksBuilder {
        protected StarBucks starbucks;
        
        public StarBucks getStarBucks() {
            return starbucks;
        }
        
        public void createStarBucks() {
            starbucks = new StarBucks();
            System.out.println("one drink is created");
        }
        
        public abstract void buildSize();
        public abstract void buildDrink();
    }
    
    //泡茶builder
    class TeaBuilder extends StarBucksBuilder {
    
        @Override
        public void buildSize() {
            // TODO Auto-generated method stub
            starbucks.setSize("medium");
            System.out.println("build medium size");
        }
    
        @Override
        public void buildDrink() {
            // TODO Auto-generated method stub
            starbucks.setDrink("tea");
            System.out.println("build tea");
        }
    }
    
    //泡咖啡builder
    class CoffeeBuilder extends StarBucksBuilder {
    
        @Override
        public void buildSize() {
            // TODO Auto-generated method stub
            starbucks.setSize("medium");
            System.out.println("medium size");
        }
    
        @Override
        public void buildDrink() {
            // TODO Auto-generated method stub
            starbucks.setDrink("coffee");
            System.out.println("build coffee");
        }
    }
    
    //指导如何封装builder
    class Waiter {
        private StarBucksBuilder starBucksBuilder;
    
        public void setStarBucksBuilder(StarBucksBuilder starBucksBuilder) {
            this.starBucksBuilder = starBucksBuilder;
        }
        
        public StarBucks getStarBucksDrink() {
            return starBucksBuilder.getStarBucks();
        }
        
        public void constructStarBucks() {
            starBucksBuilder.createStarBucks();
            starBucksBuilder.buildDrink();
            starBucksBuilder.buildSize();
        }
    }
    //客户
    public class BuilderTest {
        public static void main(String[] args) {
            Waiter waiter = new Waiter();
            StarBucksBuilder coffeeBuilder = new CoffeeBuilder();
            
            //用builder泡咖啡
            waiter.setStarBucksBuilder(coffeeBuilder);
            waiter.constructStarBucks();
            
            //取到饮料
            StarBucks drink = waiter.getStarBucksDrink();
        }
    }
  • 相关阅读:
    P4890 Never·island
    P2617 Dynamic Rankings
    P3243 [HNOI2015]菜肴制作
    P4172 [WC2006]水管局长
    P4219 [BJOI2014]大融合
    P5241 序列
    P1501 [国家集训队]Tree II
    无法读取用户配置文件,系统自动建立Temp临时用户
    组件服务 控制台打不开
    打印服务器 功能地址保护错误
  • 原文地址:https://www.cnblogs.com/heben/p/5773038.html
Copyright © 2011-2022 走看看