디자인패턴 스터디를 시작합니다...
package
{
import flash.display.Sprite;
//추상팩토리 : 생성패턴
//만들질 객체의 과정을 추상화 하여
//구체적인 생산과정과 결과를 구현클클래스(FactoryDoor2, FactoryDoor4)에 위임하는 방법
//장점
//: 객체를 쉽게 대체 가능
//: 일관성
//단점
//: 새로운 종류 객체를 추가 하기가 어렵다.
//* 제품의 설계도만 선택하면 완성 : 사용자가 제작과정이나 제품을 알필요가 없을때
public class AbstractFactory extends Sprite
{
public function AbstractFactory()
{
var main:MainFactory = new MainFactory;
main.createFactory(makeFactory(0));
main.createFactory(makeFactory(1));
}
public function makeFactory($value:int):IFactory
{
var iFactory:IFactory
if ( $value == 0 ){
iFactory = new FactoryDoor2;
}else{
iFactory = new FactoryDoor4;
}
return iFactory
}
}
}
internal class MainFactory
{
public function MainFactory()
{
}
public function createFactory($IFactory:IFactory):void
{
var car:ICar = $IFactory.create();
car.output();
}
}
internal interface IFactory
{
function create():ICar
}
internal class FactoryDoor2 implements IFactory
{
public function create():ICar
{
return new Car2;
}
}
internal class FactoryDoor4 implements IFactory
{
public function create():ICar
{
return new Car4;
}
}
internal interface ICar
{
function output():void
}
internal class Car implements ICar
{
public function output():void
{
trace("몸채생성");
addDoor();
}
protected function addDoor():void
{
}
}
internal class Car2 extends Car
{
override protected function addDoor():void
{
trace("문 2개 차동차");
}
}
internal class Car4 extends Car
{
override protected function addDoor():void
{
trace("문 4개 자동차");
}
}
-----------------
package
{
import flash.display.Sprite;
//빌더 : 생성패턴
// 객체의 생성 밥법을 정의하는 클래스를 별도로 만들고, 서로 다른 특성을 부여하더라도
// 동일한 생성 절차를 제공
//
//장점
//: 생성 방법 및 특성을 다양하게 줄수있음
//* 제품이 제작되는 방법을 선택해서 완성 : 완성되는 제품은 같지만 옵션이 다름
public class Builder extends Sprite
{
public function Builder()
{
var main:MainBuilder = new MainBuilder;
var car:Car;
var sedanBuilder:IBuilder = new SedanBuilder;
main.addBuilder(sedanBuilder);
main.addPart();
car = main.getCar();
car.printSpec();
var sportsBuilder:IBuilder = new SportsBuilder;
main.addBuilder(sportsBuilder);
main.addPart();
car = main.getCar();
car.printSpec();
super();
}
}
}
internal interface IBuilder
{
function addPart():void
function getCar():Car
}
internal class MainBuilder implements IBuilder
{
private var _iBuilder:IBuilder;
public function MainBuilder()
{
}
public function addBuilder($IBuilder:IBuilder):void
{
_iBuilder = $IBuilder;
}
public function addPart():void
{
_iBuilder.addPart();
}
public function getCar():Car
{
return _iBuilder.getCar();
}
}
internal class CarBuilder implements IBuilder
{
private var _car:Car;
public function CarBuilder()
{
_car = new Car;
}
public function addPart():void
{
}
public function getCar():Car
{
return _car;
}
public function addBody($IPart:IPart):void
{
_car.addBody($IPart);
}
public function addDoor($IPart:IPart):void
{
_car.addDoor($IPart);
}
public function addWhell($IPart:IPart):void
{
_car.addWhell($IPart);
}
}
internal class SedanBuilder extends CarBuilder
{
override public function addPart():void
{
var body:IPart;
var whell:IPart;
var door:IPart;
body = new Body4;
whell = new Wheel;
door = new Door4;
addBody(body);
addWhell(whell);
addDoor(door);
}
}
internal class SportsBuilder extends CarBuilder
{
override public function addPart():void
{
var body:IPart;
var whell:IPart;
var door:IPart;
body = new Body2;
whell = new Wheel;
door = new Door2;
addBody(body);
addWhell(whell);
addDoor(door);
}
}
internal class Car
{
private var _body:IPart;
private var _door:IPart;
private var _whell:IPart;
public function output():void
{
}
public function addBody($IPart:IPart):void
{
_body = $IPart
}
public function addDoor($IPart:IPart):void
{
_door = $IPart;
}
public function addWhell($IPart:IPart):void
{
_whell = $IPart
}
public function printSpec():void
{
trace("================");
trace(_body.getData());
trace(_door.getData());
trace(_whell.getData());
trace("================");
}
}
internal interface IPart
{
function getData():String
}
internal class Part implements IPart
{
protected var str:String;
public function getData():String
{
return str;
}
}
internal class Body4 extends Part
{
public function Body4():void
{
str = "몸체 : 4륜";
}
}
internal class Body2 extends Part
{
public function Body2():void
{
str = "몸체 : 2륜";
}
}
internal class Wheel extends Part
{
public function Wheel():void
{
str = "바퀴";
}
}
internal class Door4 extends Part
{
public function Door4():void
{
str = "문: 4개";
}
}
internal class Door2 extends Part
{
public function Door2():void
{
str = "문: 2개";
}
}
------
package
{
import flash.display.Sprite;
//팩토리메서드 : 생성패턴
// 객체를 생성하는 방법을 미리 정의하여 생성을 담당하는 클래스에 위임
//* 추상팩토리 패턴과 비슷하지만 사용자가 직접 제품을 사용할 필요가 있을때.
public class FactoryMethod extends Sprite
{
public function FactoryMethod()
{
var factory:Factory = new Factory;
var car:Car;
car = factory.create(0);
car.run();
car = factory.create(1);
car.run();
}
}
}
internal interface IFactory
{
function create($value:int):Car
}
internal class Factory implements IFactory
{
public function create($value:int):Car
{
var car:Car
if ( $value == 0 ){
car = new Car2;
}else{
car = new Car4;
}
car.create();
return car
}
}
internal class Car
{
public function create():void
{
throw new Error("create 재정의 필요");
}
public function run():void
{
trace("움직인다");
}
}
internal class Car2 extends Car
{
override public function create():void
{
trace("바퀴 2개 차동차");
}
}
internal class Car4 extends Car
{
override public function create():void
{
trace("바퀴 4개 차동차");
}
override public function run():void
{
trace("바퀴 4개로 움직인다");
}
}
'Program > 디자인패턴' 카테고리의 다른 글
observer 패턴 (0) | 2012.10.05 |
---|---|
FacadeSample (0) | 2012.09.05 |
FlyweightSample (0) | 2012.09.05 |
ProxySample (0) | 2012.09.05 |