Java设计模式 - 生成器模式

目录

生成器模式(建造者模式):

生成器优点:

生成器模式和抽象工厂模式区别:

生成器模式和策略模式区别:

生成器模式应用:

生成器实例:

类图:

示例:

代码:

上述代码 GitHub 地址:https://github.com/baicun/designPatterns


生成器模式(建造者模式):

封装一个复杂对象构建过程,并允许按步骤构造

生成器优点:

将复杂对象的创建过程封装起来,只需要指定具体的生成器就能生成特定对象

生成器模式和抽象工厂模式区别:

生成器在多步创建过程的最后一步返回产品,而抽象工厂则立即返回产品。

生成器关注的是分步创建复杂对象,很多时候同一类型的对象可以以不同的方式创建,而抽象工厂的重点在于创建简单或复杂的一整套组件。

生成器模式和策略模式区别:

生成器模式是创建型的,也就是说用来创建对象的,而策略模式属于行为型模式,通过将行为封装成对象来降低类之间的耦合度

生成器模式应用:

java.lang.StringBuilder#append(),

java.lang.StringBuffer#append()同样也是符合生成器模式,只不过方法加了synchronized,保证了线程安全。

生成器实例:

类图:

Java设计模式 - 生成器模式_第1张图片

示例:

生成旅游计划:旅游时,每天我们需要计划旅游景点、事项、时间、酒店,那一次假期可能是多个一天组成的,旅行社就会推出3天经典线路,4天经典线路...为了方便封装每天计划,我们用一个抽象类实现对应的功能,并具体拓展出不同天数的计划,最后由Director 中进行选择。这个模式了一选择生成器模式。

代码:

每日计划实体:

public class VacationDay {
	private Date mDate;
	private String mHotels;
	private ArrayList mTickets = null;
	private ArrayList mEvents = null;

	public VacationDay(Date date) {
		mDate = date;
		mTickets = new ArrayList();
		mEvents = new ArrayList();
	}

	public void setDate(Date date) {
		mDate = date;
	}

	public void setHotel(String mHotels) {
		this.mHotels = mHotels;
	}

	public void addTicket(String ticket) {
		mTickets.add(ticket);
	}

	public void addEvent(String event) {
		mEvents.add(event);
	}

	public String showInfo() {
		StringBuilder stb = new StringBuilder();
		stb.append("Date:" + mDate.toString() + "\n");
		stb.append("Hotel:" + mHotels + "\n");
		stb.append("Tickets:" + mTickets.toString() + "\n");
		stb.append("Events" + mEvents.toString() + "\n");

		return stb.toString();
	}
}

假期实体:

public class Vacation {
	private Date mStDate;
	private int mDays = 0;
	private VacationDay mVacationDay;
	private ArrayList mVacationDayLst;

	public Vacation(String std) {
		mVacationDayLst = new ArrayList();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			mStDate = sdf.parse(std);
			mVacationDay = new VacationDay(mStDate);
			mVacationDayLst.add(mVacationDay);
			mDays++;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void setStDate(String std) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			mStDate = sdf.parse(std);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public Date getStDate() {

		return mStDate;
	}

	public void addDay() {

		mVacationDay = new VacationDay(nextDate(mDays));
		mVacationDayLst.add(mVacationDay);
		mDays++;
	}

	public boolean setVacationDay(int i) {
		if ((i > 0) && (i < mVacationDayLst.size())) {
			mVacationDay = mVacationDayLst.get(i);
			return true;
		}
		mVacationDay = null;
		return false;
	}

	public void setHotel(String mHotels) {
		mVacationDay.setHotel(mHotels);
	}

	public void addTicket(String ticket) {
		mVacationDay.addTicket(ticket);
	}

	public void addEvent(String event) {
		mVacationDay.addEvent(event);
	}

	public void showInfo() {
		for (int i = 0, len = mVacationDayLst.size(); i < len; i++) {
			System.out.println("** " + (i + 1) + " day**");
			System.out.println(mVacationDayLst.get(i).showInfo());

		}
	}

	private Date nextDate(int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(mStDate);
		cal.add(Calendar.DATE, n);
		return cal.getTime();
	}
}

抽象生成器:

//抽象建造者
public abstract class AbsBuilder {

	public Vacation mVacation;

	public AbsBuilder(String std) {
		mVacation = new Vacation(std);
	}

	public abstract void buildvacation();

	public abstract void buildDay(int i);

	public abstract void addHotel(String hotel);

	public abstract void addTicket(String ticket);

	public abstract void addEvent(String tvent);

	public Vacation getVacation() {

		return mVacation;

	}

}

 具体生成器:

public class Builder3d extends AbsBuilder {

    public Builder3d(String std) {
        super(std);
        // TODO Auto-generated constructor stub

    }

    @Override
    public void buildDay(int i) {
        // TODO Auto-generated method stub

        mVacation.setVacationDay(i);

    }

    @Override
    public void addHotel(String hotel) {
        // TODO Auto-generated method stub
        mVacation.setHotel(hotel);
    }

    @Override
    public void addTicket(String ticket) {
        // TODO Auto-generated method stub
        mVacation.addTicket(ticket);
    }

    @Override
    public void addEvent(String event) {
        // TODO Auto-generated method stub
        mVacation.addEvent(event);
    }

    @Override
    public void buildvacation() {
        // TODO Auto-generated method stub
        addTicket("Plane Ticket");
        addEvent("Fly to Destination");
        addEvent("Supper");
        addEvent("Dancing");
        addHotel("Four Seasons");

        mVacation.addDay();
        addTicket("Theme Park");
        addEvent("Bus to Park");
        addEvent("lunch");
        addHotel("Four Seasons");

        mVacation.addDay();

        addTicket("Plane Ticket");
        addEvent("City Tour");
        addEvent("Fly to Home");

    }

}

 Director包装类:

public class Director {
	private AbsBuilder builder;
	
	public Director(AbsBuilder builder)
	{
		this.builder=builder;
	}
	public void setBuilder(AbsBuilder builder)
	{
		this.builder=builder;
	}
	public void construct()
	{
		builder.buildvacation();
		builder.getVacation().showInfo();
	}
}

测试类:

public class MainTest {

	public static void main(String[] args) {

		Director mDirector = new Director(new Builder4d("2015-12-29"));

		mDirector.construct();

		mDirector.setBuilder(new Builder3d("2015-8-30"));
		mDirector.construct();
	}

}

 

上述代码 GitHub 地址:https://github.com/baicun/designPatterns

你可能感兴趣的:(碎片时间学习,java,23种设计模式,Java基础,java设计模式)