작성 자 모드 builder

package mode.bulider;

/**
 * 
 *      ,     :  、  、   ;         ,         
 * 
 * */
public class Car {
	Glass glass;
	Wheel wheel;
	Engine engine;
}
package mode.bulider;
/**
 * 
 *      
 * 
 * */
public abstract class Glass {

}

다음은 두 개의 구체 적 인 유리 류 입 니 다.
package mode.bulider;

public class AmericanGlass extends Glass {

	public String toString() {
		return "\"American Glass\"";
	}
}
package mode.bulider;

public class JapaneseGlass extends Glass {
	public String toString() {
		return "\"Japanese Glass\"";
	}
}
package mode.bulider;
/**
 * 
 *      
 * 
 * */
public abstract class Wheel {

}

다음은 두 개의 구체 적 인 타이어 류 입 니 다.
package mode.bulider;

public class AmericanWheel extends Wheel {
	public String toString() {
		return "\"American Wheel\"";
	}
}
package mode.bulider;

public class JapaneseWheel extends Wheel {
	public String toString() {
		return "\"Janpanes Wheel\"";
	}
}
package mode.bulider;
/**
 * 
 *       
 * 
 * */
public abstract class Engine {

}

다음은 두 개의 구체 적 인 엔진 류 입 니 다.
package mode.bulider;

public class ChineseEngine extends Engine {
	public String toString() {
		return "\"Chinese Engine\"";
	}
}
package mode.bulider;

public class FranceEngine extends Engine {
	public String toString() {
		return "\"France Engine\"";
	}
}
package mode.bulider;

/**
 * 
 *           
 * 
 * */
public interface CarBuilder {
	public void buildGlass();

	public void buildWheel();

	public void buildEngine();

	public Car getProduct();
}

다음은 두 가지 구체 적 인 실현 이다.
package mode.bulider;

public class CarABuilder implements CarBuilder {

	private Car product = null;

	public CarABuilder() {
		product = new Car();
	}

	@Override
	public void buildGlass() {
		product.glass = new AmericanGlass();
	}

	@Override
	public void buildWheel() {
		product.wheel = new AmericanWheel();
	}

	@Override
	public void buildEngine() {
		product.engine = new ChineseEngine();
	}

	@Override
	public Car getProduct() {
		buildGlass();
		buildEngine();
		buildWheel();
		return product;
	}

}
package mode.bulider;

public class CarBBuilder implements CarBuilder {
	private Car product;

	public CarBBuilder() {
		product = new Car();
	}

	@Override
	public void buildGlass() {
		product.glass = new JapaneseGlass();
	}

	@Override
	public void buildWheel() {
		product.wheel = new JapaneseWheel();
	}

	@Override
	public void buildEngine() {
		product.engine = new FranceEngine();
	}

	@Override
	public Car getProduct() {
		buildEngine();
		buildGlass();
		buildWheel();
		return product;
	}

}
package mode.bulider;

/**
 * 
 *        
 * 
 * */
public class Director {
	private CarBuilder builder;

	public Director(CarBuilder builder) {
		this.builder = builder;
	}

	public Car Construct() {
		return builder.getProduct();
	}
}

테스트
package mode.bulider;

public class Test {
	public static void main(String[] args) {
		Director d1 = new Director(new CarABuilder());
		Director d2 = new Director(new CarBBuilder());
		d1.Construct();
		d2.Construct();
	}
}

좋은 웹페이지 즐겨찾기