Creational Patterns ( 생성 패턴 )
These design patterns provides way to create objects while hiding the creation logic, rather than instantiating objects directly using new operator. This gives program more flexibility in deciding which objects need to be created for a given use case.
생성패턴은 객체의 생성로직을 숨기고 new 명령어를 통하지 않고 객체를 생성하는 방법들을 제공한다. 이는 특정 상황에서 어떤 방법으로 객체를 생성해야할지를 결정하는데 있어서 유연성을 제공한다.
Structural Patterns ( 구조적 패턴 )
These design patterns concern class and object composition. Concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionalities.
구조적 패턴들은 클래스와 객체의 구성에 관여한다.
Behavioral Patterns ( 행위적 패턴 )
These design patterns are specifically concerned with communication between objects.
이 패턴들은 객체들 사이의 커뮤니케이션에 관심을 가진다.
오늘 살펴볼 Prototype 패턴은 Creational 패턴에 속한다.
Prototype Pattern Structure
패턴의 목적 ( Intent ) :
- Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
하나의 프로토타입 인스턴스를 만들고 필요할때마다 프로토타입 인스턴스를 복사해서 객체를 생성하기 위함
- Co-opt one instance of a class for use as a breeder of all future instances.
- The new
operator considered harmful.
new 키워드를 이용하는 것이 안좋은 상황일 때 프로토타입 패턴을 이용하자.
패턴이 나오게 된 동기 ( Motivation ) :
객체 생성 비용이 비싼경우 프로토타입 원형을 만들고 클로닝 기법을 이용해서 객체를 쉽게 생성하고자 하기 위함.
유용성 ( 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 convenient to install a
corresponding number of prototypes and clone them rather than
instantiating the class manually, each time with the appropriate state.
등장 인물 ( Participants ) :
· Prototype
o declares an interface for cloning itself.
· ConcretePrototype
o implements an operation for cloning itself.
· Client
o creates a new object by asking a prototype to clone itself.
원리 ( Collaborations ) :
패턴 사용의 장단점 ( Consequences ):
Prototype has many of the same consequences that Abstract Factory and Builder have: It hides the concrete product classes from the client, thereby reducing the number of names clients know about. Moreover, these patterns let a client work with application-specific classes without modification.
Additional benefits of the Prototype pattern are listed below.
1. Adding and removing products at run-time.
Prototypes let you incorporate a new concrete product class into a
system simply by registering a prototypical instance with the client.
That's a bit more flexible than other creational patterns, because a
client can install and remove prototypes at run-time.
2. Specifying new objects by varying values.
Highly dynamic systems let you define new behavior through object
composition—by specifying values for an object's variables, for
example—and not by defining new classes. You effectively define new
kinds of objects by instantiating existing classes and registering the
instances as prototypes of client objects. A client can exhibit new
behavior by delegating responsibility to the prototype. This kind of
design lets users define new "classes" without programming. In fact,
cloning a prototype is similar to instantiating a class. The Prototype
pattern can greatly reduce the number of classes a system needs. In our
music editor, one GraphicTool class can create a limitless variety of
music objects.
3. Specifying new objects by varying structure.
Many applications build objects from parts and subparts. Editors for
circuit design, for example, build circuits out of subcircuits.1 For
convenience, such applications often let you instantiate complex,
user-defined structures, say, to use a specific subcircuit again and
again. The Prototype pattern supports this as well. We simply add this
subcircuit as a prototype to the palette of available circuit elements.
As long as the composite circuit object implements Clone as a deep copy,
circuits with different structures can be prototypes.
4. Reduced subclassing. Factory Method often produces a hierarchy of Creator classes that parallels the product class hierarchy. The Prototype pattern lets you clone a prototype instead of asking a factory method to make a new object. Hence you don't need a Creator class hierarchy at all. This benefit applies primarily to languages like C++ that don't treat classes as first-class objects. Languages that do, like Smalltalk and Objective C, derive less benefit, since you can always use a class object as a creator. Class objects already act like prototypes in these languages.
5. Configuring an application with classes dynamically.
Some run-time environments let you load classes into an application
dynamically. The Prototype pattern is the key to exploiting such
facilities in a language like C++. An application that wants to create
instances of a dynamically loaded class won't be able to reference its
constructor statically. Instead, the run-time environment creates an
instance of each class automatically when it's loaded, and it registers
the instance with a prototype manager (see the
Implementation
section). Then the application can ask the prototype manager for
instances of newly loaded classes, classes that weren't linked with the
program originally. The ET++ application framework [WGM88] has a
run-time system that uses this scheme.
The main liability of the
Prototype pattern is that each subclass of Prototype must implement the
Clone operation, which may be difficult. For example, adding Clone is
difficult when the classes under consideration already exist.
Implementing Clone can be difficult when their internals include objects
that don't support copying or have circular references.
관련 패턴들 :
Prototype and Abstract Factory
are competing patterns in some ways, as we discuss at the end of this
chapter. They can also be used together, however. An Abstract Factory
might store a set of prototypes from which to clone and return product
objects.
Designs that make heavy use of the Composite and Decorator patterns often can benefit from Prototype as well.
추가 정보 :
출처 : http://sourcemaking.com/design_patterns/prototype
Design Patterns : Element of Reusable Object Oriented Software ( by GoF, 1994 )
[디자인패턴] 플라이웨이트 패턴 ( Flyweight Pattern ) (0) | 2015.11.03 |
---|---|
[디자인패턴] 싱글턴 패턴 ( Singleton Pattern ) (0) | 2015.11.03 |
[디자인패턴] 팩토리 메소드 패턴 ( Factory Method Pattern ) (2) | 2015.11.03 |
[디자인패턴] 추상 팩토리 패턴 ( Abstract Factory Pattern ) (0) | 2015.11.03 |
[디자인패턴] 템플릿 메소드 패턴 ( Template Method Pattern ) (0) | 2015.11.03 |