๐Ÿ’ป Programming (358)

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.

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

 

์˜ค๋Š˜ ์‚ดํŽด๋ณผ  Chain of Responsibility (์ฑ…์ž„ ์ „๊ฐ€ )ํŒจํ„ด์€ Behavioral ํŒจํ„ด์— ์†ํ•œ๋‹ค.  

์ฑ…์ž„ ์ „๊ฐ€๋ž€ ๋ญ๋ƒ? ์ฑ…์ž„์„ ๋‹ค๋ฅธ ๋†ˆํ•œํ…Œ ๋„˜๊ธด๋‹ค๋Š” ๋ง์ด๋‹ค. ๊ทธ๊ฒŒ ๋ฌด์Šจ ๋ง์ด๋ƒ๊ณ ?

์ œ ์กฐ์—…์„ ํ•˜๋Š” ๊ณต์žฅ์„ ์˜ˆ๋กœ ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ด ๊ณต์žฅ์€ ์ž๋™์ฐจ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ž๋™์ฐจ๋ฅผ ๋งŒ๋“œ๋Š” ๊ณผ์ •์€ ๋‚ด๋ถ€์—์„œ๋ถ€ํ„ฐ ์ฐจ๊ทผ ์ฐจ๊ทผ ์กฐ๋ฆฝํ•ด ๋‚˜๊ฐ€๋Š” ๊ฑฐ์ฃ . ์ฐจ์ฒด ๋ฐ”๋‹ฅ์„ ๊น”๊ณ  ๊ทธ ์œ„์— ์˜์ž๋ฅผ ์˜ฌ๋ฆฌ๊ณ  ์—”์ง„์„ ๋ผ์›Œ๋„ฃ๊ณ  ์ด๋Ÿฐ์‹์œผ๋กœ ๋ถ€ํ’ˆ(์ฑ…์ž„)์„ ๊ณ„์† ์กฐ๋ฆฝํ•ด ๋„ฃ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ค๊ฐ€ ๋งˆ์ง€๋ง‰์— ์™„์„ฑ๋œ ์ฐจ๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋˜๋Š”๊ฑฐ์ฃ .  

์ด ๊ธ€์„ ์ฝ๊ณ ์žˆ๋Š” ๋…์ž๊ป˜์„œ๋Š” ๋งก์€ ๋ถ€๋ถ„์ด ์žˆ๊ฒ ์ฃ . ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋ฐ”ํ€ด๋ฅผ ๋ผ์›Œ๋„ฃ๋Š” ์ผ์„ ํ•ด์•ผํ•œ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๋…์ž๋‹˜์€ ๊ทธ๋ƒฅ ๋ฐ”ํ€ด๋งŒ ๋ผ์šฐ๊ณ  ๋‚˜๋จธ์ง€๋Š” ์˜†์‚ฌ๋žŒํ•œํ…Œ ๋„˜๊ธฐ๋ฉด ๋˜๋Š” ๊ฒ๋‹ˆ๋‹ค. ๊ฐ„๋‹จํžˆ ๋งํ•˜๋ฉด ์ด๋Ÿฐ ๋งฅ๋ฝ์ž…๋‹ˆ๋‹ค.์ดํ•ด๊ฐ€ ๋˜์‹œ๋‚˜์š”?  

 

Chain of Responsibility Pattern Structure

 

 

A typical object structure might look like this:

์ผ๋ฐ˜์ ์ธ ๊ฐ์ฒด ๊ตฌ์กฐ๋Š” ์•„๋ž˜์™€ ๋น„์Šทํ•ฉ๋‹ˆ๋‹ค.


         

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

Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.

์ด ํŒจํ„ด์˜ ๋ชฉ์ ์€ ๋ง์ด์ฃ , A๋ผ๋Š” ๊ฐ์ฒด๊ฐ€ ์š”์ฒญ1์„ B๊ฐ์ฒด์— ๋ณด๋‚ผ ๋•Œ ์ค‘๊ฐ„์—์„œ ์ด ์š”์ฒญ์„ ํ•ธ๋“ค๋ง ํ•  ๋‹ค๋ฅธ ๊ฐ์ฒด๋“ค์„ ๋‘์–ด์„œ A ์™€ B ์‚ฌ์ด์˜ ์ปคํ”Œ๋ง ๊ด€๊ณ„๋ฅผ ํ”ผํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค. A ์™€ B ์‚ฌ์ด์˜ ๊ฐ ํ•ธ๋“ค๋Ÿฌ๋“ค์€ ์ž์‹ ์ด ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉด ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ณ  ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์—†์œผ๋ฉด ๋‹ค์Œ ํ•ธ๋“ค๋Ÿฌ๋กœ ์š”์ฒญ์„ ๋„˜๊ธฐ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.


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

 GoF ์˜ ๋””์ž์ธํŒจํ„ด์—์„œ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์žˆ๋Š” ๋„์›€๋ง๊ธฐ๋Šฅ์„ ์˜ˆ๋กœ ๋“ญ๋‹ˆ๋‹ค. ๋„์›€๋ง( ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฌผ์Œํ‘œ )์€ ์ปจํ…์ŠคํŠธ๋ณ„๋กœ ๋‹ค๋ฅด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์‰ฝ๊ฒŒ๋งํ•˜์ž๋ฉด ๋ฉ”๋‰ด๋ณ„๋กœ ์–ด๋–ค ๋ฉ”๋‰ด์ธ์ง€์— ๋Œ€ํ•œ ๋„์›€๋ง์ด ์žˆ์„ ์ˆ˜๋„ ์žˆ๊ณ  ๋ฒ„ํŠผ๋งˆ๋‹ค ์–ด๋–ค ๊ธฐ๋Šฅ์„ ์‹คํ–‰ํ•˜๋Š”์ง€์— ๋Œ€ํ•ด์„œ ๋„์›€๋ง์ด ์กด์žฌํ•˜๊ฒ ์ฃ . ๊ทธ๋Ÿฐ๋ฐ ๋„์›€๋ง์„ ์‹คํ–‰ํ•  ๋•Œ ์ด๊ฒŒ ์–ด๋–ค ๋…€์„์ด ๋„์›€๋ง์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ฒ˜๋ฆฌํ•ด์•ผํ•˜๋Š”์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋Š” ๋„์›€๋ง์„ ์š”์ฒญํ•˜๋Š” ๊ฐ์ฒด๊ฐ€ ๋ชจ๋ฅธ๋‹ค๋Š”๊ฒŒ ๋ฌธ์ œ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ˆ„๊ฐ€ ์ฒ˜๋ฆฌํ•ด์•ผํ•˜๋Š”์ง€๋„ ๋ชจ๋ฅด๋Š” ๊ฒƒ์„ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•ด์•ผํ•˜๋‚˜ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ๋‚˜์˜จ ํŒจํ„ด์ด๋ฐ”๋กœ ์ด ์ฑ…์ž„์ „๊ฐ€ ํŒจํ„ด์ž…๋‹ˆ๋‹ค. ๋„์›€๋ง๊ฐ์ฒด๊ฐ€ "A์— ๋Œ€ํ•œ ๋„์›€๋ง ์ฃผ์„ธ์š”" ๋ผ๊ณ  chain์— ์š”์ฒญ์„ ํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์ฒด์ธ์€ ํ•ธ๋“ค๋Ÿฌ๋“ค์ด ์ˆœ์„œ๋Œ€๋กœ ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค. ์ด ํ•ธ๋“ค๋Ÿฌ๋“ค ์ค‘์—์„œ ํ•œ๋†ˆ์€ A์— ๋Œ€ํ•œ ๋„์›€๋ง์„ ์ฃผ๋„๋ก ๋˜์–ด์žˆ๊ณ  ๋˜ ๋‹ค๋ฅธ ๋†ˆ์€ B์— ๋Œ€ํ•œ ๋„์›€๋ง์„ ์ฃผ๋„๋ก ๋˜์–ด์žˆ๋Š” ๊ฑฐ์ฃ . ๊ทธ๋ž˜์„œ ์ฒด์ธ์˜ ์‹œ์ž‘์ ์œผ๋กœ ์š”์ฒญ์„ ๋‚ ๋ฆฌ๋ฉด A์— ๋Œ€ํ•œ ๋„์›€๋ง์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ๋‚˜์˜ฌ๋•Œ๊นŒ์ง€ ์˜†์˜ ํ•ธ๋“ค๋Ÿฌ๋กœ ๊ณ„์† ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ค๊ฐ€ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ํ•ธ๋“ค๋Ÿฌ๋ฅผ ๋งŒ๋‚˜๋ฉด ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฑฐ์ฃ . ์—ฌ๊ธฐ์„œ ์ค‘์š”ํ•œ ๊ฒƒ์€ ์ด ์ฒด์ธ์˜ ๊ฐ€์žฅ ์ฒซ ํ•ธ๋“ค๋Ÿฌ๋Š” ๊ฐ€์žฅ specificํ•œ ๋†ˆ์ด์–ด์•ผ ํ•˜๊ณ  ์ฒด์ธ์˜ ์ œ์ผ ๋์— ์žˆ๋Š” ํ•ธ๋“ค๋Ÿฌ๋Š” ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์ธ(๊ณตํ†ต์ ์ธ) ๋†ˆ์ด์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๊ฒฐ๊ตญ ๋„์›€๋ง์„ ์ฐพ์ง€ ๋ชปํ•˜๋ฉด ์ผ๋ฐ˜์ ์ธ ๋„์›€๋ง์„ ๊ทธ๋ƒฅ ๋‚ด๋ณด๋‚ด๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด์ฃ . 

 

 

 

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

 

Use Chain of Responsibility when  

์ฑ…์ž„ ์ „๊ฐ€ ํŒจํ„ด์€ ์ด๋Ÿด๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

    • more than one object may handle a request, and the handler isn't known a priority. The handler should be ascertained automatically. ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ฐ์ฒด๊ฐ€ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•  ๋•Œ. ๊ทธ๋ฆฌ๊ณ  ํ•ธ๋“ค๋Ÿฌ์˜ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ์—†์„ ๋•Œ. ํ•ธ๋“ค๋Ÿฌ๋Š” ์ž๋™์ ์œผ๋กœ ์„ ํƒ๋˜์–ด์ ธ์•ผ ํ•œ๋‹ค.
       

    • you want to issue a request to one of several objects without specifying the receiver explicitly.
      ๋ฆฌ์‹œ๋ฒ„๋ฅผ ๋ช…์‹œํ•˜์ง€ ์•Š๊ณ  ์—ฌ๋Ÿฌ ๊ฐ์ฒด๋“ค ์ค‘ ํ•˜๋‚˜์— ์š”์ฒญ์„ ๋‚ ๋ฆฌ๊ณ  ์‹ถ์„ ๋•Œ 

    • the set of objects that can handle a request should be specified dynamically.
      ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด๋“ค์˜ set์ด ๋™์ ์œผ๋กœ ์ง€์ •๋˜์–ด์•ผ ํ•  ๋•Œ 


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


โ€ข Handler (HelpHandler)

o defines an interface for handling requests. ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ •์˜ํ•œ๋‹ค. 

o (optional) implements the successor link.(์„ ํƒ) successor ๋งํฌ๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค. 

โ€ข ConcreteHandler (PrintButton, PrintDialog)

o handles requests it is responsible for. ์ฑ…์ž„์ ธ์•ผํ•  ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ๋งŒ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.
o can access its successor. successor์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
o if the ConcreteHandler can handle the request,it does so; otherwise it forwards the request to its successor.
ConcreteHandler๊ฐ€ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์š”์ฒญ์€ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜์ง€๋งŒ ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ successor๋กœ ์š”์ฒญ์„ ํฌ์›Œ๋”ฉํ•œ๋‹ค. 

โ€ข Client

o initiates the request to a ConcreteHandler object on the chain.
์ฒด์ธ์— ์†ํ•œ ConcreteHandler ๊ฐ์ฒด๋กœ ์š”์ฒญ์„ ๋‚ ๋ฆฐ๋‹ค. 


์›๋ฆฌ ( Collaborations ) :

 

โ€ข When a client issues a request, the request propagates along the chain until a ConcreteHandler object takes responsibility for handling it.
ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์š”์ฒญ์„ ๋‚ ๋ฆฌ๋ฉด, ๊ทธ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ConcreteHandler๊ฐ€ ์š”์ฒญ์„ ๋ฐ›๊ณ  ์ฒ˜๋ฆฌ๋ฅผ ํ•  ๋•Œ ๊นŒ์ง€ ์ฒด์ธ์„ ํƒ€๊ณ  ๋‹ค๋ฅธ ํ•ธ๋“ค๋Ÿฌ๋กœ ์š”์ฒญ( ํ•ธ๋“ค๋Ÿฌ ์ž…์žฅ์—์„œ๋Š” ์ฒ˜๋ฆฌํ•ด์•ผํ•  ์ฑ…์ž„์ด๋ผ๊ณ  ๋ณด๋ฉด ๋ฉ๋‹ˆ๋‹ค )์ด ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. 

 


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

 

Chain of Responsibility has the following benefits and liabilities:  


1. Reduced coupling.The pattern frees an object from knowing which other object handles a request. An object only has to know that a request will be handled "appropriately." Both the receiver and the sender have no explicit knowledge of each other, and an object in the chain doesn't have to know about the chain's structure.

As a result, Chain of Responsibility can simplify object inter connections. Instead of objects maintaining references to all candidate receivers, they keep a single reference to their successor.
์ฑ…์ž„ ์ „๊ฐ€ ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋ฉด ์ปคํ”Œ๋ง์„ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ํ•ธ๋“ค๋Ÿฌ๋Š” ์ฒด์ธ์˜ ๊ตฌ์กฐ๋ฅผ ์•Œ ํ•„์š”๋„ ์—†๊ณ  ๋‹จ์ง€ ๋‹ค์Œ ํ•ธ๋“ค๋Ÿฌ์— ๋Œ€ํ•œ reference๋งŒ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ฐ€ ์š”์ฒญ์ด ๋“ค์–ด์™”์„ ๋•Œ ์ž์‹ ์ด ํ•ด๊ฒฐ ํ•  ์ˆ˜ ์—†๋Š” ์ผ์ด๋ฉด ๋‹ค์Œ ํ•ธ๋“ค๋Ÿฌ๋กœ ์š”์ฒญ์„ ๋„˜๊ธฐ๋Š” ์ผ๋งŒ ํ•˜๋ฉด ๋˜๋Š” ๊ฒƒ์ด์ฃ .  

 

2. Added flexibility in assigning responsibilities to objects. Chain of Responsibility gives you added flexibility in distributing responsibilities among objects. You can add or change responsibilities for handling a request by adding to or otherwise changing the chain at run-time. You can combine this with subclassing to specialize handlers statically.  


3. Receipt isn't guaranteed. Since a request has no explicit receiver, there's no guarantee it'll be handledโ€”the request can fall off the end of the chainwithout ever being handled. A request can also go unhandled when thechain is not configured properly.

 

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

 

Chain of Responsibility is often applied in conjunction with Composite. There, a component's parent can act as its successor.์ฑ…์ž„ ์ „๊ฐ€ ํŒจํ„ด์€ ์ข…์ข… ์ปดํฌ์ง€ํŠธ ํŒจํ„ด๊ณผ ํ•จ๊ป˜ ์“ฐ์ž…๋‹ˆ๋‹ค.

 

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

    • Chain of Responsibility, Command, Mediator, and Observer, address how you can decouple senders and receivers, but with different trade-offs. Chain of Responsibility passes a sender request along a chain of potential receivers. ์ฑ…์ž„์ „๊ฐ€, ์ปค๋งจ๋“œ, ์ค‘์žฌ์ž, ๊ด€์ฐฐ์ž ํŒจํ„ด์€ ๋ชจ๋‘ sender์™€ receiver์‚ฌ์ด์˜ ์ปคํ”Œ๋ง์„ ์ค„์—ฌ์ฃผ์ง€๋งŒ ๊ฐ๊ธฐ ๋‹ค๋ฅธ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ, ๋””์ปคํ”Œ๋ง ํ•จ์œผ๋กœ์จ ๋“ค์–ด๊ฐ€์•ผ ํ•˜๋Š” ๋น„์šฉ(๋‹จ์ ) ๋˜ํ•œ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.
    • Chain of Responsibility can use Command to represent requests as objects.

 


 

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

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


 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.

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

 

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

 


 

Visitor Pattern Structure

 


 


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

Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

 

 

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

Consider a compiler that represents programs as abstract syntax trees. It will need to perform operations on abstract syntax trees for "static semantic" analyses like checking that all variables are defined. Itwill also need to generate code. So it might define operations for type-checking, code optimization, flow analysis, checking for variables being assigned values before they're used, and so on.
Moreover, we could use the abstract syntax trees for pretty-printing, program restructuring, code instrumentation, and computing various metrics of aprogram.
Most of these operations will need to treat nodes that represent assignment statements differently from nodes that represent variables orarithmetic expressions. Hence there will be one class for assignment statements, another for variable accesses, another for arithmetic expressions, and so on. The set of node classes depends on the language being compiled, of course, but it doesn't change much for a given language.


 


 This diagram shows part of the Node class hierarchy. The problem here is that distributing all these operations across the various node classes leads to a system that's hard to understand, maintain, and change. It will be confusing to have type-checking code mixed with pretty-printing code or flow analysis code. Moreover, adding a new operation usually requires recompiling all of these classes. It would be better if each new operation could be added separately, and the node classes were independent of the operations that apply to them.
We can have both by packaging related operations from each class in a separate object, called a visitor, and passing it to elements of the abstract syntax tree as it's traversed. When an  element "accepts" the visitor, it sends a request to the visitor that encodes the element's class. It also includes the element as an argument. The visitor will then execute the operation for that element โ€”the operation that used to be in the class of the element.

 

For example, a compiler that didn't use visitors might type-check a procedure by calling the TypeCheck operation on its abstract syntax tree. Each of the nodes would implement TypeCheck by calling TypeCheck on its components (see the preceding class diagram). If the compiler type-checked a procedure using visitors, then it would create a TypeCheckingVisitor object and call the Accept operation on the abstract syntax tree with that object as an argument. Each of the nodes would implement Accept by calling back on the visitor: anassignment node calls
VisitAssignment operation on the visitor, while a variable reference calls VisitVariableReference. What used to be the TypeCheck operation in class AssignmentNode is now the VisitAssignment operation on TypeCheckingVisitor.  

To make visitors work for more than just type-checking, we need an abstract parent class NodeVisitor for all visitors of an abstract syntax tree. NodeVisitor must declare an operation for each node class. An application that needs to compute program metrics will define new subclasses of NodeVisitor and will no longer need to add application-specific code to the node classes. The Visitor pattern encapsulates the operations for each compilation phase in a Visitor associated with that phase.

 





  

With the Visitor pattern, you define two class hierarchies: one for the elements being operated on (the Node hierarchy) and one for the visitors that define operations on the elements (the  NodeVisitor hierarchy). You create a new operation by adding a new subclass to the visitor class hierarchy. As long as the grammar that the compiler accepts doesn't change (that is, we don't have to add new Node subclasses), we can add new functionality simply by defining new NodeVisitor subclasses.

 

 

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

Use the Visitor pattern when

Visitor ํŒจํ„ด์€ ์ด๋Ÿด๋•Œ ์‚ฌ์šฉํ•˜์ž!!


ยท an object structure contains many classes of objects with differing interfaces, and you want to  perform operations on these objects that depend on their concrete classes.
ยท many distinct and unrelated operations need to be performed on objects in an object structure, and you want to avoid "polluting" their classes with these operations. Visitor lets you keep related operations together by defining them in one class. When the object structure is shared by many applications, use Visitor to put operations in just those applications that need them.
ยท the classes defining the object structure rarely change, but you often want to define new operations over the structure. Changing the object structure classes requires redefining the interface to all visitors,which is potentially costly. If the object structure classes change often, then it's probably better to define the operations in those classes.

 

 

๋“ฑ์žฅ ์ธ๋ฌผ ( Participants ) :
ยท Visitor (NodeVisitor)
o declares a Visit operation for each class of ConcreteElement in the object structure. The operation's name and signature identifies the class that sends the Visit request to the visitor. That lets the visitor determine the concrete class of the element being visited. Then the visitor can access the element directly through its particular interface.


ยท ConcreteVisitor (TypeCheckingVisitor)
o implements each operation declared by Visitor. Each operation implements a fragment of the algorithm defined for the corresponding class of object in the structure. ConcreteVisitor provides the context for the algorithm and stores its local state. This state often accumulates results during the traversal of the structure.


ยท Element (Node)
o defines an Accept operation that takes a visitor as an argument.


ยท ConcreteElement (AssignmentNode,VariableRefNode)
o implements an Accept operation that takes a visitor as an argument.


ยท ObjectStructure (Program)
o can enumerate its elements.
o may provide a high-level interface to allow the visitor to visit its elements.
o may either be a composite (see Composite ) or a collection such as a list or a set.

 

์›๋ฆฌ ( Collaborations ) :

ยท A client that uses the Visitor pattern must create a ConcreteVisitor object and then traverse the object structure, visiting each element with the visitor.
ยท When an element is visited, it calls the Visitor operation that corresponds to its class. The element supplies itself as an argument to this operation to let the visitor access its state, if necessary.
The following interaction diagram illustrates the collaborations between an object structure, a visitor, and two elements:





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


Some of the benefits and liabilities of the Visitor pattern are as follows:


1. Visitor makes adding new operations easy. Visitors make it easy to add operations that depend on the components ofcomplex objects. You can define a new operation over an object  structure simply by adding a new visitor.
In contrast, if you spread functionality over many classes, then you must change each class to  define a newoperation.

2. A visitor gathers related operations and separates unrelated ones. Related behavior isn't spread over the classes defining the object structure; it's localized in a visitor. Unrelated sets of behavior are partitioned in their own visitor subclasses. That simplifies both the classes defining the elements and the algorithms defined in the visitors. Any algorithm-specific
data structures can be hidden in thevisitor.


3. Adding new ConcreteElement classes is hard. The Visitor pattern makes it hard to add new subclasses of Element. Each new ConcreteElement gives rise to a new abstract operation on Visitor and a corresponding implementation in every ConcreteVisitor class. Sometimes a default implementation can be provided in Visitor that can be inherited by most of the ConcreteVisitors, but this is the exception rather than the rule. So the key consideration in applying the Visitor pattern is whether you are mostly likely to change the algorithm applied over an object structure or the classes of objects that make up the structure. TheVisitor class hierarchy can be difficult to maintain when new ConcreteElement classes are added frequently. In such cases, it's probably easier just to define operations on the classes that make up the structure. If the Element class hierarchy is stable, but you are continually adding operations or changing algorithms, then the Visitor pattern will help you manage the changes.


4. Visiting across class hierarchies. An iterator (see Iterator ) can visit the objects in a structure as it traverses them by calling their operations. But an iterator can't work across object structures with different types of elements.  


5. Accumulating state. Visitors can accumulate state as they visit each element in the object structure. Without a visitor, this state would be passed as extra arguments to the operations that perform the traversal, or they might appear as global variables.


6. Breaking encapsulation. Visitor's approach assumes that the ConcreteElement interface is powerful enough to let visitors do their job.
As a result, the pattern oftenforces you to provide public operations that
access an element's internal state, which may compromise its encapsulation.


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

Composite :Visitors can be used to apply an operation over an object structure defined by the Composite pattern.
Interpreter
:Visitor may be applied to do the interpretation.

 

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

  • The abstract syntax tree of Interpreter is a Composite (therefore Iterator and Visitor are also applicable).
  • Iterator can traverse a Composite. Visitor can apply an operation over a Composite.
  • The Visitor pattern is like a more powerful Command pattern because the visitor may initiate whatever is appropriate for the kind of object it encounters.
  • The Visitor pattern is the classic technique for recovering lost type information without resorting to dynamic casts.

 

 

 

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

         Design Patterns : Element of Reusab

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.

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

 

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


๋นŒ๋” ํŒจํ„ด์€ ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ๋˜๋Š” ์ถ”์ƒ ํŒฉํ† ๋ฆฌ ํŒจํ„ด์˜ ๋ฌธ์ œ์ ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋‚˜์˜จ ํŒจํ„ด์ด๋‹ค. ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ๋˜๋Š” ์ถ”์ƒ ํŒฉํ† ๋ฆฌ ํŒจํ„ด์—๋Š” ๊ฐ์ฒด๊ฐ€ ๋งŽ์€ ์†์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ์„ ๊ฒฝ์šฐ ๋‘๊ฐ€์ง€ ์ค‘์š”ํ•œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. 

 

์ฒซ ์งธ๋กœ, ํด๋ผ์ด์–ธํŠธ๊ฐ€ ํŒฉํ† ๋ฆฌ ํด๋ž˜์Šค๋กœ ๋„˜๊ฒจ์ฃผ๋Š” ์ธ์ž๊ฐ€ ๋งŽ์•„์ง€๊ฒŒ๋˜๋ฉด( ํŒฉํ† ๋ฆฌ๊ฐ€ ๋งŽ์€ ์†์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉด ์ด์— ๋Œ€ํ•ด ์„ค์ •์„ ํ•˜๊ธฐ์œ„ํ•ด ๋งŽ์€ ์ธ์ž๋ฅผ ๋ฐ›์•„์•ผ ํ•œ๋‹ค ) ์ž˜๋ชป๋œ ์ธ์ž๋ฅผ ๋„˜๊ฒจ์ฃผ๋Š” ์ผ๋„ ๋นˆ๋ฒˆํ•˜๊ฒŒ ์ผ์–ด๋‚  ๋ฟ ์•„๋‹ˆ๋ผ ๊ด€๋ฆฌํ•˜๊ธฐ๋„ ํž˜๋“ค์–ด์ง„๋‹ค.

๋‘˜์งธ๋กœ, ๋ช‡๋ช‡ ์ธ์ž๋“ค์€ optional์ผ ์ˆ˜ ์žˆ์ง€๋งŒ ํŒฉํ† ๋ฆฌ ํŒจํ„ด์—์„œ๋Š” ๋ชจ๋“  ์ธ์ž๋ฅผ ๋„˜๊ฒจ์ฃผ์–ด์•ผ ํ•œ๋‹ค. ( optional ์ธ์ž๋Š” null๋กœ ๋„˜๊ธด๋‹ค. ์ด๊ฑด ๊ท€์ฐฎ์€ ์ผ์ด๋‹ค. )

 

๋นŒ๋” ํŒจํ„ด์€ ๋‹จ๊ณ„๋ณ„๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋งˆ์ง€๋ง‰ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•จ์œผ๋กœ์จ ์œ„์™€ ๊ฐ™์€ ๋ฌธ์ œ๋“ค์„ ํ•ด๊ฒฐํ•ด์ค€๋‹ค. 


 

Builder Pattern Structure



 


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

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

 ๋ณตํ•ฉ ๊ฐ์ฒด์˜ ์ƒ์„ฑ ๊ณผ์ •๊ณผ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์„ ๋ถ„๋ฆฌํ•˜์—ฌ ๋™์ผํ•œ ์ƒ์„ฑ ์ ˆ์ฐจ์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ํ‘œํ˜„ ๊ฒฐ๊ณผ๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ํŒจํ„ด


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


 

์˜ˆ ๋ฅผ๋“ค์–ด RTFํฌ๋งท ํ˜•ํƒœ์˜ ํŒŒ์ผ์„ ASCIIText๋‚˜ TeXText ๋˜๋Š” ๋‹ค๋ฅธ TextWidget์˜ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ ๋‹ค๊ณ  ํ•˜์ž. ์ด๋•Œ ๋ณ€ํ™˜๋˜์–ด ๋‚˜์˜ค๋Š” ํฌ๋งท์„ ์‰ฝ๊ฒŒ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•  ๊ฒƒ์ด๋‹ค. ๊ทธ๋ž˜์„œ ๋‚˜์˜จ ํŒจํ„ด์ด ๋ฐ”๋กœ ์ด ๋นŒ๋” ํŒจํ„ด์ด๋‹ค. ์œ„ ๋‹ค์ด์–ด๊ทธ๋žจ์—์„œ ๊ฐ๊ฐ์˜ ์ปจ๋ฒ„ํ„ฐ ํด๋ž˜์Šค๋“ค์€ ๋ชจ๋‘ builder์ด๋ฉฐ reader๋Š” director๋ผ ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ณ€ํ˜•๋˜์–ด ๋‚˜์˜ค๋Š” Text๊ฐ์ฒด๋“ค์€ product๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค. 

 


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

Use the Builder pattern when
ยท the algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled.
ยท the construction process must allow different representations for the object that's constructed.

 

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

ยท Builder
o specifies an abstract interface for creating parts of a Product object.
ยท ConcreteBuilder
o constructs and assembles parts of the product by implementing the Builder interface.
o defines and keeps track of the representation it creates.
o provides an interface for retrieving the product.
ยท Director
o constructs an object using the Builder interface.
ยท Product
o represents the complex object under construction. ConcreteBuilder builds the product's internal representation and defines the process by which it's assembled.
o includes classes that define the constituent parts, including interfaces for assembling the parts into the final result.


์›๋ฆฌ ( Collaborations ) :

ยท The client creates the Director object and configures it with the desired Builder object.
ยท Director notifies the builder whenever a part of the product should be built.
ยท Builder handles requests from the director and adds parts to the product.
ยท The client retrieves the product from the builder.

 

The following interaction diagram illustrates how Builder and Director cooperate with a client.

 

 


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

Here are key consequences of the Builder pattern:


1. It lets you vary a product's internal representation. The Builder object provides the director with an abstract interface for constructing the product. The interface lets the builder hide the representation and internal structure of the product. It also hides how the product gets assembled. Because the product is constructed through an abstract interface, all you have to do to change the product's internal representation is define a new kind of builder.


2. It isolates code for construction and representation. The Builder pattern improves modularity by encapsulating the way a complex object is constructed and represented. Clients needn't know anything about the classes that define the product's internal structure; such classes don't appear in Builder's interface. Each ConcreteBuilder contains all the code to create and assemble a particular kind of product. The code is written once; then different Directors can reuse it to build Product variants from the same set of parts.
In the earlier RTF example, we could define a reader for a format other than RTF, say, an SGMLReader, and use the same TextConverters to generate ASCIIText, TeXText, and TextWidget renditions of SGML documents.


3. It gives you finer control over the construction process. Unlike creational patterns that construct products in one shot, the Builder pattern constructs the product step by step under the director's control. Only when the product is finished does the director retrieve it from the builder. Hence the Builder interface reflects the process of constructing the product more than other creational patterns. This gives you finer control over the construction process and consequently the internal structure of the resulting product. 

 

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

 Abstract Factory is similar to Builder in that it โ€‹constructs too may complex objects. The primary difference is that the Builder pattern focuses on constructing a complex object step by step. Abstract Factory's emphasis is on families of product objects (either simple or complex). Builder returns the product as a final step, but as far as the Abstract Factory pattern is concerned, the product gets returned immediately.
A Composite is what the builder often builds.

 

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

  • Sometimes creational patterns are complementory: Builder can use one of the other patterns to implement which components get built. Abstract Factory, Builder, and Prototype can use Singleton in their implementations.
  • Builder focuses on constructing a complex object step by step. Abstract Factory emphasizes a family of product objects (either simple or complex). Builder returns the product as a final step, but as far as the Abstract Factory is concerned, the product gets returned immediately.
  • Builder often builds a Composite.
  • Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed.

 

 

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

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

http://www.journaldev.com/1425/builder-design-pattern-in-java

 


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.

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

 

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

 

์ด ํŒจํ„ด์„ ์ ์šฉํ•ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

1. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์ƒ์„ฑ๋˜๋Š” ๊ฐ์ฒด์˜ ์ˆ˜๊ฐ€ ์—„์ฒญ๋‚˜๊ฒŒ ํด ๋•Œ

2. ๊ฐ์ฒด์˜ ์ƒ์„ฑ์ด ๋ฉ”๋ชจ๋ฆฌ์— ๋งŽ์€ ๋ถ€ํ•˜๋ฅผ ์ฃผ๊ณ  ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆด ๋•Œ


์ข€ ๋” ์ž์„ธํžˆ ์ด ํŒจํ„ด์— ๋Œ€ํ•ด์„œ ์งš์–ด๋ณด์ž.

 

Flyweight Pattern Structure

  


 
The following object diagram shows how flyweights are shared:

 

 


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

Use sharing to support large numbers of fine-grained objects efficiently.

ํ”Œ๋ผ์ด์›จ์ดํŠธ ํŒจํ„ด์€ ๋งŽ์€ ์ˆ˜์˜ ๊ฐ์ฒด๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ œ๊ณตํ•˜๊ธฐ์œ„ํ•ด ๊ณต์œ ๊ฐœ๋…์„ ์ด์šฉํ•œ๋‹ค.  

 

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

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


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

The Flyweight pattern's effectiveness depends heavily on how and where it's used.

ํ”Œ๋ผ์ด์›จ์ดํŠธ ํŒจํ„ด์˜ ํšจ๊ณผ๋Š” ์–ด๋–ป๊ฒŒ ์–ด๋””์„œ ์‚ฌ์šฉ๋˜๋Š๋ƒ์— ๋”ฐ๋ผ ์‹ฌํ•˜๊ฒŒ ์ขŒ์ง€์šฐ์ง€ ๋œ๋‹ค.


Apply the Flyweight pattern when all of the following are true:

์•„๋ž˜ ๋‚ด์šฉ๊ณผ ๋ชจ๋‘ ๋งž์•„ ๋–จ์–ด์ง€๋Š” ์ƒํ™ฉ์—์„œ๋งŒ ์ด ํŒจํ„ด์„ ์ ์šฉํ•˜๋ฉด ๋œ๋‹ค.


ยท An application uses a large number of objects.

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ •๋ง ๋งŽ์€ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ
ยท Storage costs are high because of the sheer quantity of objects.

์ €์žฅ๋น„์šฉ์ด ๋†’์„ ๋•Œ( ๊ฐ์ฒด ๊ฐœ์ˆ˜๊ฐ€ ๋งŽ์•„์„œ )
ยท Most object state can be made extrinsic.

๋Œ€๋ถ€๋ถ„์˜ ๊ฐ์ฒด์˜ ์ƒํƒœ๊ฐ€ ๋ถ€๋Œ€์ ์ธ ๊ฒƒ์ผ ๋•Œ
ยท Many groups of objects may be replaced by relatively few shared objects once extrinsic state is removed.

๋ถ€๋Œ€์ ์ธ ์ƒํƒœ๊ฐ€ ์ œ๊ฑฐ๋์„ ๋•Œ์—๋Š” ์ ์€ ์ˆ˜์˜ ๊ณต์œ  ๊ฐ์ฒด๋กœ ๋งŽ์€ ์ˆ˜์˜ ๊ฐ์ฒด๋ฅผ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์„ ๋•Œ
ยท The application doesn't depend on object identity. Since flyweight objects may be shared, identity tests will return true for conceptually distinct objects.

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๊ฐ์ฒด์˜ identity์— ์˜์กด์ ์ด์ง€ ์•Š์„ ๋•Œ 


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

ยท Flyweight
o declares an interface through which flyweights can receive and act
on extrinsic state.
ยท ConcreteFlyweight (Character)
o implements the Flyweight interface and adds storage for intrinsic state, if any. A ConcreteFlyweight object must be sharable. Any state it stores must be intrinsic; that is, it must be independent of the ConcreteFlyweight object's context.
ยท UnsharedConcreteFlyweight (Row, Column)
o not all Flyweight subclasses need to be shared. The Flyweight interface enables sharing; it doesn't enforce it. It's common for
UnsharedConcreteFlyweight objects to have ConcreteFlyweight objects as children at some level in the flyweight object structure
(as the Row and Column classes have).
ยท FlyweightFactory
o creates and manages flyweight objects.
o ensures that flyweights are shared properly. When a client requests a flyweight, the FlyweightFactory object supplies an existing
instance or creates one, if none exists.  

ยท Client
o maintains a reference to flyweight(s).
o computes or stores the extrinsic state of flyweight(s).


์›๋ฆฌ ( Collaborations ) :

ยท State that a flyweight needs to function must be characterized as either intrinsic or extrinsic. Intrinsic state is stored in the ConcreteFlyweight object; extrinsic state is stored or computed by Client objects. Clients pass this state to the flyweight when they invoke its operations.
ยท Clients should not instantiate ConcreteFlyweights directly. Clients must obtain ConcreteFlyweight objects exclusively from the FlyweightFactory object to ensure they are shared properly.


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

 
Flyweights may introduce run-time costs associated with transferring, finding, and/or computing extrinsic state, especially if it was formerly stored as intrinsic state. However, such costs are offset by space savings, which increase as more
flyweights are shared.


Storage savings are a function of several factors:


ยท the reduction in the total number of instances that comes from sharing
ยท the amount of intrinsic state per object
ยท whether extrinsic state is computed or stored.


The more flyweights are shared, the greater the storage savings. The savings increase with the amount of shared state. The greatest savings occur when the objects use substantial quantities of both intrinsic and extrinsic state, and the extrinsic state can be computed rather than stored. Then you save on storage in two ways: Sharing reduces the cost of intrinsic state, and you trade extrinsic state for computation time.
The Flyweight pattern is often combined with the Composite (183) pattern to represent a hierarchical structure as a graph with shared leaf nodes. A consequence of sharing is that flyweight leaf nodes cannot store a pointer to their parent. Rather, the parent pointer is passed to the flyweight as part of its extrinsic state. This has a major impact on how the objects in the hierarchy  communicate with each other. 

 

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

The Flyweight pattern is often combined with the Composite pattern to implement a logically hierarchical structure in terms of a directed-acyclic graph with shared leaf nodes.

It's often best to implement State and Strategy objects as flyweights. 


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

  • Whereas Flyweight shows how to make lots of little objects, Facade shows how to make a single object represent an entire subsystem.
  • Flyweight is often combined with Composite to implement shared leaf nodes.
  • Terminal symbols within Interpreter's abstract syntax tree can be shared with Flyweight.
  • Flyweight explains when and how State objects can be shared.
  • Flyweight pattern introduces complexity and if number of shared objects are huge then there is a trade of between memory and time, so we need to use it judiciously based on our requirements.
  • Flyweight pattern implementation is not useful when the number of intrinsic properties of Object is huge, making implementation of Factory class complex.

 

 

 

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

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

http://www.journaldev.com/1562/flyweight-pattern-in-java-example-tutorial

 


 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.

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

 

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

 

Singleton Pattern Structure


 

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

Ensure a class only has one instance, and provide a global point of access to it.

ํ”„๋กœ๊ทธ๋žจ์ƒ์—์„œ ๋‹จ ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค๋งŒ์„ ์š”๊ตฌํ•˜๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค ํ•„์š”๊ฐ€ ์žˆ์–ด์„œ ๋‚˜์˜จ ํŒจํ„ด์ด๋‹ค. globalํ•˜๊ฒŒ access๊ฐ€๋Šฅํ•œ ๋‹จ ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ์ด ์ด ํŒจํ„ด์˜ ๋ชฉ์ ์ด๋‹ค. 

 

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

Application needs one, and only one, instance of an object. Additionally, lazy initialization and global access are necessary.

๋ชฉ์ ์—์„œ ๋งํ•œ ๋ฐ”์™€ ๊ฐ™์ด ๋‹จ ํ•˜๋‚˜์˜ ์œ ์ผํ•œ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์ด๋ฅผ ์‚ฌ์šฉํ•  ๋ชฉ์ ์œผ๋กœ ๋‚˜์˜ค๊ฒŒ ๋œ ํŒจํ„ด์ด๋‹ค. ํ•œ๋ฒˆ ์ƒ์„ฑํ•˜๋ฉด ๊ทธ ์ดํ›„์—๋Š” ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋ฅผ ์ด์šฉ๋งŒ ํ•  ๋ฟ์ด๋‹ค. 


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

Use the Singleton pattern when
ยท there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point.
ยท when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.

 

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

ยท Singleton
o defines an Instance operation that lets clients access its unique instance. Instance is a class operation (that is, a class method in Smalltalk and a static member function in C++).
o may be responsible for creating its own unique instance.


์›๋ฆฌ ( Collaborations ) :

ยท Clients access a Singleton instance solely through Singleton's Instance operation.

ํด๋ผ์ด์–ธํŠธ๋“ค์€ ์ด ์‹ฑ๊ธ€ํ„ด๊ฐ์ฒด์—์„œ ์ œ๊ณตํ•˜๋Š”  getter๋ฉ”์†Œ๋“œ์— ์˜ํ•ด ์˜ค์ง ํ˜ผ์ž๋งŒ ์ ‘๊ทผํ•œ๋‹ค.


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

The Singleton pattern has several benefits:
1. Controlled access to sole instance. Because the Singleton class encapsulates its sole instance, it can have strict control over how and when clients access it.
2. Reduced name space. The Singleton pattern is an improvement over global variables. It avoids polluting the name space with global variables that store sole instances.
3. Permits refinement of operations and representation. The Singleton class may be subclassed, and it's easy to configure an application with an instance of this extended class. You can configure the application with an instance of the class you need at run-time.
4. Permits a variable number of instances. The pattern makes it easy to change your mind and allow more than one instance of the Singleton class. Moreover, you can use the same approach to control the number of instances that the application uses. Only the operation that grants access to the Singleton instance needs to change.
5. More flexible than class operations. Another way to package a singleton's functionality is to use class operations (that is, static member functions in C++ or class methods in Smalltalk). But both of these language techniques make it hard to change a design to allow more than one instance of a class. Moreover, static member functions in C++ are never virtual, so subclasses can't override them polymorphically.

 

์†Œ์Šค ์˜ˆ์ œ : 


1. ์ผ๋ฐ˜์ ์ธ ๊ฒฝ์šฐ ( synchronizingํ•  ํ•„์š”๊ฐ€ ์—†๋Š” ์‹ฑ๊ธ€ํ„ด ๊ฐ์ฒด์˜ ๊ฒฝ์šฐ, ์‹ฑ๊ธ€์“ฐ๋ ˆ๋“œ(single thread) )

 

// ์‹ฑ๊ธ€ํ„ด๊ฐ์ฒด๊ฐ€ ํ•„์š”ํ•  ๋•Œ๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ์ง์ ‘ ๋งŒ๋“œ๋Š”๊ฒŒ ์•„๋‹ˆ๊ณ  ์ธ์Šคํ„ด์Šค๋ฅผ ๋‹ฌ๋ผ๊ณ  ์š”์ฒญ์„ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
public class Singleton {
  private static Singleton uniqueInstance;
 
  private Singleton() {}  // <---  ์ƒ์„ฑ์ž๋Š” private -- ์™ธ๋ถ€์—์„œ new ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๋‹ค...
 
  public static Singleton getInstance() {  // ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” static ๋ฉ”์†Œ๋“œ ํ•˜๋‚˜
    if ( uniqueInstance == null ) {        // ์ด ๋ถ€๋ถ„์€ ๊ฐ์ฒด๋ฅผ ์ฒ˜์Œ ์ƒ์„ฑํ•  ๋•Œ๋งŒ ํ•„์š”ํ•˜๋‹ค~~
      uniqueInstance = new Singleton();
    }
    return uniqueInstance;
  }
}

2. ์—ฌ๋Ÿฌ๋ช…์ด ๋™์‹œ์ ‘๊ทผํ•  ์ˆ˜๋„ ์žˆ๋Š”
์‹ฑ๊ธ€ํ„ด ๊ฐ์ฒด์˜ ๊ฒฝ์šฐ ( ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ, multi thread )

A. ์œ„ ์ฝ”๋“œ์˜ ๋ฌธ์ œ์  )
public class Singleton {
  private static Singleton uniqueInstance;
 
  private Singleton() {}
 
  public static Singleton getInstance() {  
    if ( uniqueInstance == null ) {          // A Thread๊ฐ€ ์ด ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•  ๋•Œ
      uniqueInstance = new Singleton();      // B Thread๊ฐ€ ๊ฐ์ฒด ์ƒ์„ฑ์„ ์™„๋ฃŒํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ 
    }                                        // ๋‘๊ฐœ์˜ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋  ์ˆ˜ ์žˆ๋‹ค.
 
    return uniqueInstance;
  }
}

B.Method synchronizing ์œผ๋กœ ๋ฌธ์ œ ํ•ด๊ฒฐ ๊ฐ€๋Šฅ 

 

public class Singleton {
  private static Singleton uniqueInstance;
 
  private Singleton() {}

  // ๋ฉ”์†Œ๋“œ ์ž์ฒด์— synchronized ๋ฅผ ๊ฑฐ๋Š” ๋ฐฉ๋ฒ• 
  public static synchronized Singleton getInstance() {  
    if ( uniqueInstance == null ) {          
      uniqueInstance = new Singleton(); 
    }                                        
 
    return uniqueInstance;
  }
}

์ด ๋ฐฉ๋ฒ•์˜ ๋ฌธ์ œ์  )
  • ์ด ๋ฐฉ๋ฒ•์€ ์„ฑ๋Šฅ์— ์˜ํ–ฅ์„ ๋ผ์น  ์ˆ˜ ์žˆ๋‹ค.
    ( ๋ฉ”์†Œ๋“œ๋ฅผ ๋™๊ธฐํ™”ํ•˜๋ฉด ์„ฑ๋Šฅ์ด 100๋ฐฐ ์ •๋„ ์ €ํ•˜๋œ๋‹ค๊ณ  ์ฑ…์— ์ ํ˜€์žˆ์Šต๋‹ˆ๋‹ค. )
  • synchronized๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ๋Š” ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ์ฒ˜์Œ ๋ฟ์ด๊ณ  ๊ทธ ์ดํ›„์—๋Š” ์“ธ๋ฐ์—†์ด ์„ฑ๋Šฅ์„ ์ €ํ•˜์‹œํ‚ค๋Š” ์š”์ธ์ด๋‹ค.

C. Double-checking Locking

public class Singleton { private volatile static Singleton uniqueInstance; private Singleton() {} // ๋ถ€๋ถ„์ ์œผ๋กœ synchronized๋ฅผ ๊ฑธ์–ด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋œ ์ดํ›„์—๋Š” synchronized ๊ตฌ๋ฌธ์„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค. public static Singleton getInstance() { if ( uniqueInstance == null ) { synchronized( Singleton.class ) { if ( uniqueInstance == null ) { uniqueInstance = new Singleton(); } } } return uniqueInstance; } }

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

 Many patterns can be implemented using the Singleton pattern. See Abstract Factory, Builder, and Prototype.

 

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

  • Abstract Factory, Builder, and Prototype can use Singleton in their implementation.
  • Facade objects are often Singletons because only one Facade object is required.
  • State objects are often Singletons.
  • The advantage of Singleton over global variables is that you are absolutely sure of the number of instances when you use Singleton, and, you can change your mind and manage any number of instances.
  • The Singleton design pattern is one of the most inappropriately used patterns. Singletons are intended to be used when a class must have exactly one instance, no more, no less. Designers frequently use Singletons in a misguided attempt to replace global variables. A Singleton is, for intents and purposes, a global variable. The Singleton does not do away with the global; it merely renames it.
    ์‹ฑ๊ธ€ํ„ด ํŒจํ„ด์€ ๊ฐ€์žฅ ๋ถ€์ ์ ˆํ•˜๊ฒŒ ์‚ฌ์šฉ๋˜๋Š” ํŒจํ„ด์ค‘ ํ•˜๋‚˜์ด๋‹ค. ์›๋ž˜ ์‹ฑ๊ธ€ํ„ด ํŒจํ„ด์˜ ๋ชฉ์ ์€ ๋”๋„๋ง๊ณ  ๋œ๋„๋ง๊ณ  ์˜ค๋กœ์ง€ ๋‹จ ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ–๊ธฐ ์œ„ํ•จ์ด์—ˆ์œผ๋‚˜ ์ผ๋ถ€ ๊ฐœ๋ฐœ์ž๋“ค์€ ๋‹จ์ˆœํžˆ ์ „์—ญ๋ณ€์ˆ˜๋ฅผ ๋Œ€์ฒดํ•˜๊ธฐ์œ„ํ•ด ์‚ฌ์šฉํ•  ๋•Œ๊ฐ€ ์žˆ๋‹ค.
  • When is Singleton unnecessary? Short answer: most of the time. Long answer: when it's simpler to pass an object resource as a reference to the objects that need it, rather than letting objects access the resource globally. The real problem with Singletons is that they give you such a good excuse not to think carefully about the appropriate visibility of an object. Finding the right balance of exposure and protection for an object is critical for maintaining flexibility.
  • Our group had a bad habit of using global data, so I did a study group on Singleton. The next thing I know Singletons appeared everywhere and none of the problems related to global data went away. The answer to the global data question is not, "Make it a Singleton." The answer is, "Why in the hell are you using global data?" Changing the name doesn't change the problem. In fact, it may make it worse because it gives you the opportunity to say, "Well I'm not doing that, I'm doing this" โ€“ even though this and that are the same thing.

 

 

 

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

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

http://www.javajigi.net/display/SWD/ch05_singletonpattern

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 ) :

ยท A client asks a prototype to clone itself.


ํŒจํ„ด ์‚ฌ์šฉ์˜ ์žฅ๋‹จ์  ( 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.

 

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

  • Sometimes creational patterns are competitors: there are cases when either Prototype or Abstract Factory could be used properly. At other times they are complementory: Abstract Factory might store a set of Prototypes from which to clone and return product objects. Abstract Factory, Builder, and Prototype can use Singleton in their implementations.
  • Abstract Factory classes are often implemented with Factory Methods, but they can be implemented using Prototype.
  • Factory Method: creation through inheritance. Protoype: creation through delegation.
  • Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Protoype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed.
  • Prototype doesn't require subclassing, but it does require an "initialize" operation. Factory Method requires subclassing, but doesn't require Initialize.
  • Designs that make heavy use of the Composite and Decorator patterns often can benefit from Prototype as well.
  • Prototype co-opts one instance of a class for use as a breeder of all future instances.
  • Prototypes are useful when object initialization is expensive, and you anticipate few variations on the initialization parameters. In this context, Prototype can avoid expensive "creation from scratch", and support cheap cloning of a pre-initialized prototype.
  • Prototype is unique among the other creational patterns in that it doesn't require a class โ€“ only an object. Object-oriented languages like Self and Omega that do away with classes completely rely on prototypes for creating new objects.

 

 

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

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

 


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.

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

 

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

 

Factory Method Pattern Structure


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

 Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ํŒจํ„ด์€ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ •์˜ํ•˜๋Š”๋ฐ ์ด๋•Œ ์–ด๋–ค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•œ ๊ฒฐ์ •๊ถŒ์€ ์„œ๋ธŒํด๋ž˜์Šค์—๊ฒŒ ์œ„์ž„ํ•œ๋‹ค. 

 

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


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

์ž, ๊ทธ๋Ÿฐ๋ฐ ๋ฌธ์ œ๋Š” ํŠน์ • ๋ฌธ์„œ๋Š” ํŠน์ • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์˜์กด์ ์ด๋ผ๋Š” ์ ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, docx๋Š” ์›Œ๋“œ์—์„œ ์ƒ์„ฑํ•˜๋Š” ๋ฌธ์„œ์ด์ง€ ํ•œ๊ธ€์—์„œ ์ƒ์„ฑํ•˜๋Š” ๋ฌธ์„œ๊ฐ€ ์•„๋‹Œ ๊ฒƒ๊ณผ ๊ฐ™์€ ํ˜„์ƒ์ด๋ผ๊ณ  ๋ณด๋ฉด ๋œ๋‹ค. ๋”ฐ๋ผ์„œ Application์˜ CreateDocument()๋ฅผ ์ถ”์ƒ๋ฉ”์†Œ๋“œ๋กœ ์ •์˜ํ•˜๊ณ  ์ด๋ฅผ ์„œ๋ธŒํด๋ž˜์Šค์—์„œ ๊ตฌํ˜„ํ•˜๋„๋ก ํ•˜์—ฌ ๊ฐ๊ฐ์˜ Application๋งˆ๋‹ค ๊ฐ๊ฐ์˜ Document๋ฅผ ์ƒ์„ฑํ•˜๋„๋ก ํ•œ ๊ฒƒ์ด๋‹ค. ์ด๋•Œ CreateDocument()๋ฅผ ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. 

 

 

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

 Use the Factory Method pattern when


ยท a class can't anticipate the class of objects it must create.
ยท a class wants its subclasses to specify the objects it creates.
ยท classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.


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

ยท Product (Document)
o defines the interface of objects the factory method creates.
ยท ConcreteProduct (MyDocument)
o implements the Product interface.
ยท Creator (Application)
o declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object.
o may call the factory method to create a Product object.
ยท ConcreteCreator (MyApplication)
o overrides the factory method to return an instance of a ConcreteProduct.


์›๋ฆฌ ( Collaborations ) :

ยท Creator relies on its subclasses to define the factory method so that it returns an instance of the appropriate ConcreteProduct.


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

 Factory methods eliminate the need to bind application-specific classes into your code. The code only deals with the Product interface; therefore it can work with any user-defined ConcreteProduct classes. A potential disadvantage of factory methods is that clients might have to subclass the Creator class just to create a particular ConcreteProduct object. Subclassing is fine when the client has to subclass the Creator class anyway, but otherwise the client now must deal with another point of evolution. 

 

Here are two additional consequences of the Factory Method pattern:


1. Provides hooks for subclasses. Creating objects inside a class with a factory method is always more flexible than creating an object directly. Factory Method gives subclasses a hook for providing an extended version of an object.
In the Document example, the Document class could define a factory method called CreateFileDialog that creates a default file dialog object for opening an existing document. A Document subclass can define an application-specific file dialog by overriding this factory method. In this  case the factory method is not abstract but provides a reasonable default implementation.


2. Connects parallel class hierarchies. In the examples we've considered so far, the factory method is only called by Creators. But this doesn't have to be the case; clients can find factory methods useful, especially in the case of parallel class hierarchies.
Parallel class hierarchies result when a class delegates some of its responsibilities to a separate class. Consider graphical figures that can be manipulated interactively; that is, they can be stretched, moved, or rotated using the mouse. Implementing such interactions isn't always easy.
It often requires storing and updating information that records the state of the manipulation at a given time. This state is needed only during manipulation; therefore it needn't be kept in the figure object. Moreover, different figures behave differently when the user manipulates them. For example, stretching a line figure might have the effect of moving an endpoint, whereas stretching a text figure may change its line spacing. With these constraints, it's better to use a separate Manipulator object that implements the interaction and keeps track of any manipulation-specific state that's needed. Different figures will use different Manipulator subclasses to handle particular interactions. The resulting Manipulator class hierarchy parallels (at least partially) the Figure class hierarchy:


The Figure class provides a CreateManipulator factory method that lets clients create a Figure's corresponding Manipulator. Figure subclasses override this method to return an instance of the Manipulator subclass that's right for them. Alternatively, the Figure class may implement CreateManipulator to return a default Manipulator instance, and Figure subclasses may simply inherit that default. The Figure classes that do so need no corresponding Manipulator subclassโ€”hence the hierarchies are only partially parallel.
Notice how the factory method defines the connection between the two class hierarchies. It localizes knowledge of which classes belong together.

 

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

 Abstract Factory is often implemented with factory methods. The Motivation example in the Abstract Factory pattern illustrates Factory Method as well.
Factory methods are usually called within Template Methods. In the document example above, NewDocument is a template method.
Prototypes don't require subclassing Creator. However, they often require an Initialize operation on the Product class. Creator uses Initialize to initialize the object. Factory Method doesn't require such an operation.

 

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

 
  • Abstract Factory classes are often implemented with Factory Methods, but they can be implemented using Prototype.
  • Factory Methods are usually called within Template Methods.
  • Factory Method: creation through inheritance. Prototype: creation through delegation.
  • Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed.
  • Prototype doesn't require subclassing, but it does require an Initialize operation. Factory Method requires subclassing, but doesn't require Initialize.
  • The advantage of a Factory Method is that it can return the same instance multiple times, or can return a subclass rather than an object of that exact type.
  • Some Factory Method advocates recommend that as a matter of language design (or failing that, as a matter of style) absolutely all constructors should be private or protected. It's no one else's business whether a class manufactures a new object or recycles an old one.
  • The new operator considered harmful. There is a difference between requesting an object and creating one. The new operator always creates an object, and fails to encapsulate object creation. A Factory Method enforces that encapsulation, and allows an object to be requested without inextricable coupling to the act of creation.

 

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

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


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.

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

 

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

 

Abstract Factory Pattern Structure


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

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

์ถ”์ƒ ํŒฉํ† ๋ฆฌ ํŒจํ„ด์€ ๊ตฌ์ƒํด๋ž˜์Šค๋ฅผ ๋ช…์‹œํ•˜์ง€ ์•Š์•„๋„ ์—ฐ๊ด€์žˆ๊ฑฐ๋‚˜ ์˜์กด์ ์ธ ๊ฐ์ฒด์˜ ๊ทธ๋ฃน์„ ์ƒ์„ฑํ•˜๊ธฐ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ํŒจํ„ด์ด๋‹ค.

์ฐธ๊ณ ๋กœ Factory๋Š” ์‹ฑ๊ธ€ํ„ด ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ด๋‹ค. ( ๋™์ผํ•œ ์ œํ’ˆ์„ ์ƒ์‚ฐํ•˜๋Š” ๋‘๊ฐœ์˜ ๋‹ค๋ฅธ ๊ณต์žฅ์„ ํ•˜๋‚˜์˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๊ฐ€์ง€๊ณ  ์žˆ์„ ํ•„์š”๋Š” ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ) 

 

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



 GOF์˜ ๋””์ž์ธ ํŒจํ„ด์—์„œ ์˜ˆ๋กœ ๋“  ๋‚ด์šฉ์„ ์ข€ ๊พธ๋ฉฐ์„œ ์–˜๊ธฐํ•ด๋ณด๊ฒ ๋‹ค.  

์ž, ์šฐ๋ฆฌ ํšŒ์‚ฌ๋Š” ์—ฌ๋Ÿฌ๊ฐœ์˜ ์œ„์ ฏ์„ ์ œ๊ณตํ•˜๋Š”๋ฐ ์œ„์ ฏ๋งˆ๋‹ค Look and Feel ( ์ดํ•˜ LNF )์ด ๋‹ค๋ฅด๋‹ค. ์ง€๊ธˆ๊นŒ์ง€ Window์™€ ScrollBar๋ฅผ ๋‚ด๊ฐ€ ๋งŒ๋“ค์–ด ๋†จ๋Š”๋ฐ ์‹ ์ž…๊ฐœ๋ฐœ์ž๊ฐ€ ์ด๋ฅผ ์ด์šฉํ•ด์„œ ์œ„์ ฏ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๊ณ  ์‹ถ๋‹ค. ๊ทธ๋ž˜์„œ ๋‚˜๋Š” WidgetFactory๋ฅผ ๋งŒ๋“ค์–ด ์‹ ์ž…๊ฐœ๋ฐœ์ž๊ฐ€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•  ๊ฒƒ์ด๋‹ค. ๋‚ด๊ฐ€ ๋งŒ๋“  Window์™€ ScrollBar์˜ ๊ตฌ์ƒ ํด๋ž˜์Šค๋ฅผ ๋ชฐ๋ผ๋„ WidgetFactory์—์„œ ์•Œ์•„์„œ ์ƒ์„ฑํ•ด ์ค„ ํ…Œ๋‹ˆ ์‹ ์ž…๊ฐœ๋ฐœ์ž๋Š” WidgetFactory ์‚ฌ์šฉ๋ฒ•๋งŒ ์•Œ๋ฉด ๋˜๊ฒ ์ง€. ์‹ ์ž…๊ฐœ๋ฐœ์ž๊ฐ€ PMWidget์„ ๋งŒ๋“ค๊ณ  ์‹ถ์œผ๋ฉด PMWidgetFactory๋ฅผ ์ด์šฉํ•˜๋ฉด๋˜๊ณ  MotifWidget์„ ๋งŒ๋“ค๊ณ  ์‹ถ์œผ๋ฉด MotifWidgetFactory๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๋Š”๊ฑฐ๋‹ค. 


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

Use the Abstract Factory pattern when


ยท a system should be independent of how its products are created, composed, and represented.
ยท a system should be configured with one of multiple families of products.  

ยท a family of related product objects is designed to be used together, and you need to enforce this constraint.
ยท you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.

 

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

ยท AbstractFactory (WidgetFactory)
o declares an interface for operations that create abstract product objects.
ยท ConcreteFactory (MotifWidgetFactory, PMWidgetFactory)
o implements the operations to create concrete product objects.
ยท AbstractProduct (Window, ScrollBar)
o declares an interface for a type of product object.
ยท ConcreteProduct (MotifWindow, MotifScrollBar)
o defines a product object to be created by the corresponding concrete factory.
o implements the AbstractProduct interface.
ยท Client
o uses only interfaces declared by AbstractFactory and AbstractProduct classes.


์›๋ฆฌ ( Collaborations ) :

ยท Normally a single instance of a ConcreteFactory class is created at run-time.
This concrete factory creates product objects having a particular implementation. To create different product objects, clients should use a different concrete factory.
ยท AbstractFactory defers creation of product objects to its ConcreteFactory subclass.

 



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

The Abstract Factory pattern has the following benefits and liabilities:


1. It isolates concrete classes. The Abstract Factory pattern helps you control the classes of objects that an application creates. Because a factory encapsulates the responsibility and the process of creating product objects, it isolates clients from implementation classes. Clients manipulate instances through their abstract interfaces. Product class names are isolated in the implementation of the concrete factory; they do not appear in client code.  

 

2. It makes exchanging product families easy. The class of a concrete factory appears only once in an applicationโ€”that is, where it's instantiated. This makes it easy to change the concrete factory an application uses. It can use different product configurations simply by changing the concrete factory. Because an abstract factory creates a complete family of products, the whole product family changes at once. In our user interface example, we can switch from Motif widgets to Presentation Manager widgets simply by switching the corresponding factory objects and recreating the interface.


3. It promotes consistency among products. When product objects in a family are designed to work together, it's important that an application use objects from only one family at a time. AbstractFactory makes this easy to enforce.


4. Supporting new kinds of products is difficult. Extending abstract factories to produce new kinds of Products isn't easy. That's because the AbstractFactory interface fixes the set of products that can be created. Supporting new kinds of products requires extending the factory interface, which involves changing the AbstractFactory class and all of its subclasses

์ƒˆ ๋กœ์šด ์ข…๋ฅ˜์˜ ์ œํ’ˆ์„ ์ƒ์‚ฐํ•˜๋Š” ๊ฒƒ์ด ํž˜๋“ค๋‹ค. ์ƒˆ๋กœ์šด ์ œํ’ˆ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด AbstractFactory๊ฐ€ ๋ณ€๊ฒฝ์ด ๋˜๋ฉด ์ด๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ตฌ์ƒํด๋ž˜์Šค(concrete classes)๋“ค์ด ๋ชจ๋‘ ๋ณ€๊ฒฝ๋˜์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด์„œ Prototype-based Factory ( ํ”„๋กœํ† ํƒ€์ž… ํŒจํ„ด์„ ์ด์šฉํ•œ Factory )๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค.  

 

 

 

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

 AbstractFactory classes are often implemented with factory methods (Factory Method), but they can also be implemented using Prototype.
A concrete factory is often a singleton.

 

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

  • Sometimes creational patterns are competitors: there are cases when either Prototype or Abstract Factory could be used profitably. At other times they are complementary: Abstract Factory might store a set of Prototypes from which to clone and return product objects, Builder can use one of the other patterns to implement which components get built. Abstract Factory, Builder, and Prototype can use Singleton in their implementation.
  • Abstract Factory, Builder, and Prototype define a factory object that's responsible for knowing and creating the class of product objects, and make it a parameter of the system. Abstract Factory has the factory object producing objects of several classes. Builder has the factory object building a complex product incrementally using a correspondingly complex protocol. Prototype has the factory object (aka prototype) building a product by copying a prototype object.
  • Abstract Factory classes are often implemented with Factory Methods, but they can also be implemented using Prototype.
  • Abstract Factory can be used as an alternative to Facade to hide platform-specific classes.
  • Builder focuses on constructing a complex object step by step. Abstract Factory emphasizes a family of product objects (either simple or complex). Builder returns the product as a final step, but as far as the Abstract Factory is concerned, the product gets returned immediately.
  • Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed.

 

 

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

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


 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.

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

 

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

 

ํ…œ ํ”Œ๋ฆฟํ•˜๋ฉด ๋ญ๊ฐ€ ๋– ์˜ค๋ฅด๋Š”๊ฐ€? ์‚ฌ์ „์  ์˜๋ฏธ๋Š” ํ˜•ํŒ, ๊ฒฌ๋ณธ์ด๋‹ค. ์šฐ๋ฆฌ๊ฐ€ ์ผ์ƒ์ ์œผ๋กœ ์“ธ ๋•Œ ํ…œํ”Œ๋ฆฟ์ด๋ผ ํ•˜๋ฉด ๋ญ”๊ฐ€์˜ ๊ธฐ๋ณธ๊ตฌ์กฐ(?)๋ผ๊ณ  ์ƒ๊ฐ์„ ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์ด ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ๋Š” ๋ฐ”๋กœ ๊ทธ๋Ÿฐ ํ˜•ํŒ, ๊ฒฌ๋ณธ, ๊ธฐ๋ณธ๊ตฌ์กฐ์˜ ์—ญํ• ์„ ํ•˜๋Š” ๋ฉ”์†Œ๋“œ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด๋Ÿฐ ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜๋Š” ํŒจํ„ด์„ ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ ํŒจํ„ด์ด๋ผ๊ณ  ์ดํ•ดํ•˜๋ฉด ๋œ๋‹ค. ์ž ์•„๋ž˜ ๊ทธ๋ฆผ์„ ์‚ดํŽด๋ณด์ž.

 


 

 

์œ„ ๊ทธ๋ฆผ์€ ์†Œ๋ฐฉ๊ด€, ๋ชฉ์ˆ˜, ์šฐ์ฒด๋ถ€, ๊ด€๋ฆฌ์ž ๋“ฑ ๋‹ค์–‘ํ•œ ์ง์—…์˜ ์‚ฌ๋žŒ๋“ค์„ ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์—ˆ๋‹ค. ์ด ์‚ฌ๋žŒ๋“ค์€ ๊ณตํ†ต์ ์œผ๋กœ ์ผํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์ด๋ฏ€๋กœ Worker๋ผ๋Š” ํด๋ž˜์Šค์˜ ํ•˜์œ„๋กœ ๋งŒ๋“ค์—ˆ๊ณ  ๋ชจ๋“  ์‚ฌ๋žŒ๋“ค์€ ๋™์ผํ•œ ์ผ๊ณผ ์ˆœ์„œ๊ฐ€ ์žˆ๋‹ค. ์ด ์ผ๊ณผ ์ˆœ์„œ๋Š” DailyRoutine()๋‚ด์— ์ •์˜๋˜์–ด์žˆ๊ณ  Worker ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•˜๋Š” ํด๋ผ์ด์–ธํŠธ์—์„œ๋Š” DailyRoutine()๋งŒ ํ˜ธ์ถœํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ด Worker๊ฐ€ ์†Œ๋ฐฉ๊ด€์ด๋ƒ, ์šฐ์ฒด๋ถ€๋ƒ์— ๋”ฐ๋ผ work()์—์„œ ํ•˜๋Š” ์ผ์ด ๋‹ฌ๋ผ์ง„๋‹ค. ์œ„ ๊ทธ๋ฆผ์—์„œ๋ณด๋ฉด ๋ชจ๋“  ์‚ฌ๋žŒ๋“ค์ด work()๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•ด์„œ ์žฌ์ •์˜ํ•˜๊ณ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ์—ฌ๊ธฐ์„œ DailyRoutine() ๋ฉ”์†Œ๋“œ๊ฐ€ ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ๊ฐ€ ๋˜๋Š” ๊ฒƒ์ด๊ณ  ์ด๋Ÿฐ ์‹์˜ ํŒจํ„ด์„ ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ ํŒจํ„ด์ด๋ผ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 

 

Template Method Pattern Structure

 


 


ํŒจํ„ด์˜ ๋ชฉ์  ( Intent ) :
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.


์–ด๋–ค ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๋ผˆ๋Œ€๋ฅผ ์ •์˜ํ•˜๊ณ  ๊ณผ์ •์€ ํ•˜์œ„ํด๋ž˜์Šค๊ฐ€ ์ •์˜ํ•˜๊ฒŒ๋” ํ•œ๋‹ค. ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ๋Š” ํ•˜์œ„ํด๋ž˜์Šค๋“ค์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ตฌ์กฐ์— ๋ณ€ํ™”๋ฅผ ์ฃผ์ง€ ์•Š๋Š” ์„ ์—์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋‚ด์˜ ํŠน์ • ๊ณผ์ •๋งŒ ์žฌ์ •์˜ ํ•  ์ˆ˜ ์žˆ๊ฒŒ๋” ํ•ด์ค€๋‹ค.

 

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



 ์›Œ ๋“œ ํ”„๋กœ์„ธ์„œ๋ฅผ ์˜ˆ๋ฅผ๋“ค์–ด ์„ค๋ช…ํ•˜์ž๋ฉด Document(๋ฌธ์„œ)ํŒŒ์ผ๊ณผ Application(์›Œ๋“œํ”„๋กœ์„ธ์„œ)๊ฐ€ ์žˆ๋Š”๋ฐ ์ด ์›Œ๋“œํ”„๋กœ์„ธ์„œ๊ฐ€ ๋ฌธ์„œ๋ฅผ ์—ด์–ด๋ณธ๋‹ค๋Š” ์‹œ๋‚˜๋ฆฌ์˜ค๋ฅผ ์ƒ๊ฐํ•ด๋ณด์ž. OpenDocument()๋Š” ๋ฌธ์„œ๊ฐ€ ์—ด์ˆ˜ ์žˆ๋Š” ๋ฌธ์„œ์ธ์ง€(์ฝ์–ด๋“ค์ผ ์ˆ˜ ์žˆ๋Š”์ง€) ๊ฒ€์‚ฌ๋ฅผ ํ•  ๊ฒƒ์ด๊ณ , ์›Œ๋“œํ”„๋กœ์„ธ์„œ๋‚ด์—์„œ ์ •์˜ํ•œ Document๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ๋ฌธ์„œ๋ฅผ ์ฝ์–ด๋“ค์—ฌ ๊ฐ์ฒด์— ์ €์žฅํ•  ๊ฒƒ์ด๋‹ค.  

We call OpenDocument a template method. A template method defines an algorithm in terms of abstract operations that subclasses override to provide concrete behavior. Application subclasses define the steps of the algorithm that check if the document can be opened (CanOpenDocument) and that create the Document (DoCreateDocument). Document classes define the step that reads the document (DoRead). The template method also defines an operation that lets Application subclasses know when the document is about to be opened (AboutToOpenDocument), in case they care.  

By defining some of the steps of an algorithm using abstract operations, the template method fixes their ordering, but it lets Application and Document subclasses vary those steps to suit their needs.

 

์—ฌ๊ธฐ์„œ ์ด OpenDocument()๋Š” ํ•˜๋‚˜์˜ ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ์ด๋‹ค. ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ๋Š” ๋ง๊ทธ๋Œ€๋กœ ํŠน์ • ๋™์ž‘์„ ํ•˜๊ธฐ์œ„ํ•œ ๊ณจ๊ฒฉ๋งŒ ์ œ๊ณตํ•œ๋‹ค. ๊ณจ๊ฒฉ๋‚ด๋ถ€์˜ ๊ตฌ์„ฑ์€ ํ•˜์œ„ํด๋ž˜์Šค๋“ค์ด ํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 

 

 

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

The Template Method pattern should be used

ยท to implement the invariant parts of an algorithm once and leave it up to subclasses to implement the behavior that can vary.

ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ ํŒจํ„ด์€ ๋ณ€ํ•˜์ง€ ์•Š์„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ตฌ์กฐ๋งŒ ์ •์˜ํ•˜๊ณ  ๊ตฌ์ฒด์ ์ธ ๊ตฌํ˜„์€ ํ•˜์œ„ํด๋ž˜์Šค๋“ค์—๊ฒŒ ์œ„์ž„ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. 

 

ยท when common behavior among subclasses should be factored and localized in a common class to avoid code duplication. This is a good example of"refactoring to generalize" as described by Opdyke and Johnson.

You first identify the differences in the existing code and then separate the differences into new operations. Finally, you replace the differing code with a template method that calls one of these new operations.

ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ ํŒจํ„ด์€ ์—ฌ๋Ÿฌ ํ•˜์œ„ ํด๋ž˜์Šค๋“ค ์ค‘์—์„œ ์ค‘๋ณต๋œ ๋ถ€๋ถ„์„ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋„ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค.


ยท to control subclasses extensions. You can define a template method that calls "hook" operations (see Consequences) at specific points, thereby permitting extensions only at those points.

ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ ํŒจํ„ด์€ ํ•˜์œ„ ํด๋ž˜์Šค๋“ค์˜ ํ™•์žฅ์„ ์ปจํŠธ๋กคํ•˜๊ณ ์ž ํ•  ๋•Œ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ํ…œํ”Œ๋ฆฟ ๋ฉ”์†Œ๋“œ์— ํŠน์ •์‹œ์ ์— hook๋ฅผ ํ˜ธ์ถœํ•˜๋„๋ก ์ •์˜ํ•˜์—ฌ ๊ทธ ํŠน์ • ์‹œ์ ์—๋งŒ ํ™•์žฅ์„ ํ—ˆ์šฉํ•˜๋„๋ก ํ•  ์ˆ˜๋„ ์žˆ๋‹ค. ( ๋ฌด์Šจ ๋ง์ธ์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค. ์•„๋ž˜์ชฝ์— Consequences์—์„œ hook๊ฐ€ ๋ญ”์ง€ ๋” ์•Œ์•„๋ณด์ž ) 

 

 

๋“ฑ์žฅ ์ธ๋ฌผ ( Participants ) :
ยท AbstractClass (Application)
o defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm.
o implements a template method defining the skeleton of an algorithm.The template method calls primitive operations as well as operationsdefined in AbstractClass or those of other objects.
ยท ConcreteClass (MyApplication)
o implements the primitive operations to carry out subclass-specific steps of the algorithm.

 

์›๋ฆฌ ( Collaborations ) :

ConcreteClass relies on AbstractClass to implement the invariant steps of the algorithm.


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

Template methods are a fundamental technique for code reuse. They are particularly important in class libraries, because they are the means for factoring out common behavior in library classes.
Template methods lead to an inverted control structure that's sometimes referred to as "the Hollywood principle," that is, "Don't call us, we'll call you" .
This refers to how a parent class calls the operations of a subclass and not the other way around.

Template methods call the following kinds of operations:
ยท concrete operations (either on the ConcreteClass or onclient classes);
ยท concrete AbstractClass operations (i.e., operations that are generally useful to subclasses);
ยท primitive operations (i.e., abstract operations);
ยท factory methods (see Factory Method); and
ยท hook operations, which provide default behavior that subclasses can extend if necessary. A hook operation often doesnothing by default. It's important for template methods to specify which operations arehooks (may be overridden) and which are abstract operations(must be  overridden). To reuse an abstract class effectively,subclass writers must understand which operations are designed foroverriding.
A subclass can extend a parent class operation's behavior byoverriding the operation and calling the parent operation explicitly:


void DerivedClass::Operation () {
// DerivedClass extended behavior
ParentClass::Operation();
}

 

Unfortunately, it's easy to forget to call the inherited operation.We can transform such an operation into a template method to give the parent control over how subclasses extend it. The idea is to call a hook operation from a template method in the parent class.Then subclasses can then override this hook operation:


void ParentClass::Operation () {
// ParentClass behavior
HookOperation();
}


HookOperation does nothing in ParentClass:


void ParentClass::HookOperation () { }


Subclasses override HookOperation to extend its behavior:


void DerivedClass::HookOperation () {
// derived class extension
}


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

Factory Methods are often called by template methods. In the Motivation example, the factory method DoCreateDocument is called by the template method OpenDocument.
Strategy : Template methods use inheritance to vary part of an algorithm. Strategies use delegation to vary the entire algorithm.

 

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

  • Strategy is like Template Method except in its granularity.
  • Template Method uses inheritance to vary part of an algorithm. Strategy uses delegation to vary the entire algorithm.
  • Strategy modifies the logic of individual objects. Template Method modifies the logic of an entire class.
  • Factory Method is a specialization of Template Method.

 

 

 

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

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


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.

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

 

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

 

State Pattern Structure

 

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

Allow an object to alter its behavior when its internal state changes.The object will appear to change its class.

State ํŒจํ„ด์˜ ๋ชฉ์ ์€ ๋‚ด๋ถ€ ์ƒํƒœ์˜ ๋ณ€ํ™”์— ๋”ฐ๋ผ ๊ฐ์ฒด์˜ ํ–‰์œ„๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค.


 ์œ„ ๋‹ค์ด์–ด๊ทธ๋žจ์„ ์˜ˆ์ œ๋กœ ๋“ค๋ฉด TCPConnection์€ TCPState๊ฐ์ฒด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํด๋ž˜์Šค๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด TCPState๊ฐ์ฒด๋Š” ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํด๋ž˜์Šค๋กœ ํฌ๊ฒŒ ์„ธ๊ฐ€์ง€ ์ƒํƒœ๋กœ ๋‚˜๋‰˜๋Š”๋ฐ ( Established, Listen, Closed ) ์ด ์ƒํƒœ์˜ ๋ณ€ํ™”์— ๋”ฐ๋ผ TCPConnection์—์„œ ํ˜ธ์ถœํ•˜๋Š” open()๋ฉ”์†Œ๋“œ๊ฐ€ TCPEstablished๊ฐ์ฒด์˜ open()์ด ๋  ์ˆ˜๋„ ์žˆ๊ณ  TCPListen๋‚˜  TCPClosed ๊ฐ์ฒด์˜ open()์ด ๋  ์ˆ˜๋„ ์žˆ๋‹ค.  

 

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

ยท An object's behavior depends on its state, and it must change its behavior at run-time depending on that state.

๊ฐ์ฒด์˜ ํ–‰์œ„๊ฐ€ ์ƒํƒœ์— ๋”ฐ๋ผ ๋™์ ์œผ๋กœ ๋ณ€ํ•ด์•ผ ํ•  ๋•Œ
ยท Operations have large, multipart conditional statements that depend on the object's state. This state is usually represented by one or more enumerated constants. Often, several operations will contain this same conditional structure. The State pattern puts each branch of the conditional in a
separate class. This lets you treat the object's state as an object in its own right that can vary independently from other objects.

์–ด๋–ค ์ž‘์—…์ด ๊ฐ์ฒด์˜ ์ƒํƒœ์— ์˜์กดํ•˜๋Š” ์—ฌ๋Ÿฌ๊ฐœ์˜ ํฐ ์กฐ๊ฑด๋ฌธ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๊ณ  ํ•ด๋ณด์ž( ์‰ฝ๊ฒŒ ๋งํ•ด ๊ฐ์ฒด์˜ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ƒ์ˆ˜๊ฐ’์ด ์กฐ๊ฑด๋ฌธ์˜ ์กฐ๊ฑด์ ˆ์— ๋“ค์–ด๊ฐ€ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž ). ์ผ๋ฐ˜์ ์œผ๋กœ ์ƒํƒœ๋ผ๋Š” ๊ฒƒ์€ ์ตœ์†Œ ํ•˜๋‚˜ ์ด์ƒ์˜ ์ƒ์ˆ˜๊ฐ’์— ์˜ํ•ด ํ‘œํ˜„์ด ๋  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด๋ฅผ ์ƒํƒœํŒจํ„ด์„ ์ด์šฉํ•˜์—ฌ ํด๋ž˜์Šค์— ๋‹ด์•„๋ฒ„๋ฆฌ๋Š” ๊ฒƒ์ด๋‹ค. ์ฆ‰, ๊ฐ์ฒด์˜ ์ƒํƒœ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด ๋ฒ„๋ฆฌ๋Š” ๊ฒƒ์ด๋‹ค.

 

๋“ฑ์žฅ ์ธ๋ฌผ :
ยท Context (TCPConnection)
o defines the interface of interest to clients.
o maintains an instance of a Concrete State subclass that defines the current state.

ยท State (TCPState)
o defines an interface for encapsulating the behavior associated with a particular state of the Context. 

 

ยท ConcreteState subclasses (TCPEstablished, TCPListen, TCPClosed)
o each subclass implements a behavior associated with a state of the Context.

 

์›๋ฆฌ ( Collaborations ) :

ยท Context delegates state-specific requests to the current ConcreteState object.

Context๋Š” ์ƒํƒœ์— ๋”ฐ๋ฅธ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ์„ ConcreteState๊ฐ์ฒด์— ์œ„์ž„ํ•œ๋‹ค.
ยท A context may pass itself as an argument to the State object handling the request. This lets the State object access the context if necessary.

์ƒํƒœ๊ฐ์ฒด๊ฐ€ Context๋ฅผ ํ•„์š”๋กœํ•œ๋‹ค๋ฉด Context๊ฐ์ฒด๋Š” ์ž์‹ ์„ argument๋กœ ๋„˜๊ฒจ์ฃผ์–ด ์ƒํƒœ๊ฐ์ฒด์—์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•  ์ˆ˜ ์žˆ๋‹ค.
ยท Context is the primary interface for clients. Clients can configure a context with State objects. Once a context is configured, its clients don't have to deal with the State objects directly.

ํด๋ผ์ด์–ธํŠธ๋Š” ์ƒํƒœ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•ด์„œ context๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ํ•œ๋ฒˆ ์„ค์ •ํ•˜๋ฉด ๊ทธ ๋‹ค์Œ๋ถ€ํ„ฐ๋Š” ์ƒํƒœ๊ฐ์ฒด์— ์ง์ ‘์ ์œผ๋กœ ์ ‘๊ทผํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.
ยท Either Context or the ConcreteState subclasses can decide which state succeeds another and under what circumstances.

Context ๋˜๋Š” ConcreteState์˜ ํ•˜์œ„ ํด๋ž˜์Šค๋“ค์€ ์ƒํƒœ์˜ ์ˆœ์„œ๋ฅผ ๊ฒฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.


ํŒจํ„ด ์‚ฌ์šฉ์˜ ์žฅ๋‹จ์  ( Consequences ):
1. It localizes state-specific behavior and partitions behavior for different states.The State pattern puts all behavior associated with a particular state into one object. Because all state-specific code lives in a State subclass, new states and transitions can be added easily by defining new subclasses.
An alternative is to use data values to define internal states and have Context operations check the data explicitly. But then we'd have look-alike conditional or case statements scattered throughout Context's implementation. Adding a new state could require changing several operations, which complicates maintenance.

The State pattern avoids this problem but might introduce another, because the pattern distributes behavior for different states across several State subclasses. This increases the number of classes and is less compact than a single class. But such distribution is actually good if there are many states, which would otherwise necessitate large conditional statements.
Like long procedures, large conditional statements are undesirable.They're monolithic and tend to make the code less explicit, which in turn makes them difficult to modify and extend. The State pattern offers a better way to structure state-specific code. The logic that determines the state transitions doesn't reside in monolithic if or switch statements but instead is partitioned between the State  subclasses. Encapsulating each state transition and action in a class elevates the idea of an execution state to full object status. That imposes structure on the code and makes its intent clearer. 

 

2. It makes state transitions explicit.When an object defines its current state solely in terms of internal data values, its state transitions have no explicit representation;they only show up as assignments to some variables. Introducing separate objects for different states makes the transitions more explicit. Also, State objects can protect the Context from inconsistent internal states, because state transitions are atomic from the Context's perspectiveโ€”they happen by rebinding one variable (the Context's State object variable), not several. 

 

3. State objects can be shared.If State objects have no instance variablesโ€”that is, the state they represent is encoded entirely in their typeโ€”then contexts can sharea State object. When states are shared in this way, they are essentially flyweights (see Flyweight) with nointrinsic state, only behavior.


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

The Flyweight pattern explains when and how State objects can be shared.

Flyweight ํŒจํ„ด์€ ์–ธ์ œ ์–ด๋–ป๊ฒŒ ์ƒํƒœ ๊ฐ์ฒด๋“ค์ด ๊ณต์œ ๋˜๋Š”์ง€ ์„ค๋ช…ํ•ด์ค€๋‹ค.

State objects are often Singletons.

์ƒํƒœ ๊ฐ์ฒด๋“ค์€ ๋Œ€๋ถ€๋ถ„ ์‹ฑ๊ธ€ํ„ด์ด๋‹ค.



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

  • State objects are often Singletons.
  • Flyweight explains when and how State objects can be shared.
  • Interpreter can use State to define parsing contexts.
  • Strategy has 2 different implementations, the first is similar to State. The difference is in binding times (Strategy is a bind-once pattern, whereas State is more dynamic).
  • The structure of State and Bridge are identical (except that Bridge admits hierarchies of envelope classes, whereas State allows only one). The two patterns use the same structure to solve different problems: State allows an object's behavior to change along with its state, while Bridge's intent is to decouple an abstraction from its implementation so that the two can vary independently.
  • The implementation of the State pattern builds on the Strategy pattern. The difference between State and Strategy is in the intent. With Strategy, the choice of algorithm is fairly stable. With State, a change in the state of the "context" object causes it to select from its "palette" of Strategy objects.

 

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

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


 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.

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

 

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

 

์šฐ์„  ์‰ฌ์šด ์ดํ•ด๋ฅผ ์œ„ํ•ด์„œ ์•„๋ž˜ ์˜ˆ์ œ๋ถ€ํ„ฐ ๋ณด์ž.

Example

๊ณตํ•ญ์œผ๋กœ ๊ฐ€๋Š” ๊ตํ†ต์ˆ˜๋‹จ์ด ์„ธ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.  ๋ฒ„์Šค, ์ž๋™์ฐจ, ๊ทธ๋ฆฌ๊ณ  ํƒ์‹œ. ์šฐ๋ฆฌ๊ฐ€ ์–ด๋–ค ๊ฒƒ์„ ํƒ€๊ณ ๊ฐˆ ์ง€๋Š” ๊ฐ€๊ฒฉ, ์ด๋™ํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„, ๋ฐฐ์ฐจ์‹œ๊ฐ„, ๊ธฐ๋ฆ„๊ฐ’ ๋“ฑ๋“ฑ์ด ๋  ๊ฒƒ์ด๋‹ค. ์ด๋Ÿฐ๊ฒƒ์„ ๋กœ์ง์œผ๋กœ ๊ตฌํ˜„ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž. ์ „๋žตํŒจํ„ด์„ ๊ณต๋ถ€ํ•˜๋Š” ์‹œ๊ฐ„์ด๋‹ˆ ์ „๋žต์ด๋ž€ ๋ง์„ ์จ์„œ ๊ฐ๊ฐ์˜ ๊ตํ†ต์ˆ˜๋‹จ์„ ํ•˜๋‚˜์˜ ์ „๋žต์ด๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ ์ „๋žต์˜ ๊ณตํ†ต๋ถ„๋ชจ๋ฅผ ๋ชจ์•„์„œ Strategy๋ผ๋Š” ํด๋ž˜์Šค์•ˆ์— ๋„ฃ์—ˆ๋‹ค. ์ด๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ทธ๋ฆผ์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. TransportationToAirportํด๋ž˜์Šค๋Š” ๋‹น์‹ ์˜ ๋‘๋‡Œ๋ผ๊ณ  ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์–ด๋–ค ์ „๋žต์„ ์„ ํƒํ•  ์ง€ ๊ฒฐ์ •์ง“๋Š” ๋กœ์ง์ด ๋“ค์–ด์žˆ๋Š” ๊ณณ์ด ๋ ํ…Œ๋‹ˆ ๋ง์ด๋‹ค. ์ „๋žตํŒจํ„ด์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งํ•˜์ž๋ฉด ์ด๋Ÿฐ ์‹์˜ ํŒจํ„ด์ธ๊ฑฐ๋‹ค. ์ข€ ๋” ์ž์„ธํ•œ ์ด๋ก ์ ์ธ ์–˜๊ธฐ๋“ค์€ ์•„๋ž˜์—์„œ ์‚ดํŽด๋ณด์ž. 

 

 

Strategy Pattern Structure

 

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

Define a family of algorithms, encapsulate each one, and make them interchangeable.
Strategy lets the algorithm vary independently from clients that use it.


์ „๋žต ํŒจํ„ด์€ ๊ต์ฒด๊ฐ€ ๊ฐ€๋Šฅํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ทธ๋ฃน์„ ์ •์˜ํ•˜๊ณ  ๊ฐ๊ฐ์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์บก์Šํ™”ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์ „๋žต์€ ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋…๋ฆฝ์ ์ด๊ณ  ๋‹ค์–‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค€๋‹ค.



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

 

Suppose a Composition class is responsible for maintaining and updating the linebreaks of text displayed in a text viewer. Linebreaking strategies aren't implemented by the class Composition. Instead, they are implemented separately by subclasses of the abstract Compositor class. Compositor subclasses implement different strategies:

ยท SimpleCompositor implements a simple strategy that determines linebreaks one at a time.
ยทโ€‹ TeXCompositor implements the TeX algorithm for finding linebreaks. This strategy tries to optimize linebreaks globally, that is, one paragraph at a time.
ยท ArrayCompositor implements a strategy that selects breaks so that each row has a fixed number of items. It's useful for breaking a collection of icons into rows, for example. 

 

A Composition maintains a reference to a Compositor object. Whenever a Composition reformats its text, it forwards this responsibility to its Compositor object. The client of Composition specifies which Compositor should be used by installing the Compositor it desires into the Composition.

 

 

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

ยท many related classes differ only in their behavior. Strategies provide a way to configure a class with one of many behaviors.

์—ฐ๊ด€๋œ ๋งŽ์€ ํด๋ž˜์Šค๋“ค์ด ํ–‰ํ•˜๋Š” ํ–‰์œ„์— ์žˆ์–ด์„œ๋งŒ ๋‹ค๋ฅผ ๋•Œ


ยท you need different variants of an algorithm. For example, you might define algorithms  reflecting different space/time trade-offs.Strategies can be used when these variants are  implemented as a class hierarchy of algorithms.

ํด๋ž˜์Šค๊ณ„์ธต์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋‹ค์–‘ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ํ•„์š”ํ•  ๋•Œ ์ „๋žตํŒจํ„ด์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.


ยท an algorithm uses data that clients shouldn't know about. Use the Strategy pattern to avoid exposing complex, algorithm-specific data structures.

๋ณต์žกํ•˜๊ณ  ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ํŠนํ™”๋œ ๋ฐ์ดํƒ€๊ตฌ์กฐ๋ฅผ ๋…ธ์ถœ์‹œํ‚ค๊ณ  ์‹ถ์ง€ ์•Š์œผ๋ฉด ์ „๋žตํŒจํ„ด์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.


ยท a class defines many behaviors, and these appear as multiple conditional statements in its operations. Instead of many conditionals, move related conditional branches into their own Strategy class.

ํ•œ ํด๋ž˜์Šค๊ฐ€ ์—ฌ๋Ÿฌ ํ–‰์œ„๋ฅผ ์ •์˜ํ•˜๊ณ  ์ˆ˜๋งŽ์€ ์กฐ๊ฑด์ ˆ์— ์˜ํ•ด ํŠน์ • ํ–‰์œ„๋ฅผ ํ•ด์•ผํ•  ๋•Œ๊ฐ€ ์žˆ๋‹ค. ์ด๋Ÿด ๋•Œ๋Š” ๋งŽ์€ ์กฐ๊ฑด์ ˆ๋Œ€์‹ ์— ์—ฐ๊ด€์žˆ๋Š” ์กฐ๊ฑด์ ˆ๋“ค์„ ์ „๋žต ํด๋ž˜์Šค๋กœ ๋ฌถ์–ด๋ฒ„๋ ค๋ผ. 

 

 

๋“ฑ์žฅ ์ธ๋ฌผ ( Participants ) :
ยท Strategy (Compositor)
o declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.

์ง€์›๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ๊ณตํ†ต ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ •์˜ํ•œ๋‹ค. Context๊ฐ€ ConcreteStrategy์— ์˜ํ•ด ์ •์˜๋œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ด์šฉํ•œ๋‹ค. 

 

ยท ConcreteStrategy (SimpleCompositor, TeXCompositor,ArrayCompositor)  

o implements the algorithm using the Strategy interface.

Strategy ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ด์šฉํ•ด์„œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ•œ๋‹ค.


ยท Context (Composition)
o is configured with a ConcreteStrategy object.

o maintains a reference to a Strategy object.  

Strategy๊ฐ์ฒด๋กœ์˜ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์œ ์ง€ํ•œ๋‹ค.
o may define an interface that lets Strategy access its data.  

Strategy๊ฐ€ Context์˜ ๋ฐ์ดํƒ€๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ •์˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

์›๋ฆฌ ( Collaborations ) :

ยท Strategy and Context interact to implement the chosen algorithm. A context may pass all data required by the algorithm to the strategy when the algorithm is called. Alternatively, the context can pass itself as an argument to Strategy operations. That lets the strategy call back on the context as required.
ยท A context forwards requests from its clients to its strategy. Clients usually create and pass a ConcreteStrategy object to the context; thereafter, clients interact with the context exclusively. There is often a family of ConcreteStrategy classes for a client to choose from.


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

1. Families of related algorithms. Hierarchies of Strategy classes define a family of algorithms  or behaviors for contexts to reuse. Inheritance can help factor out common functionality of the algorithms.

์ „๋žตํŒจํ„ด์€ ์—ฐ๊ด€๋œ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ทธ๋ฃน์„ ์ƒ์†์„ ํ†ตํ•ด ํด๋ž˜์Šค๊ณ„์ธต์œผ๋กœ ๋งŒ๋“ค์–ด ์ด๋ฅผ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.  

 

2. An alternative to subclassing. Inheritance offers another way to support a variety of algorithms or behaviors. You can subclass a Context class directly to give it different behaviors. But this hard-wires the behavior into Context. It mixes the algorithm implementation with Context's, making Context harder to understand, maintain, and extend. And you can't vary the algorithm dynamically. You wind up with many related classes whose only difference is the algorithm or behavior they employ. Encapsulating the algorithm in separate Strategy classes lets you vary the algorithm independently of its context, making it easier to switch, understand, and extend.


3. Strategies eliminate conditional statements. The Strategy pattern offers an alternative to conditional statements for selecting desired behavior. When different behaviors are lumped into one class, it's hard to avoid using conditional statements to select the right behavior. Encapsulating the behavior in separate Strategy classes eliminates these conditional statements.
Code containing many conditional statements often indicatesthe need to apply the Strategy pattern.


4. A choice of implementations. Strategies can provide different implementations of the  same behavior. The client can choose among strategies with different time and space trade-offs.


5. Clients must be aware of different Strategies.The pattern has a potential drawback in that a client must understand how Strategies differ before it can select the appropriate one. Clients might be exposed to implementation issues. Therefore you should use the Strategy pattern only when the variation in behavior is relevant to clients.


6. Communication overhead between Strategy and Context. The Strategy interface is shared by all ConcreteStrategy classes whether the algorithms they implement are trivial or complex. Hence it's likely that some ConcreteStrategies won't use all the information passed to them through this interface; simple ConcreteStrategies may use none of it! That means there will be times when the context creates and initializes parameters that never get used. If this is an issue, then you'll need tighter coupling between Strategy and Context.


7. Increased number of objects. Strategies increase the number of objects in an application. Sometimes you can reduce this overhead by implementing strategies as stateless objects that contexts can share. Any residual state is maintained by the context, which passes it in each request to the Strategy object. Shared strategies should not maintain state across invocations. The Flyweight pattern describes this approach in more detail.


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

Flyweight

 

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

  • Strategy is like Template Method except in its granularity.
    Strategy ํŒจํ„ด์€ Template Method ํŒจํ„ด๊ณผ ๋น„์Šทํ•˜๋‹ค. ( granularity๋ฅผ ์ œ์™ธํ•˜๋ฉด )
    ( granularity๋ฅผ ํ…Œํฌ๋†€๋กœ์ง€์ปฌํ•˜๊ฒŒ ๋ญ๋ผ๊ณ  ํ•ด์„ํ•ด์•ผํ•  ์ง€ ๋ชจ๋ฅด๊ฒ ๋„ค์š”.)

  • State is like Strategy except in its intent.
    State ํŒจํ„ด์€ Strategy ํŒจํ„ด๊ณผ ๋น„์Šทํ•˜๋‹ค. ( intent๋ฅผ ์ œ์™ธํ•˜๋ฉด )
  • Strategy lets you change the guts of an object. Decorator lets you change the skin.
    Strategy ํŒจํ„ด์„ ์ด์šฉํ•ด์„œ ๊ฐ์ฒด๋‚ด๋ถ€๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด, Decorator ํŒจํ„ด์€ ๊ฐ์ฒด๊ป๋ฐ๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.
  • State, Strategy, Bridge (and to some degree Adapter) have similar solution structures. They all share elements of the 'handle/body' idiom. They differ in intent - that is, they solve different problems.
    State, Strategy, Bridge ํŒจํ„ด๋“ค์€ ๋ชจ๋‘ ๋น„์Šทํ•œ ๊ตฌ์กฐ๋ฅผ ๊ฐ–๊ณ  ์žˆ์ง€๋งŒ, ํŒจํ„ด์˜ ๋ชฉ์ (intent)๊ฐ€ ๋‹ค๋ฅด๋‹ค. ์ฆ‰, ์„œ๋กœ ๋‹ค๋ฅธ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‚˜์˜จ ํŒจํ„ด๋“ค์ด๋ผ๋Š” ์–˜๊ธฐ๋‹ค.
  • Strategy has 2 different implementations, the first is similar to State. The difference is in binding times (Strategy is a bind-once pattern, whereas State is more dynamic).
    Strategy ํŒจํ„ด์ด ์ •์ ์œผ๋กœ ํ•œ๋ฒˆ ๋ฐ”์ธ๋”ฉํ•˜๋Š” ๋ฐ˜๋ฉด State ํŒจํ„ด์€ ๋™์ ์œผ๋กœ ๋ฐ”์ธ๋”ฉํ•œ๋‹ค.
  • Strategy objects often make good Flyweights.
    Strategy ๊ฐ์ฒด๋“ค์€ ๋ณดํ†ต ์ข‹์€ Filyweights๋ฅผ ๋งŒ๋“ ๋‹ค. ( Flyweight ํŒจํ„ด์„ ๊ณต๋ถ€ํ•˜๋ฉด ๋ฌด์Šจ ๋ง์ธ์ง€ ์ดํ•ด๊ฐ€ ๋  ๋“ฏ ์‹ถ๋„ค์š” )

 

 

 

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

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


- ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ ํ•˜์œ„์˜ ๋ชจ๋“  file ํƒ€์ž… ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ

find . -type f|wc -l




- ํ˜„์žฌ ๋””๋ ‰ํ† ๋ฆฌ ํ•˜์œ„์˜ ๋ชจ๋“  ๋””๋ ‰ํ† ๋ฆฌ์—์„œ ํŒŒ์ผ ํ™•์žฅ์ž๊ฐ€ class์ธ ํŒŒ์ผ ๊ฐœ์ˆ˜ ๊ตฌํ•˜๊ธฐ
find . -name '*.class' | wc -l

1. ํƒ€์ž…์ด ํŒŒ์ผ์ธ ๊ฒƒ๋“ค ์ค‘ ํŠน์ • ์ •๊ทœ์‹์— ํ•ด๋‹น๋˜๋Š” ํŒŒ์ผ ์กฐํšŒํ•˜๊ธฐ

find . -type f -regex '/ex/'



2. ํƒ€์ž…์ด ํŒŒ์ผ์ธ ๊ฒƒ๋“ค ์ค‘์—์„œ ํŠน์ • ์ •๊ทœ์‹์— ํ•ด๋‹น๋˜๋Š” ํŒŒ์ผ์„ ์‚ญ์ œํ•˜๊ธฐ

find . -type f -regex '/ex/' -exec rm {} \;



3. ํ™•์žฅ์ž๊ฐ€ class๊ฐ€ ์•„๋‹Œ ํŒŒ์ผํƒ€์ž… ์‚ญ์ œํ•˜๊ธฐ

find . -type f ! -name '*.class' -exec rm {} \;



4. ํ™•์žฅ์ž๊ฐ€ class๊ฐ€ ์•„๋‹ˆ๊ณ  java๋„ ์•„๋‹Œ ํŒŒ์ผ ํƒ€์ž… ์‚ญ์ œํ•˜๊ธฐ

find ./online/ -type f ! -name '*.class' ! -name '*.java' -exec rm {} \;



๊น”๋”ํ•˜๊ฒŒ ์ •๋ฆฌ๋˜์–ด์žˆ๋Š” ์ž๋ฃŒ๊ฐ€ ์žˆ์–ด์„œ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค.


top ๋ช…๋ น ์‹คํ–‰์‹œ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” ์˜ต์…˜

    * (top) -d [sec]: ์„ค์ •๋œ ์ดˆ๋‹จ์œ„๋กœ Refresh
    * (top) -c      : command๋’ค์— ์ธ์ž๊ฐ’ ํ‘œ์‹œ

top ๋ช…๋ น ์‹คํ–‰ ํ›„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์˜ต์…˜

    * shift + t     : ์‹คํ–‰๋œ ์‹œ๊ฐ„์ด ํฐ ์ˆœ์„œ๋กœ ์ •๋ ฌ
    * shift + m     : ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰์ด ํฐ ์ˆœ์„œ๋กœ ์ •๋ ฌ
    * shift + p     : cpu ์‚ฌ์šฉ๋Ÿ‰์ด ํฐ ์ˆœ์„œ๋กœ ์ •๋ ฌ
    * k             : Process ์ข…๋ฃŒ
       o k ์ž…๋ ฅ ํ›„ ์ข…๋ฃŒํ•  PID๋ฅผ ์ž…๋ ฅํ•œ๋‹ค
       o signal์„ ์ž…๋ ฅํ•˜๋ผ ํ‘œ์‹œ๋˜๋ฉด 9๋ฅผ ๋„ฃ์–ด์ค€๋‹ค
    * c             : ๋ช…๋ น ์ธ์ž ํ‘œ์‹œ / ๋น„ํ‘œ์‹œ
    * l(์†Œ ๋ฌธ์ž์—˜)   : uptime line(์ฒซ๋ฒˆ์งธ ํ–‰)์„ ํ‘œ์‹œ / ๋น„ํ‘œ์‹œ
    * space bar     : Refresh
    * u             : ์ž…๋ ฅํ•œ ์œ ์ € ์†Œ์œ ์˜ Process๋งŒ ํ‘œ์‹œ
       o which user: ์™€ ๊ฐ™์ด ์œ ์ €๋ฅผ ์ž…๋ ฅํ•˜๋ผ ํ‘œ์‹œ๋ ๋•Œ User๋ฅผ ์ž…๋ ฅ
       o blank(๊ณต๋ฐฑ) ์ž…๋ ฅ์‹œ ๋ชจ๋‘ ํ‘œ์‹œ
    * shift + b     : ์ƒ๋‹จ์˜ uptime ๋ฐ ๊ธฐํƒ€ ์ •๋ณด๊ฐ’์„ ๋ธ”๋ฝ์„ ํƒํ•ด ํ‘œ์‹œ
    * f             : ํ™”๋ฉด์— ํ‘œ์‹œ๋  ํ”„๋กœ์„ธ์Šค ๊ด€๋ จ ํ•ญ๋ชฉ ์„ค์ •


top ๋ช…๋ น ์‹คํ–‰ ํ›„ ํ™”๋ฉด

 top.PNG


์„ธ๋ถ€ ์ •๋ณด ํ•„๋“œ๋ณ„ ํ•ญ๋ชฉ


  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND

    * PID : ํ”„๋กœ์„ธ์Šค ID (PID)
    * USER : ํ”„๋กœ์„ธ์Šค๋ฅผ ์‹คํ–‰์‹œํ‚จ ์‚ฌ์šฉ์ž ID
    * PRI : ํ”„๋กœ์„ธ์Šค์˜ ์šฐ์„ ์ˆœ์œ„ (priority)
    * NI : NICE ๊ฐ’. ์ผ์˜ nice value๊ฐ’์ด๋‹ค. ๋งˆ์ด๋„ˆ์Šค๋ฅผ ๊ฐ€์ง€๋Š” nice value๋Š” ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์Œ.
    * VIRT : ๊ฐ€์ƒ ๋ฉ”๋ชจ๋ฆฌ์˜ ์‚ฌ์šฉ๋Ÿ‰(SWAP+RES)
    * RES : ํ˜„์žฌ ํŽ˜์ด์ง€๊ฐ€ ์ƒ์ฃผํ•˜๊ณ  ์žˆ๋Š” ํฌ๊ธฐ(Resident Size)
    * SHR : ๋ถ„ํ• ๋œ ํŽ˜์ด์ง€, ํ”„๋กœ์„ธ์Šค์— ์˜ํ•ด ์‚ฌ์šฉ๋œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋‚˜๋ˆˆ ๋ฉ”๋ชจ๋ฆฌ์˜ ์ดํ•ฉ.
    * S : ํ”„๋กœ์„ธ์Šค์˜ ์ƒํƒœ [ S(sleeping), R(running), W(swapped out process), Z(zombies) ]
    * %CPU : ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” CPU์˜ ์‚ฌ์šฉ์œจ
    * %MEM : ํ”„๋กœ์„ธ์Šค๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”๋ชจ๋ฆฌ์˜ ์‚ฌ์šฉ์œจ
    * COMMAND : ์‹คํ–‰๋œ ๋ช…๋ น์–ด




์ถœ์ฒ˜ : http://faq.hostway.co.kr/?mid=Linux_ETC&page=7&document_srl=1441


์•ˆ๋…•ํ•˜์„ธ์š” ์ผ€์ด์น˜์ž…๋‹ˆ๋‹ค. 


์˜ค๋žœ๋งŒ์— ํฌ์ŠคํŒ…์„ ํ•˜๊ฒŒ๋˜๋„ค์š”. ์˜ค๋Š˜์€ ์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค์—์„œ ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ์ด์šฉํ•œ ํ™”๋ฉด์ „ํ™˜์„ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.


์ด ํฌ์ŠคํŒ…์„ ํ•˜๊ฒŒ๋œ ์ด์œ ๋Š” Do It ์•ˆ๋“œ๋กœ์ด๋“œ์˜ ๋™์˜์ƒ ๊ฐ•์ขŒ ์†Œ์Šค(๋กค๋ฆฌํŒ ๊ธฐ์ค€ Day 10 - 2)๊ฐ€ ํ˜„์žฌ ์•ˆ์Šค์—์„œ ์ƒ์„ฑํ•ด์ฃผ๋Š” ๊ธฐ๋ณธ์†Œ์Šค์™€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.


์ผ๋‹จ ๊ฐœ๋ฐœํ™˜๊ฒฝ๋ถ€ํ„ฐ ๋ณด์‹œ์ฃ .




์ž, ์ผ๋‹จ ์•ˆ์Šค์—์„œ ์ƒˆ๋กœ์šด ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•ด๋ณด๋„๋ก ํ•ฉ์‹œ๋‹ค. 

์•„๋ž˜ ์Šคํฌ๋ฆฐ์ƒท๋Œ€๋กœ ๋”ฐ๋ผ์„œ ํ•ด๋ณด์‹œ๊ณ  ์ƒ์„ฑ๋œ ์†Œ์Šค๊ฐ€ ๋™์ผํ•œ์ง€ ํ™•์ธํ•˜์„ธ์š”.













์ž, ์ด์ œ ์šฐ๋ฆฌ๋Š” ์—ฌ๊ธฐ์— ์ƒˆ๋กœ์šด ํ”„๋ž˜๊ทธ๋จผํŠธ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ๊ทธ ํ”„๋ž˜๊ทธ๋จผํŠธ์˜ ๋ ˆ์ด์•„์›ƒ์„ ์ถ”๊ฐ€ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ƒˆ๋กœ ์ƒ์„ฑ๋  ํŒŒ์ผ์€ ๋‘๊ฐœ์ž…๋‹ˆ๋‹ค.

MenuFragment.java์™€ fragment_menu.xml ์ž…๋‹ˆ๋‹ค.

MenuFragment.java๋Š” MainActivityFragment.java๋ฅผ ๋ณต์‚ฌํ•ด์„œ ์ƒ์„ฑํ•˜์‹œ๋ฉด๋˜๊ณ 

fragment_menu.xml์€ fragment_main.xml์„ ๋ณต์‚ฌํ•ด์„œ ์ƒ์„ฑํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.


์ž, ์ด๋ ‡๊ฒŒ ๋‘๊ฐœ๋ฅผ ์ถ”๊ฐ€ํ•˜์…จ์œผ๋ฉด ๊ธฐ์กด ์†Œ์Šค๋ฅผ ์•„๋ž˜์ฒ˜๋Ÿผ ๋ณ€๊ฒฝํ•ด์ฃผ์„ธ์š”.


MenuFragment.java


fragment_menu.xml


MainActivityFragment.java


fragment_main.xml



MainActivity.java


activity_main.xml


๋ณ€๊ฒฝ๋œ ์†Œ์Šค๋ฅผ ๋ณด์‹œ๋ฉด ๋ฉ”์ธ์•กํ‹ฐ๋น„ํ‹ฐ ์ชฝ์ด ์ข€ ํฌ๊ฒŒ ๋ฐ”๋€Œ์—ˆ์Šต๋‹ˆ๋‹ค. activity_main.xml์—์„œ Fragment๊ฐ€ ์•„์˜ˆ ๋น ์ ธ๋ฒ„๋ฆฌ๊ณ  ๊ทธ ์ž๋ฆฌ๋ฅผ RelativaLayout์ด ์ฐจ์ง€ํ–ˆ์Šต๋‹ˆ๋‹ค.


๊ทธ๋ฆฌ๊ณ  MainActivity.java์—์„œ setContentView ๋‹ค์Œ์— ํ•œ ๋ผ์ธ์ด ์ถ”๊ฐ€๋˜์—ˆ๋Š”๋ฐ ์ด ๋ผ์ธ์ด ํ•˜๋Š” ์ผ์€ ํ”„๋ž˜๊ทธ๋จผํŠธ ๋ฉ”๋‹ˆ์ €๊ฐ€ ๋ฉ”์ธ ๋ ˆ์ด์•„์›ƒ ์ฆ‰ RelativeLayout์— ๋ฉ”์ธํ”„๋ž˜๊ทธ๋จผํŠธ(MainActivityFragment)๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. 

๊ทธ๋ฆฌ๊ณ  ๋ฒ„ํŠผ ํด๋ฆญ ๋ฆฌ์Šค๋„ˆ์—๋Š” ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฉ”๋‹ˆ์ €๊ฐ€ ๋ฉ”์ธํ”„๋ž˜๊ทธ๋จผํŠธ์™€ ๋ฉ”๋‰ดํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ๊ต์ฒดํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.


์ด๋ ‡๊ฒŒ ํ•จ์œผ๋กœ์จ ๊ฐ ํ”„๋ž˜๊ทธ๋จผํŠธ์—์„œ ๋‹ค๋ฅธ ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ํ˜ธ์ถœ(๋ฒ„ํŠผ์„ ํด๋ฆญํ•จ์œผ๋กœ์จ ํ˜ธ์ถœ)ํ•  ๋•Œ ์„œ๋กœ ๊ต์ฒด๊ฐ€ ๊ฐ€๋Šฅํ•˜๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค.


activity_main.xml์—์„œ Fragment๋ฅผ ๋บ€ ์ด์œ ๋Š”, ํ•ด๋ณด์‹œ๋ฉด ์•„์‹œ๊ฒ ์ง€๋งŒ, ๋ฉ”์ธ ๋ ˆ์ด์•„์›ƒ์„ ์•„์˜ˆ Fragment๋กœ ์ง€์ •ํ•จ์œผ๋กœ์จ ์—‘ํ‹ฐ๋น„ํ‹ฐ์— ์˜ฌ๋ผ๊ฐ„ ํ”„๋ž˜๊ทธ๋จผํŠธ๊ฐ€ ํ”„๋ž˜๊ทธ๋จผํŠธ ๊ด€๋ฆฌ์ž์˜ replace๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ์ด ๋˜์—ˆ์„ ๋•Œ destroy ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ •ํ™•ํ•œ ์ด์œ ๋Š” ์ œ๊ฐ€ ์•„์ง ์ดˆ๋ณด์ž๋ผ ์ž˜ ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ ๊ด€๋ฆฌ์ž์˜ ์†์„ ๋– ๋‚œ ๋‹ค๊ณ  ํ•ด์•ผํ•˜๋‚˜, ํ”„๋ž˜๊ทธ๋จผํŠธ๊ฐ€ ๊ทธ๋ƒฅ ํ•˜๋‚˜์˜ ์•กํ‹ฐ๋น„ํ‹ฐ๊ฐ€ ๋˜์–ด๋ฒ„๋ฆฐ๊ฒƒ ๊ฐ™์€ ๋Š๋‚Œ์ด ๋“ญ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ฒ˜์Œ์— ํ™”๋ฉด์ด ์ค‘๋ณต๋˜์–ด ๋‚˜์˜ค๊ธธ๋ž˜ ๋ฐฉ์•ˆ์„ ์ฐพ์•„์„œ ํ•œ ๊ฒƒ์ด ์œ„์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์ด์—ˆ์Šต๋‹ˆ๋‹ค.


๊ทธ๋ƒฅ ํ˜ผ์ž ๊ณต๋ถ€ํ•˜๋ฉด์„œ ๋„์ ์ด๋Š” ๊ฑฐ๋ผ ์ •๋ฆฌ๊ฐ€ ์ž˜ ์•ˆ๋œ๊ฒƒ ๊ฐ™๊ธด ํ•œ๋ฐ ์กฐ๊ธˆ์ด๋ผ๋„ ๋„์›€์ด ๋˜์…จ์œผ๋ฆฌ๋ผ ์ƒ๊ฐํ•˜๊ณ  ์ด๋งŒ ์ค„์ด๊ฒ ์Šต๋‹ˆ๋‹ค. 


์ด์ƒ ์ผ€์ด์น˜์˜€์Šต๋‹ˆ๋‹ค. 


๊ฑด๊ฐ•์กฐ์‹ฌํ•˜์‹œ๊ณ  ์ฆํ”„ํ•˜์„ธ์š” ^-^ ( ์ „ ์—ฌ๋ฆ„ ๊ฐ๊ธฐ์— ๊ฑธ๋ ค์„œ ์ฃฝ์„๋ง›์ด๋„ค์š” ใ… ใ…  )



์•ˆ๋…•ํ•˜์„ธ์š” ์ผ€์ด์น˜์ž…๋‹ˆ๋‹ค. 


์˜ค๋Š˜์€ ์ž๋ฐ” ๊ฐ์ฒด๋ฅผ ๋ฐ”์ดํŠธ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ํฌ์ŠคํŒ…ํ•ฉ๋‹ˆ๋‹ค.



1. Object to byte[]


ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
  out = new ObjectOutputStream(bos);   
  out.writeObject(yourObject);
  byte[] yourBytes = bos.toByteArray();
  ...
} finally {
  try {
    if (out != null) {
      out.close();
    }
  } catch (IOException ex) {
    // ignore close exception
  }
  try {
    bos.close();
  } catch (IOException ex) {
    // ignore close exception
  }
}


2. byte[] to Object


ByteArrayInputStream bis = new ByteArrayInputStream(yourBytes);
ObjectInput in = null;
try {
  in = new ObjectInputStream(bis);
  Object o = in.readObject(); 
  ...
} finally {
  try {
    bis.close();
  } catch (IOException ex) {
    // ignore close exception
  }
  try {
    if (in != null) {
      in.close();
    }
  } catch (IOException ex) {
    // ignore close exception
  }
}


๊ธฐ๋ณธ์ ์ธ ์ž๋ฐ”๋ฅผ ์ด์šฉํ•˜๋ฉด ์ €๋ ‡๊ฒŒ ํ•˜์‹œ๋ฉด ๋˜๋Š”๋ฐ ์•„ํŒŒ์น˜ ์œ ํ‹ธ์„ ์ด์šฉํ•˜๋ฉด ์ข€ ๋” ํŽธํ•˜๊ฒŒ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


ApacheUtils ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋‹ค์šด ๋ฐ›์•„์„œ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.



To Serialize:

byte[] data = SerializationUtils.serialize(yourObject);

To Deserialize:

YourObject yourObject = (YourObject) SerializationUtils.deserialize(byte[] data)
๋‹ฌ๋ž‘ ํ•œ์ค„๋กœ Object -> byte[] ๋„ ๊ฐ€๋Šฅํ•˜๊ณ  ์—ญ์œผ๋กœ๋„ ๊ฐ€๋Šฅํ•˜๋„ค์š”.

์ฐธ ๊ฐ„๋‹จํ•˜์ฅฌ์ž‰~?

์ด์ƒ ์ผ€์ด์น˜์˜€์Šต๋‹ˆ๋‹ค.

์ฆํ”„ํ•˜์„ธ์š”~ 




1. cmd์˜ ๋ฐ”๋กœ๊ฐ€๊ธฐ ์˜ค๋ฅธ์ชฝ ํด๋ฆญ. ์†์„ฑ>์˜ต์…˜>ํ˜„์žฌ ์ฝ”๋“œ ํŽ˜์ด์ง€ ๋ณ€๊ฒฝ

2. cmd์ฐฝ์—์„œ chcp ์‹คํ–‰

MS949๋กœ ๋ณ€๊ฒฝํ•˜๊ณ ์ž ํ•  ๊ฒฝ์šฐ --> chcp 949

utf-8๋กœ ๋ณ€๊ฒฝํ•˜๊ณ ์ž ํ•  ๊ฒฝ์šฐ --> chcp 65001


๊ธฐํƒ€ ๋‹ค๋ฅธ ์ฝ”๋“œ ํŽ˜์ด์ง€์— ๋Œ€ํ•ด์„œ๋Š” ์•„๋ž˜ ์œ„ํ‚คํŽ˜์ด์ง€๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.
http://en.wikipedia.org/wiki/Code_page


์„ค์น˜ํ™˜๊ฒฝ

์œˆ๋„์šฐ 8

์ดํด๋ฆฝ์Šค Luna

 

๋ชฉ์ฐจ 

1. SonarQube ์ดํด๋ฆฝ์Šค ํ”Œ๋Ÿฌ๊ทธ์ธ ์„ค์น˜

2. SonarQube Download ๋ฐ ์„œ๋ฒ„ ์‹คํ–‰

3. ๋ถ„์„ํ•˜๊ณ ์žํ•˜๋Š” ํ”„๋กœ์ ํŠธ์™€ SonarQube ํ”„๋กœ์ ํŠธ ์—ฐ๋™ํ•˜๊ธฐ

4. ๋ถ„์„ ๊ฒฐ๊ณผ ๋ณด๊ธฐ

 

 

1. SonarQube ์ดํด๋ฆฝ์Šค ํ”Œ๋Ÿฌ๊ทธ์ธ ์„ค์น˜

ํ”Œ๋Ÿฌ๊ทธ์ธ ์„ค์น˜๋Š” ์‰ฌ์šฐ๋‹ˆ๊นŒ ์•„๋ž˜ ์˜๋ฌธ ๊ฐ€์ด๋“œ ๋ณด์‹œ๋ฉด์„œ ํ•˜์‹œ๋ฉด ๋ ๊ฒƒ ๊ฐ™๋„ค์š”.

 

SonarQube ๊ณต์‹ ์ดํด๋ฆฝ์Šค ์„ค์น˜ ๊ฐ€์ด๋“œ ๋ฌธ์„œ

http://docs.sonarqube.org/display/SONAR/Installing+SonarQube+in+Eclipse

 

  1. Go to Help > Eclipse Marketplace... and search for "SonarQube"
    or
    Use offline update site: https://bintray.com/artifact/download/sonarsource/Distribution/org.sonar.ide.eclipse.site-3.5.0.20150804-1512-RELEASE.zip
    or
    Go to Help > Install New Software... This should display the Install dialog box. Paste the Update Site URL (http://downloads.sonarsource.com/eclipse/eclipse/) into the field Work with and press Enter. This should display the list of available plugins and components:



  2. Check the component you wish to install (see Features details).
  3. Click Next. Eclipse will then check to see if there is any issue which would prevent a successful installation.
  4. Click Finish to begin the installation process. Eclipse will then download and install the necessary components.
  5. Once the installation process is finished, Eclipse will ask if you want to restart the IDE. It is strongly recommended that you restart the IDE.

 

2. SonarQube Download ๋ฐ ์„œ๋ฒ„ ์‹คํ–‰ 

์•„๋ž˜ ๋งํฌ๋กœ ๊ฐ€์‹œ๋ฉด zip ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œ ํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

http://www.sonarqube.org/downloads/

 

๋‹ค์šด๋ฐ›์€ zipํŒŒ์ผ์„ ๋ฐ”ํƒ•ํ™”๋ฉด(์›ํ•˜๋Š” ๊ณณ)์— ์••์ถ•์„ ํ’€์–ด์ฃผ์„ธ์š”.

 

ํด๋” ๋“ค์–ด๊ฐ€๋ฉด binํด๋”๊ฐ€ ์žˆ๊ณ  ๊ทธ ์•ˆ์— StartSonar.bat๋ผ๋Š” ํŒŒ์ผ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด ํŒŒ์ผ์„ ์‹คํ–‰์‹œ์ผœ์„œ ์„œ๋ฒ„๋ฅผ ๋„์›Œ์ฃผ์„ธ์š”.

 

๋งŒ์•ฝ ์„œ๋ฒ„ ์‹คํ–‰์‹œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์•„๋ž˜ ๋งํฌ๋ฅผ ์ฐธ์กฐํ•ด์ฃผ์„ธ์š”.

 

http://blog.naver.com/stork838/220442651809

 

๋งˆ์ง€๋ง‰์œผ๋กœ ์„œ๋ฒ„๊ฐ€ ์ œ๋Œ€๋กœ ์‹คํ–‰ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ

http://localhost:9000 ( ๋‹ค๋ฅธ ์„ค์ •์„ ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š์•˜๋‹ค๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ์ด ์ฃผ์†Œ๋กœ ์ ‘์†์ด ๊ฐ€๋Šฅํ•ด์•ผ ํ•จ )

 

3. ๋ถ„์„ํ•˜๊ณ ์žํ•˜๋Š” ํ”„๋กœ์ ํŠธ์™€ SonarQube ํ”„๋กœ์ ํŠธ ์—ฐ๋™ํ•˜๊ธฐ

a. ์ผ๋‹จ ์„œ๋ฒ„์— ์ ‘์†์ด ์ž˜ ๋˜๋Š”์ง€ ํ™•์ธ์ด ๋๋‹ค๋ฉด admin์œผ๋กœ ๋กœ๊ทธ์ธ ํ•ฉ๋‹ˆ๋‹ค.

( admin / admin ์œผ๋กœ ์ ‘์† ๊ฐ€๋Šฅ )

 

b. Go to Settings -> System -> Provisioning

c. Create new project (Create ๋ฒ„ํŠผ์€ ์šฐ์ƒ๋‹จ์— ์œ„์น˜ํ•ด์žˆ์Šต๋‹ˆ๋‹ค. )

d. ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ์‹œ key์™€ ํ”„๋กœ์ ํŠธ๋ช…์„ ์ ์ ˆํ•˜๊ฒŒ ์•„๋ฌด๊ฑฐ๋‚˜ ์จ์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‚˜์ค‘์— ์ดํด๋ฆฝ์Šค์—์„œ ์—ฐ๋™ํ•  ๋•Œ ์ด ์ด๋ฆ„์ด ์“ฐ์ž…๋‹ˆ๋‹ค.

 

e. ์ดํด๋ฆฝ์Šค์—์„œ ํ”„๋กœ์ ํŠธ์—์„œ ์šฐํด๋ฆญ -> Configure -> Associate with SonaQube

 

f. Sona Project ์ž…๋ ฅ๋ž€์— ์•„๊นŒ ์ƒ์„ฑํ•œ Sona ํ”„๋กœ์ ํŠธ๋ช…์„ ์ž…๋ ฅํ•˜๋ฉด ์ž๋™์™„์„ฑ๊ธฐ๋Šฅ์œผ๋กœ ๋“œ๋ž๋‹ค์šด ์˜ต์…˜์ด ๋ณด์ž…๋‹ˆ๋‹ค.

g. ์†Œ๋‚˜ ํ”„๋กœ์ ํŠธ๋ฅผ ์„ ํƒํ•˜๊ณ  ์™„๋ฃŒํ•˜๋ฉด ์ž๋™์œผ๋กœ ๋ถ„์„์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

 

4. ๋ถ„์„ ๊ฒฐ๊ณผ ๋ณด๊ธฐ

๋ถ„์„์ด ์ง„ํ–‰๋˜๋Š” ๋™์•ˆ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฉ”์‹œ์ง€๊ฐ€ ์ดํด๋ฆฝ์Šค ์ฝ˜์†”์ฐฝ์— ๋‚˜์˜ต๋‹ˆ๋‹ค.

 

Retrieve remote issues of project myproject...
Project not found on remote SonarQube server [myproject]      ---- A
Start SonarQube analysis on myproject...
INFO: SonarQube Server 5.1.2
11:20:19.564 INFO  - Incremental mode
11:20:19.935 INFO  - Load global repositories
11:20:20.786 INFO  - Load global repositories (done) | time=857ms
11:20:20.791 INFO  - Server id: 20150806110249
11:20:20.797 INFO  - User cache: C:\Users\user\.sonar\cache
11:20:20.813 INFO  - Install plugins
11:20:20.863 INFO  - Include plugins:
11:20:20.864 INFO  - Exclude plugins: devcockpit, jira, pdfreport, views, report, buildstability, buildbreaker
11:20:20.866 INFO  - Download sonar-core-plugin-5.1.2.jar
11:20:20.946 INFO  - Download sonar-email-notifications-plugin-5.1.2.jar
11:20:21.249 INFO  - Download sonar-java-plugin-3.0.jar
11:20:21.745 INFO  - Download sonar-scm-git-plugin-1.0.jar
11:20:21.963 INFO  - Download sonar-l10n-en-plugin-5.1.2.jar
11:20:21.987 INFO  - Download sonar-scm-svn-plugin-1.0.jar
11:20:23.923 INFO  - Load project repositories
11:20:24.658 INFO  - Load project repositories (done) | time=735ms
11:20:24.659 INFO  - Load project settings
11:20:25.575 INFO  - Apply project exclusions
11:20:25.613 INFO  - -------------  Scan myproject
11:20:25.624 INFO  - Load module settings
11:20:26.068 INFO  - Base dir: E:\ใ…ใ„ดใ…‡ใ„น
11:20:26.068 INFO  - Working dir: E:\ใ…ใ„ดใ…‡ใ„น
11:20:26.072 INFO  - Source paths: src
11:20:26.073 INFO  - Test paths: src
11:20:26.079 INFO  - Binary dirs: build/classes
11:20:26.079 INFO  - Source encoding: UTF-8, default locale: ko_KR
11:20:26.084 INFO  - Index files
11:20:26.098 INFO  - Excluded sources:
11:20:26.099 INFO  -   **/*Test.*
11:20:26.099 INFO  -   **/test/**/*
11:20:26.099 INFO  - Included tests:
11:20:26.099 INFO  -   **/*Test.*
11:20:26.099 INFO  -   **/test/**/*
11:20:27.123 INFO  - 370 files indexed
11:20:27.124 INFO  - 430 files ignored because of inclusion/exclusion patterns
11:20:27.150 INFO  - Quality profile for java: Sonar way
11:20:27.226 INFO  - Sensor JavaSquidSensor
11:20:30.695 INFO  - Java Main Files AST scan...
11:20:30.695 INFO  - 370 source files to be analyzed
11:20:40.706 INFO  - 26/370 files analyzed, current is E:\sdf.java
11:20:50.706 INFO  - 84/370 files analyzed, current is E:\dfg.java
11:21:00.706 INFO  - 187/370 files analyzed, current is E:\fgh.java
11:21:10.708 INFO  - 281/370 files analyzed, current is E:\rtyService.java
11:21:20.711 INFO  - 329/370 files analyzed, current is E:\erwtDao.java
11:21:27.080 INFO  - Java Main Files AST scan done: 56385 ms
11:21:27.080 INFO  - 370/370 source files analyzed
11:21:27.098 INFO  - Java bytecode scan...
11:21:29.109 INFO  - Java bytecode scan done: 2011 ms
11:21:29.109 INFO  - Java Test Files AST scan...
11:21:29.109 INFO  - 0 source files to be analyzed
11:21:29.110 INFO  - Java Test Files AST scan done: 1 ms
11:21:29.111 INFO  - 0/0 source files analyzed
11:21:29.121 INFO  - Package design analysis...
11:21:29.655 INFO  - Package design analysis done: 534 ms
11:21:30.485 INFO  - Sensor JavaSquidSensor (done) | time=63259ms
11:21:30.486 INFO  - Sensor Lines Sensor
11:21:30.576 INFO  - Sensor Lines Sensor (done) | time=90ms
11:21:30.576 INFO  - Sensor SurefireSensor
11:21:30.579 INFO  - parsing E:\sdf\surefire-reports
11:21:30.580 WARN  - Reports path not found: E:\dfg\surefire-reports     ------ B
11:21:30.580 INFO  - Sensor SurefireSensor (done) | time=4ms
11:21:31.576 INFO  - Export issues to E:\dfg\sonar-report.json
11:21:32.193 INFO  - ANALYSIS SUCCESSFUL

 

์œ„ ๋นจ๊ฐ„ ์ค„๋กœ ๋ณด์ด๋Š” ๋ถ€๋ถ„์€ ๋ฌด์‹œํ•˜์…”๋„ ๋ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋ฅผ ์ข€ ๋” ์ฝ์–ด๋ณด์‹œ๋ฉด ๊ทธ ์ด์œ ๊ฐ€ ๋‚˜์˜ต๋‹ˆ๋‹ค.

 

5. ์ˆ˜๋™์œผ๋กœ ๋ถ„์„ํ•˜๊ธฐ 

์ดํด๋ฆฝ์Šค ํ”„๋กœ์ ํŠธ๊ฐ€ Sona ํ”„๋กœ์ ํŠธ์™€ ์—ฐ๋™์ด ๋˜๋ฉด ํ”„๋กœ์ ํŠธ ์šฐํด๋ฆญ ๋ฉ”๋‰ด์— SonaQube ๋ฉ”๋‰ด๊ฐ€ ๋œจ๊ฒŒ๋ฉ๋‹ˆ๋‹ค.

ํ•ด๋‹น ๋ฉ”๋‰ด๋กœ ๋“ค์–ด๊ฐ€๋ฉด Analysis๊ฐ€ ์žˆ์œผ๋‹ˆ ์ด๋ฅผ ์ด์šฉํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

์ฐธ๊ณ ๋กœ ์ €๋Š” ์ง„ํ–‰ํ•˜๋Š” ๋™์•ˆ ์•„๋ž˜์™€ ๊ฐ™์€ ์˜ค๋ฅ˜๋ฉ”์‹œ์ง€๋ฅผ ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

์ฝ˜์†”์ฐฝ์—๋Š” ANALYSIS SUCCESSFUL์ด๋ผ๊ณ  ๋–ด์ง€๋งŒ ๋™์‹œ์— ํŒ์—…์ฐฝ์œผ๋กœ

An internal error occurred during: "SonarQube Analysis".
Unable to create markers

๋ผ๊ณ  ๋‚˜์˜ค๋„ค์š”.

 

์›์ธ์„ ์ฐพ์•„์„œ ๋‹ค์‹œ ์ด์–ด์„œ ๊ธ€์„ ์“ฐ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

๊ทธ๋Ÿผ ๋‹ค๋“ค ์ฆํ”„~

 

------------- updated 2015-08-07 -------------

์ดํด๋ฆฝ์Šค์—์„œ๋Š” preview ๋‚˜ incremental ๋ถ„์„๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์ž์„ธํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.

http://docs.sonarqube.org/display/SONAR/Working+with+SonarQube+in+Eclipse.

 

๊ทธ ๋ง์ธ ์ฆ‰์Šจ, ๋ถ„์„์ด ๋กœ์ปฌ๋กœ ์ด๋ฃจ์–ด์ง€๊ณ  SonarQube DB์— ์ €์žฅ๋˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๋ง์ž…๋‹ˆ๋‹ค.

์ž์„ธํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž˜์š”.

http://docs.sonarqube.org/display/SONAR/Concepts#Concepts-AnalysisModes

 

SonarQube ์„œ๋ฒ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ์›นํŽ˜์ด์ง€์—์„œ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ ค๋ฉด SonarQube Runner๋ฅผ ๋‹ค์šด ๋ฐ›์•„์„œ ์‹คํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ž์„ธํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.

http://docs.sonarqube.org/display/SONAR/Analyzing+with+SonarQube+Runner

 

 

5. ์ˆ˜๋™์œผ๋กœ ๋ถ„์„ํ•˜๊ธฐ - 2

์ง€๋‚œ ๋ฒˆ ํฌ์ŠคํŒ…์—์„œ ์ˆ˜๋™์œผ๋กœ ๋ถ„์„ํ•˜๊ธฐ๋ฅผ ํ• ๋•Œ ๋‹จ์ˆœํžˆ ์ดํด๋ฆฝ์Šค์—์„œ Analyzeํ–ˆ์—ˆ๋Š”๋ฐ ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ฉด ์„œ๋ฒ„์— ๊ธฐ๋ก๋„ ๋‚จ์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•˜์ฃ . ๊ทธ๋ž˜์„œ ์ข€ ๋” ์กฐ์‚ฌ๋ฅผ ํ•ด์„œ ์ง„๋„๋ฅผ ์ข€ ๋นผ ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

์šฐ์„  ์š”๊ธฐ ๋ฐ”๋กœ ์œ„์— ์„ธ๊ฐœ์˜ ๋งํฌ ์ค‘์—์„œ ๋งˆ์ง€๋ง‰ ๋งํฌ๋กœ ๊ฐ€์…”์„œ ์†Œ๋‚˜ํ๋ธŒ ๋Ÿฌ๋„ˆ๋ฅผ ๋‹ค์šด๋ฐ›์œผ์‹œ๊ณ  ์›ํ•˜๋Š” ์œ„์น˜์— ์••์ถ•์„ ํ’€์–ด์ฃผ์„ธ์š”.

๊ทธ ๋ฆฌ๊ณ  ๋‚ด์ปดํ“จํ„ฐ > ์†์„ฑ >  ๊ณ ๊ธ‰์‹œ์Šคํ…œ์„ค์ • > ํ™˜๊ฒฝ๋ณ€์ˆ˜ ๋กœ ์ด๋™ํ•˜์…”์„œ ( ์œˆ๋„์šฐ 8 ๊ธฐ์ค€์ž…๋‹ˆ๋‹ค. ) ์•„๋ž˜์ฒ˜๋Ÿผ SONAR_RUNNER_HOME ๋ณ€์ˆ˜๋ฅผ ์ƒˆ๋กœ๋งŒ๋“ค๊ธฐ๋ฅผ ์ด์šฉํ•ด์„œ ๋“ฑ๋กํ•ด์ฃผ์‹œ๊ณ , Path์— ์‚ฌ์ง„์— ๋ณด์ด์‹œ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋“ฑ๋กํ•ด์ฃผ์„ธ์š”.

 

 

 

 

๊ทธ๋ฆฌ๊ณ  ์†Œ๋‚˜ ์„œ๋ฒ„๊ฐ€ ๊ธฐ๋™๋˜์–ด์žˆ๋‹ค๋Š” ๊ฐ€์ •ํ•˜์—, ๋ถ„์„ํ•˜๊ณ ์žํ•˜๋Š” ํ”„๋กœ์ ํŠธ์˜ ๋ฃจํŠธ ๋””๋ ‰ํ† ๋ฆฌ๋กœ ์ด๋™ํ•ด์ฃผ์„ธ์š”.

์˜ˆ๋ฅผ๋“ค๋ฉด d:\eclipse\workspace\myproject ๊ฐ€ ๋˜๊ฒ ์ฃ ?

 

๊ทธ๋ฆฌ๊ณ  ํŒŒ์ผ์„ ํ•˜๋‚˜ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

 

sonar-project.properties ๋ผ๋Š” ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  ์•„๋ž˜ ๋‚ด์šฉ์„ ๋„ฃ์–ด์ฃผ์„ธ์š”. ๋ณธ์ธ์˜ ์„ค์ •์— ๋งž๊ฒŒ projectKey์™€ projectName ๊ทธ๋ฆฌ๊ณ  ์ œ์ผ ์•„๋ž˜์˜ ์†Œ์Šค ์ธ์ฝ”๋”ฉ ๊ฐ’์„ ๋ฐ”๊ฟ”์ฃผ์„ธ์š”.

 

 

# must be unique in a given SonarQube instance
sonar.projectKey=myproject

# this is the name displayed in the SonarQube UI
sonar.projectName=myproject project
sonar.projectVersion=1.0
 
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# Since SonarQube 4.2, this property is optional if sonar.modules is set.
# If not set, SonarQube starts looking for source code from the directory containing
# the sonar-project.properties file.
sonar.sources=.
 
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8

 

 

์ด์ œ ์ €์žฅ์„ ํ•˜๊ณ  ๋‚˜์™€์„œ cmd์ฐฝ์„ ์—ด์–ด์„œ ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ ๋””๋ ‰ํ† ๋ฆฌ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ์†Œ๋‚˜ ๋Ÿฌ๋„ˆ๋ฅผ ์‹คํ–‰์‹œํ‚ต๋‹ˆ๋‹ค.

 

๊ธฐ๋ณธ์ ์œผ๋กœ sonar-runner ๋ผ๊ณ  ํ•˜๋ฉด ๋˜๋Š”๋ฐ -X ์˜ต์…˜์„ ์ฃผ๋ฉด debug ๋ชจ๋“œ๋กœ ์ง„ํ–‰์ƒํ™ฉ์„ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์•„๋งˆ ํ”„๋กœ์ ํŠธ๊ฐ€ svn๊ณผ ์—ฐ๊ฒฐ๋˜์–ด์žˆ์ง€ ์•Š์€ ์ƒํƒœ๋ผ๋ฉด ( .svn ๋””๋ ‰ํ† ๋ฆฌ๊ฐ€ ํ”„๋กœ์ ํŠธ ๋ฃจํŠธ๋””๋ ‰ํ† ๋ฆฌ์— ์—†๋‹ค๋ฉด ) ์‹คํ–‰์ด ์ž˜ ๋ ๊ฒ๋‹ˆ๋‹ค.

 

์‹คํ–‰์ด ์ž˜ ๋˜์—ˆ๋‹ค๋ฉด ๋กœ๊ทธ ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์— ์•„๋ž˜์ฒ˜๋Ÿผ ๋‚˜์˜ค๊ณ  ์†Œ๋‚˜ ๋Ÿฌ๋„ˆ๋Š” ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค.

 

INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
Total time: 25.866s
Final Memory: 15M/499M
INFO: ------------------------------------------------------------------------

 

์ด์ œ ์†Œ๋‚˜ํ๋ธŒ ์›น์œผ๋กœ๊ฐ€์„œ ๋ถ„์„๋œ ์ž๋ฃŒ๋ฅผ ํ™•์ธํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

 

 

์ด๋Ÿฐ์‹์œผ๋กœ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•˜์‹ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ์ˆ˜์น˜๊ฐ€ ๋ญ˜ ์˜๋ฏธํ•˜๋Š”์ง€์— ๋Œ€ํ•ด์„œ๋Š” ์ž๋ฃŒ๋ฅผ ์ข€ ์ฐพ์•„๋ด์•ผ ํ• ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋ถ€๋ถ„์€ ์—ฌ๋Ÿฌ๋ถ„๋“ค์ด ๊ฐ์ž ์•Œ์•„์„œ ํ•ด๋ณด์‹œ๋ฉด ๋ ๊ฒƒ ๊ฐ™๋„ค์š”.

 

์ฐธ๊ณ ๋กœ ์ €๋Š” svn ์—ฐ๋™ํ•ด๋†“์€ ํ”„๋กœ์ ํŠธ๋„ ์‹œ๋„ํ•ด๋ณด์•˜๋Š”๋ฐ svn ๊ด€๋ จ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.

 

์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ๋Š” ์ถ”ํ›„ ์—…๋ฐ์ดํŠธ ํ•  ๋•Œ ์—ฐ์ด์–ด์„œ ๊ธ€์„ ๋‚จ๊ธฐ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 ์ด์ƒ ์ผ€์ด์น˜์˜€์Šต๋‹ˆ๋‹ค.

 

์œˆ๋„์šฐ์—์„œ ์†Œ๋‚˜ํ๋ธŒ ์„œ๋ฒ„๋ฅผ ์‹คํ–‰์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ

$SonarQube Home$/bin/windows-x86-64/StartSonar.bat ํŒŒ์ผ์„ ์‹คํ–‰์‹œํ‚ค๋ฉด cmd์ฐฝ์— ์•„๋ž˜์ฒ˜๋Ÿผ ์—๋Ÿฌ๊ฐ€ ๋œจ๋Š”๋ฐ์š”


wrapper  | --> Wrapper Started as Console
wrapper  | Launching a JVM...
jvm 1    | Error occurred during initialization of VM
jvm 1    | GC triggered before VM initialization completed. Try increasing NewSize, current value 1536K.
wrapper  | JVM exited while loading the application.
wrapper  | JVM Restarts disabled.  Shutting down.
wrapper  | <-- p="" stopped="" wrapper="">

๊ณ„์†ํ•˜๋ ค๋ฉด ์•„๋ฌด ํ‚ค๋‚˜ ๋ˆ„๋ฅด์‹ญ์‹œ์˜ค . . .



์ฒ˜์Œ์—๋Š” sonar.properties์—์„œ ํ•œ์ฐธ ์ฐพ๋‹ค๊ฐ€ ์–ด๋–ค ๋ถ„์ด ํฌ์ŠคํŒ…ํ•œ ๊ธ€์„ ๋ณด๊ณ  ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค.


์œ„์™€ ๊ฐ™์€ ์—๋Ÿฌ๋‚˜ ๊ฐ€์˜ค๋ฉด
$SonarQube Home$/conf/wrapper.conf ์—์„œ ์•„๋ž˜ ์„ค์ •์„ ์ฐพ์•„์„œ ๊ฐ’์„ ์˜ฌ๋ ค์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.


(default)
wrapper.java.initmemory=3
wrapper.java.maxmemory=32


 

(after changing)

wrapper.java.initmemory=128
wrapper.java.maxmemory=256



๊ทธ๋Ÿผ ์ฆํ”„ ํ•˜์„ธ์š”~ ^-^

jQuery ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•ด์„œ ๋งŒ๋“  ํ”„๋ ˆ์ž„์›Œํฌ๋ผ๊ณ  ๋ณด์‹œ๋ฉด๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ธฐ๋Šฅ์„ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜์‹ค ์ˆ˜ ์žˆ๋‹ค๊ณ  ๋ณด์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” jQuery์—์„œ ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๊ฐœ๋…์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

String:

์ž๋ฐ”์˜ ์ŠคํŠธ๋ง์ฒ˜๋Ÿผ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์ŠคํŠธ๋ง๋˜ํ•œ ๋™์ผํ•œ ์†์„ฑ์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. immutable ์ด๋ž€ ๋ง์ด์ฃ . 

JavaScript ์—์„œ ์ŠคํŠธ๋ง์€ ์•„๋ž˜์™€ ๊ฐ™์€ ํ˜•์‹์„ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์Œ๋”ฐ์˜ดํ‘œ ๋˜๋Š” ํ™‘๋”ฐ์˜ดํ‘œ์•ˆ์— ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. 

"This is JavaScript String"
'This is JavaScript String'
'This is "really" a JavaScript String'
"This is 'really' a JavaScript String"

Numbers:

JavaScript์—์„œ ์ˆซ์ž๋Š” double-precision 64-bit format IEEE 754 ๊ฐ’์ž…๋‹ˆ๋‹ค. ์ŠคํŠธ๋ง๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ immutable์ž…๋‹ˆ๋‹ค.

JavaScript Numbers ์˜ˆ์ œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

5350
120.27
0.26

Boolean:

true , false ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด์ฃ . ์ˆซ์ž๊ฐ€ 0์ด๋ฉด, false์ด๊ณ  ์ŠคํŠธ๋ง์ด ๋น„์–ด์žˆ์–ด๋„ false์ž…๋‹ˆ๋‹ค.

์˜ˆ์ œ:

true      // true
false     // false
0         // false
1         // true
""        // false
"hello"   // true

Objects:

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฐ์ฒด ์ƒ์„ฑ ์˜ˆ์ œ:

var emp = {
   name: "Zara",
   age: 10
};

๊ฐ์ฒด์˜ ์†์„ฑ์ •๋ณด๋ฅผ ์ฝ๊ฑฐ๋‚˜ ์“ฐ๋Š” ์˜ˆ์ œ:

// Getting object properties
emp.name  // ==> Zara
emp.age   // ==> 10

// Setting object properties
emp.name = "Daisy"  // <== Daisy
emp.age  =  20      // <== 20

Arrays(๋ฐฐ์—ด):

๋ฐฐ์—ด์€ ์•„๋ž˜์ฒ˜๋Ÿผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

var x = [];
var y = [1, 2, 3, 4, 5];

๋ฐฐ์—ด์€ ๊ธธ์ด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฃจํ”„๋ฅผ ๋Œ๋ฆด๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋˜์ฃ .

var x = [1, 2, 3, 4, 5];
for (var i = 0; i < x.length; i++) {
   // Do something with x[i]
 }

Functions(ํ•จ์ˆ˜):

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ํ•จ์ˆ˜๋Š” named์™€ anonymous๋กœ ๋‚˜๋‰˜๋Š”๋ฐ์š” ํ•จ์ˆ˜ ์ด๋ฆ„์„ ์ง์ ‘ ์ค€๊ฒƒ์ด named๊ณ  ํ•จ์ˆ˜ ์ด๋ฆ„์ด ์—†๋Š” ๊ฒƒ์ด anonymous์ž…๋‹ˆ๋‹ค. ๋„ค์ž„๋“œ ํ•จ์ˆ˜๋Š” function ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์•„๋ž˜์ฒ˜๋Ÿผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

function named(){
  // do some stuff here
}

anonymous ํ•จ์ˆ˜๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ์ด๋ฆ„์—†์ด ์•„๋ž˜์ฒ˜๋Ÿผ ๋ณ€์ˆ˜์— ํ• ๋‹น๋  ์ˆ˜๋„ ์žˆ๊ณ  ๋‹ค๋ฅธ ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

var handler = function (){
  // do some stuff here
}

JQuery ์—์„œ ์ต๋ช… ํ•จ์ˆ˜๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ์ž์ฃผ ์‚ฌ์šฉ๋˜์–ด์ง‘๋‹ˆ๋‹ค.

$(document).ready(function(){
  // do some stuff here
});

Arguments(์•„๊ทœ๋จผํŠธ):

JavaScript variable arguments is a kind of array which has length property. Following example explains it very well:

function func(x){
  console.log(typeof x, arguments.length);
}
func();                //==> "undefined", 0
func(1);               //==> "number", 1
func("1", "2", "3");   //==> "string", 3

The arguments object also has a callee property, which refers to the function you're inside of. For example:

function func() {
   return arguments.callee; 
}
func();                // ==> func

Context(์ปจํ…์ŠคํŠธ):

JavaScript famous keyword this always refers to the current context. Within a function this context can change, depending on how the function is called:

$(document).ready(function() {
  // this refers to window.document
});

$("div").click(function() {
  // this refers to a div DOM element
});

You can specify the context for a function call using the function-built-in methods call() and apply() methods.

The difference between them is how they pass arguments. Call passes all arguments through as arguments to the function, while apply accepts an array as the arguments.

function scope() {
  console.log(this, arguments.length);
}

scope() // window, 0
scope.call("foobar", [1,2]);  //==> "foobar", 1
scope.apply("foobar", [1,2]); //==> "foobar", 2

Scope(๋ฒ”์œ„):

The scope of a variable is the region of your program in which it is defined. JavaScript variable will have only two scopes.

  • Global Variables: A global variable has global scope which means it is defined everywhere in your JavaScript code.

  • Local Variables: A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.

Within the body of a function, a local variable takes precedence over a global variable with the same name:

var myVar = "global";     // ==> Declare a global variable

function ( ) {
   var myVar = "local";   // ==> Declare a local variable
   document.write(myVar); // ==> local
}

Callback(์ฝœ๋ฐฑ):

A callback is a plain JavaScript function passed to some method as an argument or option. Some callbacks are just events, called to give the user a chance to react when a certain state is triggered.

jQuery's event system uses such callbacks everywhere for example:

$("body").click(function(event) {
   console.log("clicked: " + event.target);
 });

Most callbacks provide arguments and a context. In the event-handler example, the callback is called with one argument, an Event.

Some callbacks are required to return something, others make that return value optional. To prevent a form submission, a submit event handler can return false as follows:

$("#myform").submit(function() {
   return false;
 });

Closures(ํด๋กœ์ ธ):

Closures are created whenever a variable that is defined outside the current scope is accessed from within some inner scope.

Following example shows how the variable counter is visible within the create, increment, and print functions, but not outside of them:

function create() {
  var counter = 0;
  return {
    increment: function() {
      counter++;
    },
    print: function() {
      console.log(counter);
    }
  }
}
var c = create();
c.increment();
c.print();     // ==> 1

This pattern allows you to create objects with methods that operate on data that isn't visible to the outside world. It should be noted that data hiding is the very basis of object-oriented programming.

Proxy Pattern(ํ”„๋ก์‹œํŒจํ„ด):

A proxy is an object that can be used to control access to another object. It implements the same interface as this other object and passes on any method invocations to it. This other object is often called the real subject.

A proxy can be instantiated in place of this real subject and allow it to be accessed remotely. We can saves jQuery's setArray method in a closure and overwrites it as follows:

(function() {
  // log all calls to setArray
  var proxied = jQuery.fn.setArray;

  jQuery.fn.setArray = function() {
    console.log(this, arguments);
    return proxied.apply(this, arguments);
  };
})();

The above wraps its code in a function to hide the proxied variable. The proxy then logs all calls to the method and delegates the call to the original method. Using apply(this, arguments) guarantees that the caller won't be able to notice the difference between the original and the proxied method.

Built-in Functions:

JavaScript comes along with a useful set of built-in functions. These methods can be used to manipulate Strings, Numbers and Dates.

Following are important JavaScript functions:

MethodDescription
charAt()Returns the character at the specified index.
concat()Combines the text of two strings and returns a new string.
forEach()Calls a function for each element in the array.
indexOf()Returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found.
length()Returns the length of the string.
pop()Removes the last element from an array and returns that element.
push()Adds one or more elements to the end of an array and returns the new length of the array.
reverse()Reverses the order of the elements of an array -- the first becomes the last, and the last becomes the first.
sort()Sorts the elements of an array.
substr()Returns the characters in a string beginning at the specified location through the specified number of characters.
toLowerCase()Returns the calling string value converted to lower case.
toString()Returns the string representation of the number's value.
toUpperCase()Returns the calling string value converted to uppercase.

A complete list of built-in function is available here : Built-in Functions.

๋ฌธ์„œ ๊ฐ์ฒด ๋ชจ๋ธ ( DOM ):

๋ฌธ์„œ๊ฐ์ฒด๋ชจ๋ธ์ด๋ผ๋Š” ๊ฒƒ์€ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‹ค์–‘ํ•œ html ์š”์†Œ๋“ค์˜ ํŠธ๋ฆฌ๊ตฌ์กฐ๋ฅผ ๋งํ•ฉ๋‹ˆ๋‹ค.

<html>
<head>
   <title>the title</title>
</head>
<body>
   <div>
      <p>This is a paragraph.</p>
      <p>This is second paragraph.</p>
      <p>This is third paragraph.</p>
   </div>
</body>
</html>

Following are the important points about the above tree structure:

  • The <html> is the ancestor of all the other elements; in other words, all the other elements are descendants of <html>.

  • The <head> and <body> elements are not only descendants, but children of <html>, as well.

  • Likewise, in addition to being the ancestor of <head> and <body>, <html> is also their parent.

  • The <p> elements are children (and descendants) of <div>, descendants of <body> and <html>, and siblings of each other <p> elements.



 ๊ทธ๋ƒฅ html ๊ธฐ๋ณธ์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•˜๊ณ  ์žˆ๋Š”๊ฑฐ์ฃ . ์ด์ •๋„๋Š” ๋‹ค ์•„์‹œ๋ฆฌ๋ผ๋ณด๊ณ  ๋ฒˆ์—ญ์•ˆํ•ฉ๋‹ˆ๋‹ค ใ…Ž


 

 

 

 

Reference : http://www.tutorialspoint.com/jquery/jquery-basics.htm