Design patterns 06.

Creational patterns - Prototype

by Dichter
원형(Prototype)이란?

우선 에릭 감마를 비롯한 4인방이 소개하는 원형의 의도, 동기 그리고 활용을 읽어보자. 어렵지 않은 단어와 문장이니, 의미를 보며 정독해보자.


Intent

Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.


Motivation

You could build an editor for music scores by customizing a general framework for graphical editors and adding new objects that represent notes, rests, and staves. The editor framework may have a palette of tools for adding these music objects to the score. The palette would also include tools for selecting, moving, and otherwise manipulating music objects.


Let's assume the framework provides an abstract Graphic class for graphical components, like notes and staves. Moreover, it'll provide an abstract Tool class for defining tools like those in the palette. The framework also predefines a GraphicTool subclass for tools that create instances of graphical objects and add them to the document.


But GraphicTool presents a problem to the framework designers. The classes for notes and staves are specific to our application, but the GraphicTool class belongs to the framework. GraphicTool doesn't know how to create instances of our music classes to add to the score. We could subclass GraphicTool for each kind of music objects, but that would produce lots of subclasses that differ only in the kind of music object they instantiate. We know object composition is a flexible alternative to subclassing. The question is, how can the framework use it to parameterize instances of GraphicTool by the class of Graphic they're supposed to create?


The solution lies in making GraphicTool create a new Graphic by copying or "cloning" an instance of a Graphic subclass. We call this instance a prototype. If all Graphic subclasses support a Clone operation, then the GraphicTool can clone any kind of Graphic.


We can use the Prototype pattern to reduce the number of classes even further. We have separate classes for whole notes nad half notes, but that's probably unnecessary. Instead they could be instance of the same class initialized with different bitmaps and durations.


Applicability

Use the Prototype pattern when a system should be independent of how its products are created, composed, and represented; and

when the classes to instantiate are specified at run-time, for example, by dynamic loading; or

to avoid building a class hierarchy of factories that parallels the class hierarchy of products; or

when instances of a class can have one of only a few different combinations of state. It may be more conventient to install a corresponding number of prototypes and clone them rather than instantiating the class manually, each time with the appropriate state.


원형(Prototype)은 객체의 복사(Clone)를 이용한 패턴이다. 복사는 직접 생성하는 비용 대비 더 효율적인 장정이 있다. 위 예시에서는 악보를 만드는 데에 사용 가능한 프레임워크를 가정한다. 악보의 특성상 반복되는 객체가 많다. 음표 하나하나를 객체라고 생각하고, 그 객체의 음 높낮이와 길이 정도로 차이를 둔다면 중복된 음표가 수백 개가 될 수 있다. 이러한 음표 객체를 생성하기 위해 매번 그래픽 생성 팩토리로부터 객체를 인스턴스화 하지 않고, 이미 만들어진 원형을 복사하여 악보에 그려주는 것이 편리할 것이다.


하지만 모든 악보를 구성하는 클래스들에 복사 메서드를 구현하는 것은 추가적인 개발 비용을 야기할 수 있음으로 주의하여야 한다.


원형 예제

작성 중...


파올로 베로네세카나의 결혼
카나의 결혼 잔치는 기독교에서 물을 포도주로 바꾼 요한 복음서에 나오는 최초의 예수의 기적 이야기이다. 성경에 따르면 예수와 그의 제자들은 결혼 잔치에 초대받았는데, 포도주가 떨어지자 예수는 물을 포도주로 바꾸는 기적을 일으켰다.


1쇄. 2020.12.13.

keyword
작가의 이전글Design patterns 05.