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.
์ด ํจํด๋ค์ ๊ฐ์ฒด๋ค ์ฌ์ด์ ์ปค๋ฎค๋์ผ์ด์ ์ ๊ด์ฌ์ ๊ฐ์ง๋ค.
์ค๋ ์ดํด๋ณผ Decorator ํจํด์ Structural ํจํด์ ์ํ๋ค. ์ด ํจํด์ ์ปดํฌ์งํธ ํจํด๊ณผ ๋น์ทํ ๋ค์ด์ด๊ทธ๋จ์ ๊ฐ์ง๊ณ ์๋ค.
Decorator Pattern Structure
ํจํด์ ๋ชฉ์ ( Intent ) :
Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
๋์ ์ผ๋ก ๊ฐ์ฒด์ ์ฑ ์(๊พธ๋ฐ)์ ์ถ๊ฐํ๋ค. ๋ฐ์ฝ๋ ์ดํฐ๋ ๊ธฐ๋ฅ์ ํ์ฅํ๊ธฐ ์ํ subclassing์ด ์๋ ๋ค๋ฅธ ์ ๋์ ์ธ ๋ฐฉ๋ฒ์ ์ ๊ณตํ๋ค.
ํจํด์ด ๋์ค๊ฒ ๋ ๋๊ธฐ ( Motivation ) :
์ผ๋ฐ์ ์ผ๋ก ๊ฐ๋จํ ํ๋ก์ ํธ๊ฐ ์๋ ์ด์ ํ๋์ ํด๋์ค ๋ด์์ ๋ชจ๋ ๊ฒ์ ํด๊ฒฐํ๋ ์ผ์ ๊ฑฐ์ ์๋ค. ์ ์ง๋ณด์๋ฅผ ์ํด์๋ผ๋ ๊ธฐ๋ฅ๋ณ๋ก
ํด๋์ค๋ฅผ ๋๋๊ฒ ๋๋๋ฐ ๋ฐ์ฝ๋ ์ดํฐ ํจํด๋ ์ด๋ฐ ์์ ์๊ฐ์์ ๋์ค๊ฒ ๋ ๊ฒ์ด๋ค. ๊ทธ๋์ ์์์ ์ฌ์ฉํ๊ฒ ๋๊ธฐ๋ ํ๋๋ฐ ์ด๋ ๋งค์ฐ
๊ธฐ๋ณธ์ ์ธ ๋ฐฉ๋ฒ์ด๋ฉฐ flexibleํ์ง ์๊ณ ์ ์ ์ด๋ค. ๋ฐ๋ผ์ ๋์ ์ผ๋ก ๊ธฐ๋ฅํ์ฅ์ด ์ฉ์ดํ๋๋ก ํ ๊ฒ์ด ๋ฐ๋ก ์ด ๋ฐ์ฝ๋ ์ดํฐ ํจํด์ด๋ค.
์๋ ๊ทธ๋ฆผ์ TextView์ ScrollDecorator์ BorderDecorator๋ฅผ ์ด์ฉํ์ฌ ์ฑ
์(๊พธ๋ฐ)์ ์ถ๊ฐํ
๊ฒฐ๊ณผ์ด๋ค.
์ ์ฉ์ฑ ( Applicability ) :
Use Decorator
๋ฐ์ฝ๋ ์ดํฐ ํจํด์ ์๋์ ๊ฐ์ ์ํฉ์์ ์ ์ฉํ๋ค.
ยท to add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects.
๋ค๋ฅธ ๊ฐ์ฒด์ ์ํฅ์ ์ฃผ์ง ์๊ณ ๋์ ์ผ๋ก ํฌ๋ช
ํ๊ฒ ๋
๋ฆฝ์ ์ธ ๊ฐ์ฒด๋ค์ ์ถ๊ฐํ ๋
ยท for responsibilities that can be withdrawn.
์ฒ ํ๊ฐ ๊ฐ๋ฅํ ์ฑ
์(๊พธ๋ฐ)์ ์ถ๊ฐํ๊ณ ์ถ์ ๋
ยท
when extension by subclassing is impractical. Sometimes a large number
of independent extensions are possible and would produce an explosion of
subclasses to support every combination. Or a class definition may be
hidden or otherwise unavailable for subclassing.
๋ฑ์ฅ ์ธ๋ฌผ ( Participants ) :
ยท Component (VisualComponent)
o defines the interface for objects that can have responsibilities added to them dynamically.
๋์ ์ผ๋ก ์ถ๊ฐ๋๋ ์ฑ
์์ ๊ฐ์ง ์ ์๋ ๊ฐ์ฒด๋ฅผ ์ํ ์ธํฐํ์ด์ค๋ฅผ ์ ์ํ๋ค.
ยท ConcreteComponent (TextView)
o defines an object to which additional responsibilities can be attached.
์ถ๊ฐ์ ์ธ ์ฑ
์์ ๊ฐ์ง ์ ์๋ ๊ฐ์ฒด๋ฅผ ์ ์ํ๋ค.
ยท Decorator
o maintains a reference to a Component object and defines an interface that conforms to Component's interface.
์ด ํจํด์ ๋ฉ์ธ์ด๋ค. ์ปดํฌ๋ํธ ๊ฐ์ฒด์ ๋ ํผ๋ฐ์ค๋ฅผ ๊ฐ์ง๊ณ ์์ผ๋ฉฐ, ์ปดํฌ๋ํธ์ ์กฐํ๋ฅผ ์ด๋ฃจ๋ ์ธํฐํ์ด์ค๋ฅผ ์ ์ํ๋ค.
ยท ConcreteDecorator (BorderDecorator, ScrollDecorator)
o adds responsibilities to the component.
์ปดํฌ๋ํธ์ ์ฑ ์์ ๋ถ๊ณผํ๋ค.
์๋ฆฌ ( Collaborations ) :
ํจํด ์ฌ์ฉ์ ์ฅ๋จ์ ( Consequences ):
The Decorator pattern has at least two key benefits and two liabilities:
1. More flexibility than static inheritance. ์ ์ ์ธ ์์๋ณด๋ค ํจ์ฌ ์ ๋์ ์ด๋ค. The
Decorator pattern provides a more flexible way to add responsibilities
to objects than can be had with static (multiple) inheritance. With
decorators, responsibilities can be added and removed at run-time simply
by attaching and detaching them. In contrast, inheritance requires
creating a new class for each additional responsibility (e.g.,
BorderedScrollableTextView, BorderedTextView). This gives rise to many
classes and increases the complexity of a system.
Furthermore, providing different Decorator classes for a specific Component class lets you mix and match responsibilities.
Decorators
also make it easy to add a property twice. For example, to give a
TextView a double border, simply attach two BorderDecorators. Inheriting
from a Border class twice is error-prone at best.
2. Avoids feature-laden classes high up in the hierarchy.
Decorator offers a pay-as-you-go approach to adding responsibilities.
Instead of trying to support all foreseeable features in a complex,
customizable class, you can define a simple class and add functionality
incrementally with Decorator objects. Functionality can be composed from
simple pieces. As a result, an application needn't pay for features it
doesn't use. It's also easy to define new kinds of Decorators
independently from the classes of objects
they extend, even for
unforeseen extensions. Extending a complex class tends to expose details
unrelated to the responsibilities you're adding.
3. A decorator and its component aren't identical.
A decorator acts as a transparent enclosure. But from an object
identity point of view, a decorated component is not identical to the
component itself. Hence you shouldn't rely on object identity when you
use decorators.
4. Lots of little objects. A design
that uses Decorator often results in systems composed of lots of little
objects that all look alike. The objects differ only in the way they are
interconnected, not in their class or in the value of their variables.
Although these systems are easy to customize by those who understand
them, they can be hard to learn and debug.
๊ด๋ จ ํจํด๋ค :
Adapter : A decorator is different from an adapter in that a decorator only changes an object's responsibilities, not its interface; an adapter will give an object a completely new interface.
Composite
: A decorator can be viewed as a degenerate composite with only one
component. However, a decorator adds additional responsibilitiesโit
isn't intended for object aggregation.
Strategy : A decorator
lets you change the skin of an object; a strategy lets you change the
guts. These are two alternative ways of changing an object.
์ถ๊ฐ ์ ๋ณด :
- Adapter provides a different interface to its subject. Proxy provides the same interface. Decorator provides an enhanced interface.
- Adapter changes an object's interface, Decorator enhances an object's responsibilities. Decorator is thus more transparent to the client. As a consequence, Decorator supports recursive composition, which isn't possible with pure Adapters.
- Composite and Decorator have similar structure diagrams, reflecting the fact that both rely on recursive composition to organize an open-ended number of objects.
- A Decorator can be viewed as a degenerate Composite with only one component. However, a Decorator adds additional responsibilities - it isn't intended for object aggregation.
- Decorator is designed to let you add responsibilities to objects without subclassing. Composite's focus is not on embellishment but on representation. These intents are distinct but complementary. Consequently, Composite and Decorator are often used in concert.
- Composite could use Chain of Responsibility to let components access global properties through their parent. It could also use Decorator to override these properties on parts of the composition.
- Decorator and Proxy have different purposes but similar structures. Both describe how to provide a level of indirection to another object, and the implementations keep a reference to the object to which they forward requests.
- Decorator lets you change the skin of an object. Strategy lets you
change the guts.
- Decorator pattern is helpful in providing runtime modification abilities and hence more flexible. Its easy to maintain and extend when the number of choices are more.
- The disadvantage of decorator pattern is that it uses a lot of similar kind of objects (decorators).
๋ฐ์ฝ๋ ์ดํฐ ํจํด์ ๋จ์ ์ ์ ์ฌํ ๋ฐ์ฝ๋ ์ดํฐ ๊ฐ์ฒด๊ฐ ๋ง์์ง ์ ์๋ค๋ ์ ์ด๋ค. - Decorator pattern is used a lot in Java IO classes, such as FileReader, BufferedReader etc.
๋ฐ์ฝ๋ ์ดํฐ ํจํด์ FileReader๋ BufferedReader์ ๊ฐ์ Java IO ํด๋์ค๋ค์์ ๋ง์ด ์ฌ์ฉ๋๊ณ ์๋ค.
์ถ์ฒ : http://sourcemaking.com/design_patterns/decorator
Design Patterns : Element of Reusable Object Oriented Software ( by GoF, 1994 )
http://www.journaldev.com/1540/decorator-pattern-in-java-example-tutorial
์ค์ํ์ ์๋ก ๋ ์ข์ ์์ ๋ ์๋์์ ํ์ธํ๊ธฐ ๋ฐ๋๋ค.
'๐ป Programming > GoF Design Pattern' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋์์ธํจํด] ๋ธ๋ฆฌ์ง ํจํด ( Bridge Pattern ) (0) | 2015.11.03 |
---|---|
[๋์์ธํจํด] ํ๋ก์ ํจํด ( Proxy Pattern ) (0) | 2015.11.03 |
[๋์์ธํจํด] ์ด๋ํฐ ํจํด ( Adapter Pattern ) (0) | 2015.11.03 |
[๋์์ธํจํด] ์ฑ ์์ ๊ฐ ํจํด ( Chain of Responsibility Pattern ) (0) | 2015.11.03 |
[๋์์ธํจํด] ๋น์งํฐ(๋ฐฉ๋ฌธ์) ํจํด ( Visitor Pattern ) (0) | 2015.11.03 |