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

设计模式那点事读书笔记(3)----建造者模式

时间:2016-08-09 00:00:59      阅读:405      评论:0      收藏:0      [点我收藏+]

标签:

建造者模式: 建造者模式是将复杂的对象构造进行分离,使得每个部件完成比较独立的工作.通过抽象类,接口来约束建造的过程.

解决什么问题: 

  当生成的产品对象内部具有复杂的结构.

  当复杂对象需要与表示分离,可能需要创建不同的表示时.

  当需要向客户隐藏产品内部结构表现时.

UML:

  技术分享

代码结构:

  技术分享

商品的实体:

package com.demo.builder.model;

/**
 * 产品实体
 */
public class MobilePackage {
    private float money;
    private int shortInfo;
    private String music;

    public float getMoney() {
        return money;
    }

    public void setMoney(float money) {
        this.money = money;
    }

    public int getShortInfo() {
        return shortInfo;
    }

    public void setShortInfo(int shortInfo) {
        this.shortInfo = shortInfo;
    }

    public String getMusic() {
        return music;
    }

    public void setMusic(String music) {
        this.music = music;
    }
}

建造者的抽象类:

package com.demo.builder.base;

import com.demo.builder.model.MobilePackage;

/**
 * 建造者抽象类
 */
public abstract class AbstractBasePackage {
    protected MobilePackage mobilePackage;

    public AbstractBasePackage() {
        this.mobilePackage = new MobilePackage();
    }
}

抽象建造者的行为:

package com.demo.builder.itf;

import com.demo.builder.model.MobilePackage;

/**
 * 抽象建造者行为
 */
public interface IMobileBuilder {
    void buildMoney();
    void buildShortInfo();
    void buildMusic();
    MobilePackage getMobilePackage();
}

建造者实体1:

package com.demo.builder.itf;

import com.demo.builder.base.AbstractBasePackage;
import com.demo.builder.model.MobilePackage;

/**
 * 建造者实体1
 */
public class MobileBuilderImpl1 extends AbstractBasePackage implements IMobileBuilder{
    @Override
    public void buildMoney() {
        this.mobilePackage.setMoney(20.0f);
    }

    @Override
    public void buildShortInfo() {
        this.mobilePackage.setShortInfo(400);
    }

    @Override
    public void buildMusic() {
        this.mobilePackage.setMusic("歌曲1");
    }

    @Override
    public MobilePackage getMobilePackage() {
        return this.mobilePackage;
    }
}

建造者的实体2:

package com.demo.builder.itf;

import com.demo.builder.base.AbstractBasePackage;
import com.demo.builder.model.MobilePackage;

/**
 * 建造者类2
 */
public class MobileBuilderImpl2 extends AbstractBasePackage implements IMobileBuilder{
    @Override
    public void buildMoney() {
        this.mobilePackage.setMoney(30.0f);
    }

    @Override
    public void buildShortInfo() {
        this.mobilePackage.setShortInfo(600);
    }

    @Override
    public void buildMusic() {
        this.mobilePackage.setMusic("歌曲2");
    }

    @Override
    public MobilePackage getMobilePackage() {
        return this.mobilePackage;
    }
}

指导者:

package com.demo.builder.director;

import com.demo.builder.itf.IMobileBuilder;
import com.demo.builder.model.MobilePackage;

/**
 * 建造指导者
 */
public class MobileDirector {
    public MobilePackage createMobilePackage(IMobileBuilder mobileBuilder) {
        if (mobileBuilder != null) {
            mobileBuilder.buildMoney();
            mobileBuilder.buildMusic();
            mobileBuilder.buildShortInfo();
            return mobileBuilder.getMobilePackage();
        }
        return null;
    }
}

实现方法:

package main;

import com.demo.builder.director.MobileDirector;
import com.demo.builder.itf.MobileBuilderImpl1;
import com.demo.builder.itf.MobileBuilderImpl2;
import com.demo.builder.model.MobilePackage;

/**
 * Created by Administrator on 2016/8/8.
 */
public class MainApp {
    public static void main(String[] args) {
        MobileDirector mobileDirector = new MobileDirector();

        MobileBuilderImpl1 mobileBuilderImpl1 = new MobileBuilderImpl1();
        MobileBuilderImpl2 mobileBuilderImpl2 = new MobileBuilderImpl2();

        printMessage(mobileDirector.createMobilePackage(mobileBuilderImpl1));
        printMessage(mobileDirector.createMobilePackage(mobileBuilderImpl2));
    }

    public static void printMessage(MobilePackage mobilePackage) {
        System.out.println("--话费: " + mobilePackage.getMoney() + "\t短信: " + mobilePackage.getShortInfo() + "\t彩铃: " + mobilePackage.getMusic());
    }
}

  

设计模式那点事读书笔记(3)----建造者模式

标签:

原文地址:http://www.cnblogs.com/arctique/p/5751303.html

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