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.

์ด ํŒจํ„ด๋“ค์€ ๊ฐ์ฒด๋“ค ์‚ฌ์ด์˜ ์ปค๋ฎค๋‹ˆ์ผ€์ด์…˜์— ๊ด€์‹ฌ์„ ๊ฐ€์ง„๋‹ค.

 

์˜ค๋Š˜ ์‚ดํŽด๋ณผ  Proxy ํŒจํ„ด์€ Structural ํŒจํ„ด์— ์†ํ•œ๋‹ค.  

ํ”„ ๋ก์‹œ ํŒจํ„ด์€ ๋„คํŠธ์›Œํ‚น์„ ๊ณต๋ถ€ํ•ด๋ณธ ์‚ฌ๋žŒ์ด๋ผ๋ฉด ์ดํ•ดํ•˜๊ธฐ๊ฐ€ ์‰ฌ์šธ ๊ฒƒ์ด๋‹ค. ํ”„๋ก์‹œ๊ฐ€ ๋ญ”์ง€๋ฅผ ์•Œํ…Œ๋‹ˆ๊นŒ ๋ง์ด๋‹ค. ์—ฌ๊ธฐ์„œ ๋งํ•˜๋Š” ํ”„๋ก์‹œ๋ž€ ํ”„๋ก์‹œ ์„œ๋ฒ„์—์„œ ๋งํ•˜๋Š” ๊ทธ๊ฒƒ๊ณผ ๋™์ผํ•œ ๊ฐœ๋…์ด๋‹ค. ์ผ์ข…์˜ ์ค‘๊ฐ„์—ฐ๊ฒฐ์ž ์—ญํ• ์„ ํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์„œ, ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด ์ตœ์ดˆ ์š”์ฒญ์ผ ๊ฒฝ์šฐ ์ƒˆ๋กœ ์ƒ์„ฑํ•˜๊ณ  ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋ฅผ ์‘๋‹ต์œผ๋กœ ๋ณด๋‚ด๊ณ , ๋‘๋ฒˆ์งธ ์š”์ฒญ๋ถ€ํ„ฐ๋Š” ๊ธฐ์กด์— ์ƒ์„ฑํ–ˆ๋˜ ๊ฒƒ์„ ์‘๋‹ต์œผ๋กœ ๋ณด๋‚ด๋Š” ๊ฒƒ์ด๋‹ค.

์ดํ•ด๊ฐ€ ์ž˜ ์•ˆ๊ฐ„๋‹ค๋ฉด ์ข€ ๋” ์ž์„ธํžˆ ์•Œ์•„๋ณด์ž. 

 

Proxy Pattern Structure

 


์šฐ์„  ์ด ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ๋ถ€ํ„ฐ ์ดํ•ด๋ฅผ ํ•ด๋ณด๋„๋ก ํ•˜์ž. ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๊ฒƒ์€ RealSubject์™€ Proxy ๊ทธ๋ฆฌ๊ณ  Subject์‚ฌ์ด์˜ ๊ด€๊ณ„์ด๋‹ค. ํด๋ผ์ด์–ธํŠธ๋Š” Subject๊ฐ์ฒด์˜ Request()๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  Subject๊ฐ์ฒด๋ฅผ ๋ฐ›๋Š”๋‹ค. RealSubject์™€  Proxy๋Š” Subject๋ฅผ ์ƒ์†ํ•˜๋Š” ํด๋ž˜์Šค๋“ค์ธ๋ฐ Proxyํด๋ž˜์Šค์˜ Request()๋Š” ๊ฒฝ์šฐ์— ๋”ฐ๋ผ RealSubject์˜ Request()๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. RealSubject์˜ Request()์—์„œ๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์›ํ•˜๋Š” Subject๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.  

๋”ฐ๋ผ์„œ runtime์—๋Š” ํ˜ธ์ถœ ์ˆœ์„œ๊ฐ€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. Proxy๋Š” RealSubject๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๋ฐ ์ด๊ฒƒ์ด null์ด ์•„๋‹Œ ๊ฒฝ์šฐ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•˜๊ณ  null์ธ๊ฒฝ์šฐ RealSubject์˜ Request()๋ฅผ ์š”์ฒญํ•˜์—ฌ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ทธ๊ฒƒ์„ Proxy ๋‚ด๋ถ€์— ๊ฐ€์ง€๊ณ  ์žˆ๊ฒŒ ๋œ๋‹ค.



ํŒจํ„ด์˜ ๋ชฉ์  ( Intent ) :  


Provide a surrogate or placeholder for another object to control access to it.
ํ”„๋ก์‹œ ํŒจํ„ด์˜ ๋ชฉ์ ์€ ๋˜๋‹ค๋ฅธ ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ ‘๊ทผ์„ ์ปจํŠธ๋กคํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ทธ ๊ฐ์ฒด๋ฅผ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๊ทธ๋ฆ‡์„ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.


ํŒจํ„ด์ด ๋‚˜์˜ค๊ฒŒ ๋œ ๋™๊ธฐ ( Motivation ) :

 

์›Œ ๋“œํ”„๋กœ๊ทธ๋žจ์—์„œ ์‚ฌ์ง„ํŒŒ์ผ์„ ๋„ฃ๊ฒŒ๋˜๋ฉด ๋กœ๋”ฉ์ด ๋Š๋ ค์ง€๋Š” ํ˜„์ƒ์ด ๋ฐœ์ƒํ•  ๋•Œ๊ฐ€ ์žˆ์—ˆ๋‹ค. ์ง€๊ธˆ์ด์•ผ ์›Œ๋‚™ ์ปดํ“จํ„ฐ ์‚ฌ์–‘์ด ์ข‹์•„์ ธ์„œ ๊ทธ๋Ÿฐ์ผ์„ ๋งŽ์ด ๋ณด์ง€๋Š” ๋ชปํ•˜์ง€๋งŒ ์‚ฌ์ง„ํŒŒ์ผ ์‚ฌ์ด์ฆˆ์—ญ์‹œ ์ข‹์€ ํ™”์งˆ์ธ ๊ฒฝ์šฐ ์ƒ๋‹นํ•œ ์‚ฌ์ด์ฆˆ๋ฅผ ์ž๋ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฌด์‹œํ•  ์ˆ˜ ์—†๋‹ค. ์ด๋Ÿฐ ๊ฒฝ์šฐ ํŽ˜์ด์ง€๋ฅผ ์ด๋™ํ•  ๋•Œ๋งˆ๋‹ค ๋””์Šคํฌ์— ์žˆ๋Š” ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ๊ณ„์† ์š”์ฒญํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ํ•œ๋ฒˆ๋งŒ ์š”์ฒญํ•˜์—ฌ ํ”„๋ก์‹œ์— ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ฐ€ ๋‹ค์‹œ ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด ํ”„๋ก์‹œ์—์„œ ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€์ ธ์™€์„œ ๋””์Šคํ”Œ๋ ˆ์ด ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์ด ํ•„์š”ํ•˜๊ฒŒ ๋œ ๊ฒƒ์ด๋‹ค. ์ด๋Ÿฐ์‹์œผ๋กœ ์†๋„์˜ ํ–ฅ์ƒ์„ ๊ฟฐํ•œ๊ฒƒ์ด๋‹ค. ์•„๋ž˜ ๊ทธ๋ฆผ์€ ๋””์Šคํฌ์— ์žˆ๋Š” ์ด๋ฏธ์ง€ ํŒŒ์ผ์—์„œ ๋ฐ์ดํƒ€๋ฅผ ์ฝ์–ด์™€์„œ ๋ฉ”๋ชจ๋ฆฌ์— ์ƒ์ฃผํ•˜๋Š” ํ”„๋ก์‹œ์— ๋„ฃ๊ณ  ๋ฌธ์„œ์—์„œ ์ด์— ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์„ ๋ณด์—ฌ์ฃผ๊ณ ์žˆ๋‹ค.

 

์œ„์—์„œ ๋ดค๋˜ ํด๋ž˜์Šค ๋‹ค์ด์–ด๊ทธ๋žจ์— ๋ฌธ์„œํ”„๋กœ๊ทธ๋žจ์˜ ์˜ˆ๋ฅผ ์ ์šฉ์‹œ์ผœ ๋ณธ ๊ฒƒ์ด๋‹ค. ImageProxy์˜ ์šฐ์ธก์„ ๋ณด๋ฉด image๊ฐ€ 0์ธ์ง€๋ฅผ ํŒ๋ณ„ํ•˜๊ณ  0์ผ๊ฒฝ์šฐ Image๋ฅผ ๊ฐ€์ ธ์™€์„œ ๋ณด์—ฌ์ค€๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๋ ค์ฃผ๋Š” ๊ฐ„๋žตํ•œ ์†Œ์Šค๊ฐ€ ๋ณด์ธ๋‹ค.

 

 

์œ ์šฉ์„ฑ ( Applicability ) :

 

Proxy is applicable whenever there is a need for a more versatile or sophisticated reference to an object than a simple pointer. Here are several common situations in which the Proxy pattern is applicable:  

ํ”„๋ก์‹œ ํŒจํ„ด์€ ์•„๋ž˜์™€ ๊ฐ™์€ ์ƒํ™ฉ์—์„œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.


1. A remote proxy provides a local representative for an object in a different address space. NEXTSTEP [Add94] uses the class NXProxy for this purpose. Coplien [Cop92] calls this kind of proxy an "Ambassador."


2. A virtual proxy creates expensive objects on demand. The ImageProxy described in the Motivation is an example of such a proxy.


3. A protection proxy controls access to the original object. Protection proxies are useful when objects should have different access rights. For example, KernelProxies in the Choices operating system [CIRM93] provide protected access to operating system objects.


4. A smart reference is a replacement for a bare pointer that performs additional actions when an object is accessed. Typical uses include

o counting the number of references to the realobject so that it can be freed automatically when there are no more references (also called smart pointers [Ede92]).

o loading a persistent object into memory when it's first referenced.  

o checking that the realobject is locked before it's accessed to ensure that no other object can change it.

 


๋“ฑ์žฅ ์ธ๋ฌผ ( Participants ) :

โ€ข Proxy (ImageProxy) 

o maintainsareferencethatletstheproxyaccesstherealsubject.

Proxy may refer to a Subject if the RealSubject and Subject interfaces are the same.

o provides an interface identical to Subject's so that a proxy can by substituted for the real subject.

o controls access to the real subject and may be responsible for creating and deleting it.

o other responsibilities depend on the kind of proxy:

รŸ remote proxies are responsible for encoding a request and its arguments and for sending the encoded request to the real

subject in a different address space.
รŸ virtual proxies may cache additional information about the real subject so that they can postpone accessing it. For example, the ImageProxy from the Motivation caches the real image's extent.

รŸ protection proxies check that the caller has the access permissions required to perform a request.

โ€ข Subject (Graphic)

o defines the common interface for RealSubject and Proxy so that a Proxy can be used anywhere a RealSubject is expected.

โ€ข RealSubject (Image)

o defines the real object that the proxy represents.


์›๋ฆฌ ( Collaborations ) :

โ€ข Proxy forwards requests to RealSubject when appropriate, depending on the kind of proxy.


ํŒจํ„ด ์‚ฌ์šฉ์˜ ์žฅ๋‹จ์  ( Consequences ): 

 

The Proxy pattern introduces a level of indirection when accessing an object. The additional indirection has many uses, depending on the kind of proxy:

  1. A remote proxy can hide the fact that an object resides in a different address space.

  2. A virtual proxy can perform optimizations such as creating an object on demand.

  3. Both protection proxies and smart references allow additional house keeping tasks when an object is accessed.

There's another optimization that the Proxy pattern can hide from the client. It's called copy-on-write, and it's related to creation on demand. Copying a large and complicated object can be an expensive operation. If the copy is never modified, then there's no need to incur this cost. By using a proxy to postpone the copying process, we ensure that we pay the price of copying the object only if it's modified.

 

To make copy-on-write work, the subject must be reference counted. Copying the proxy will do nothing more than increment this reference count. Only when the client requests an operation that modifies the subject does the proxy actually copy it. In that case the proxy must also decrement the subject's reference count. When the reference count goes to zero, the subject gets deleted.

Copy-on-write can reduce the cost of copying heavyweight subjects significantly.

 

๊ด€๋ จ ํŒจํ„ด๋“ค : 

 

Adapter : An adapter provides a different interface to the object it adapts. In contrast, a proxy provides the same interface as its subject. However, a proxy used for access protection might refuse to perform an operation that the subject will perform, so its interface may be effectively a subset of the subject's.

Decorator : Although decorators can have similar implementations as proxies, decorators have a different purpose. A decorator adds one or more responsibilities to an object, whereas a proxy controls access to an object.

 

Proxies vary in the degree to which they are implemented like a decorator. A protection proxy might be implemented exactly like a decorator. On the other hand, a remote proxy will not contain a direct reference to its real subject but only an indirect reference, such as "host ID and local address on host." A virtual proxy will start off with an indirect reference such as a file name but will eventually obtain and use a direct reference.

 

 

์ถ”๊ฐ€ ์ •๋ณด :   

  • Adapter provides a different interface to its subject. Proxy provides the same interface. Decorator provides an enhanced interface.
  • 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.

 

 

ํ”„๋ก์‹œ ํŒจํ„ด์„ ์‚ฌ์šฉํ•œ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

 

interface Image { public void displayImage(); } //on System A class RealImage implements Image { private String filename; public RealImage(String filename) { this.filename = filename; loadImageFromDisk(); } private void loadImageFromDisk() { System.out.println("Loading " + filename); } public void displayImage() { System.out.println("Displaying " + filename); } } //on System B class ProxyImage implements Image { private String filename; private Image image; public ProxyImage(String filename) { this.filename = filename; } public void displayImage() { if (image == null) { image = new RealImage(filename); } image.displayImage(); } } class ProxyExample { public static void main(String[] args) { Image image1 = new ProxyImage("HiRes_10MB_Photo1"); Image image2 = new ProxyImage("HiRes_10MB_Photo2"); image1.displayImage(); // loading necessary image2.displayImage(); // loading necessary
image1.displayImage(); // loading unnecessary image2.displayImage(); // loading unnecessary } }

๋˜ ๋‹ค๋ฅธ ๋ชฉ์ ( ๋ฆฌ๋ˆ…์Šค์—์„œ ๊ถŒํ•œ์— ๋”ฐ๋ผ ๋ช…๋ น์–ด ์‹คํ–‰์„ ๋ง‰๊ฑฐ๋‚˜ ํ†ต๊ณผ์‹œํ‚ค๋Š”)์œผ๋กœ ์‚ฌ์šฉ๋œ ํ”„๋ก์‹œ ํŒจํ„ด์˜ ์˜ˆ์ œ๋Š” ์•„๋ž˜ ๋งํฌ์—์„œ ๋ณด์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

http://www.journaldev.com/1572/proxy-design-pattern-in-java-example-tutorial

 



์ถœ์ฒ˜ :  http://sourcemaking.com/design_patterns/proxy

Design Patterns : Element of Reusable Object Oriented Software ( by GoF, 1994 )  

 http://ko.wikipedia.org/wiki/%ED%94%84%EB%A1%9D%EC%8B%9C_%ED%8C%A8%ED%84%B4