1. 내가 설계한 Abstract factory pattern 예제

제목 : kinder초콜릿공장

kinder초콜릿이란 과거에 에그몽 초콜릿처럼 달걀모양 초콜릿 안에 장난감이 들어있는 초콜릿을 말한다. Abstract factory pattern을 이용하여 kinder초콜릿 제조공장 프로그램을 만들었다. kinder초콜릿의 종류는 파랑색포장지를 한 blueKinder와 빨강색 포장지를 한 redKinder 2가지이다. 파랑색과 빨강색에는 각각 다른 장난감이 들어가 있다.

언어는 자바언어를 사용했고 에디터는 이클립스를 사용했다. 총 클래스는 11가지이다.


1) main클래스

main클래스는 프로그램이 처음 시작하는 클래스이다. get_kinder_type함수를 사용하여 kinder의 종류를 선택하여 거기에 맞는 foodFactory객체를 생성한다. 그리고 running함수를 이용하여 공장을 가동한다.

public class main {

public static void main(String args[]) {

foodFactory foodfactory;

Kinder kinder = new Kinder();

if(kinder.get_kinder_type().equals("blue")){

foodfactory = new blueKinderFactory();

kinder.running(foodfactory);;

}

else if(kinder.get_kinder_type().equals("red")){

foodfactory = new redKinderFactory();

kinder.running(foodfactory);;

}

else {

System.out.println("error \n 공장 운행을 중단합니다.");

}

}

}

 

2) kinder클래스

kinder클래스는 kinder종류 선택과 foodFactory클래스 객체를 넘겨받아 running함수를 통해 공장을 가동한다.

import java.util.Scanner;

 

public class Kinder {

String _kinder_type;

public Kinder() {

Scanner sc = new Scanner(System.in);

System.out.println("어떤 킨더초콜릿을 생산하시겠습니까?(blue, red)");

this._kinder_type = sc.nextLine();

System.out.println(_kinder_type+" Kinder가 선택되었습니다.");

}

public void running(foodFactory foodfactory) {

foodfactory.return_toy_object();

foodfactory.return_label_object();

System.out.println("모든 작업이 완료되었으므로 공장을 종료합니다.");

}

public String get_kinder_type() {

return _kinder_type;

}

}

 

3) foodFactory클래스

생성자를 통해서 공장가동을 알리고 toy객체와 label객체를 반환한다.

public abstract class foodFactory {

 

public foodFactory() {

System.out.println("공장을 가동합니다.");

}

public abstract Toy return_toy_object();

public abstract Label return_label_object();

}

 

4) redKinderFactory클래스

foodFactory객체를 상속받아 toyPrincess객체와 labelredLabel객체를 반환한다.

public class redKinderFactory extends foodFactory {

public Toy return_toy_object() {

Princess princess = new Princess();

princess.production();

return princess;

}

public Label return_label_object() {

redLabel redlabel = new redLabel();

redlabel.packing();

return redlabel;

}

}

 

 

 

 

5) bleKinderFactory클래스

foodFactory객체를 상속받아 toySoldier객체와 labelbleLabel객체를 반환한다.

public class blueKinderFactory extends foodFactory{

 

public Toy return_toy_object() {

Soldier soldier = new Soldier();

soldier.production();

return soldier;

}

 

public Label return_label_object() {

blueLabel bluelabel = new blueLabel();

bluelabel.packing();

return bluelabel;

}

}

 

6) Toy클래스

생성자를 통해 장난감 생산을 알리고 prodctuion함수를 이용하여 장난감을 생산한다.

public abstract class Toy {

public Toy() {

System.out.println("장난감 생산 준비완료");

}

public abstract void production();

}

 

7) Soldier클래스

Toy클래스를 상속받고 생성자를 통해 soldier장난감 생산을 알리고 production함수를 통해서 장난감을 생산한다.

public class Soldier extends Toy{

public Soldier() {

System.out.println("군인 장난감을 생산합니다.");

}

public void production() {

System.out.println("군인 장난감이 생산되었습니다.");

}

}

 

8) Princess클래스

Toy클래스를 상속받고 생성자를 통해 Princess장난감 생산을 알리고 production함수를 통해서 장난감을 생산한다.

public class Princess extends Toy{

 

public Princess() {

System.out.println("공주 장난감을 생산합니다.");

}

public void production() {

System.out.println("공주장난감이 생산되었습니다.");

}

}

 

9) Label클래스

생성자를 통해서 포장지 생산준비를 알리고 packing함수를 통해서 포장한다.

public abstract class Label {

public Label() {

System.out.println("포장지 준비중");

}

public abstract void packing();

}

 

10) blueLabel클래스

생성자를 통해서 파란 포장지 생산을 알리고 packing함수를 통해서 포장지로 포장한다.

public class blueLabel extends Label{

 

public blueLabel() {

System.out.println("파랑 포장지 준비완료");

}

public void packing() {

System.out.println("kinder를 파란색으로 포장합니다.");

}

}

 

10) redLabel클래스

생성자를 통해서 빨간 포장지 생산을 알리고 packing함수를 통해서 포장지로 포장한다.

public class redLabel extends Label{

public redLabel() {

System.out.println("빨강 포장지 준비완료");

}

public void packing() {

System.out.println("kinder를 빨강색으로 포장합니다.");

}

}


2. 클래스다이어그램


3. 코드 실행 결과


'학교공부' 카테고리의 다른 글

이해하기쉬운 template method pattern 예제  (0) 2018.12.03
텔넷  (0) 2018.12.03
이해하기쉬운 command pattern 예제  (0) 2018.11.25
이해하기쉬운 bridge pattern 예제  (0) 2018.11.23
쉽게설명한 전자상거래 유형  (0) 2018.11.22

+ Recent posts