大话设计模式_建造者模式

以绘制小人为例子。

package com.wzs.design;

/**
 * 大话设计模式--page116 建造者模式
 * 
 * @author Administrator
 * 
 */
public class BuilderPattern {
	public static void main(String[] args) {
		PersonDirector personThinDirector = new PersonDirector(new PersonThinBuilder());// 瘦子
		personThinDirector.createPerson();
		System.out.println();
		PersonDirector personFatDirector = new PersonDirector(new PersonFatBuilder());// 胖子
		personFatDirector.createPerson();
	}
}

/*
 * 指挥者
 */
class PersonDirector {
	private PersonBuilder personBuilder;

	public PersonDirector(PersonBuilder personBuilder) {
		this.personBuilder = personBuilder;
	}

	// 绘制人
	public void createPerson() {
		personBuilder.buildHead();
		personBuilder.buildBody();
		personBuilder.buildArm();
		personBuilder.buildLeg();
	}
}

/*
 * 绘制人的抽象类
 */
abstract class PersonBuilder {
	public abstract void buildHead();

	public abstract void buildBody();

	public abstract void buildArm();

	public abstract void buildLeg();
}

/*
 * 瘦子
 */
class PersonThinBuilder extends PersonBuilder {

	@Override
	public void buildArm() {
		System.out.println("瘦子的手.");
	}

	@Override
	public void buildBody() {
		System.out.println("瘦子的身体.");
	}

	@Override
	public void buildHead() {
		System.out.println("瘦子的头.");
	}

	@Override
	public void buildLeg() {
		System.out.println("瘦子的腿.");
	}

}

/*
 * 胖子
 */
class PersonFatBuilder extends PersonBuilder {
	@Override
	public void buildArm() {
		System.out.println("胖子的手.");
	}

	@Override
	public void buildBody() {
		System.out.println("胖子的身体.");
	}

	@Override
	public void buildHead() {
		System.out.println("胖子的头.");
	}

	@Override
	public void buildLeg() {
		System.out.println("胖子的腿.");
	}
}


你可能感兴趣的:(建造者模式,大话设计模式,绘制小人)