스프링(Spring) 프레임워크 기본 개념 강좌 (7) - Patterns
 

7) 패턴(Patterns)

 

패턴은 말 그대로 어떤 일정한 형태나 양식 또는 유형을 뜻함.

패턴이라는 개념은 스프링 프레임워크에 한정된 것이 아니라 개발 디자인(Development Design)에 대해 사용되는 일반적인 개념 중의 하나.

 

개발 디자인 패턴에는 다음의 2가지가 존재.

 

 * 데코레이터 패턴 :

 타깃의 코드에 손 대지 않고, 클라이언트가 호출하는 방법도 변경하지 않은 채로 새로운 기능을 추가할 때 유용한 방법. 
 핵심 코드에 부가적인 기능을 추가하기 위해서 런타임시 다이나믹하게 추가되는 프록시를 사용. 즉 동일한 인터페이스를 구현한 여러 개의 객체를 사용하는 것.
 주어진 상황 및 용도에 따라 어떤 객체에 책임을 덧붙이는 패턴. 기능 확장이 필요할 때 서브클래싱(Subclassing) 대신 쓸 수 있는 유연한 대안이 될 수 있음.
 동적으로 객체의 추가적인 기능들을 가진 객체를 덧붙여 꾸밈.


 

 * 프록시 패턴 :

 타깃의 기능 자체에는 관여하지 않으면서 접근하는 방법을 제어해주는 프록시를 이용하는 방법.

위의 2가지 패턴의 차이점은 프록시의 경우는 실제 실행될 타깃을 확장하거나 기능을 추가하는 것이 아니라, 단지 타깃에 접근하는 방법 자체를 프록시를 통하여 가능하게 하는 것이고, 데코레이터는 실행 타깃의 확장을 의미함.

 

 

데코레이터 패턴에 대한 예제 코드 : 출처 - 위키피디아 (http://en.wikipedia.org/wiki/Decorator_pattern)

 

// Windows Interface & Simple Window Class

interface Window {

    public void draw();   // draws the Window

    // returns a description of the Window

    public String getDescription();   

}

 

class SimpleWindow implements Window {

    public void draw() {

        // draw window

    }

    public String getDescription() {

        return "simple window";

    }

}

 

// Decorators

abstract class WindowDecorator implements Window {

    protected Window decoratedWindow; // the Window being decorated

public WindowDecorator (Window decoratedWindow)  {

        this.decoratedWindow = decoratedWindow;

    }

}

class VerticalScrollBarDecorator extends WindowDecorator {

    public VerticalScrollBarDecorator (Window decoratedWindow) {

        super(decoratedWindow);

    }

    public void draw() {

        drawVerticalScrollBar();

        decoratedWindow.draw();

    }

    private void drawVerticalScrollBar() { // draw the vertical scrollbar

    }

    public String getDescription() {

        return decoratedWindow.getDescription() + ", including vertical scrollbars";

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

    }

}

class HorizontalScrollBarDecorator extends WindowDecorator {

    public HorizontalScrollBarDecorator (Window decoratedWindow) {

        super(decoratedWindow);

    }

    public void draw() {

        drawHorizontalScrollBar();

        decoratedWindow.draw();

    }

    private void drawHorizontalScrollBar() { // draw the horizontal scrollbar

    }

    public String getDescription() {

        return decoratedWindow.getDescription() + ", including horizontal scrollbars";

    }

}

 

 

// Decorator Pattern Example

public class DecoratedWindowTest {

    public static void main(String[] args) {

        // create a decorated Window with horizontal and vertical scrollbars

        Window decoratedWindow = new HorizontalScrollBarDecorator (

                new VerticalScrollBarDecorator(new SimpleWindow()));

 

        // print the Window's description

        System.out.println(decoratedWindow.getDescription());

    }

}

 

윈도우에 대한 인터페이스(interface Window)와 클래스(class SimpleWindow),

그리고 수직 스크롤바가 있는 윈도우 클래스(class VerticalScrollBarDecorator), <- 데코레이터

수평 스크롤바가 있는 윈도우 클래스(class HorizontalScrollBarDecorator) <- 데코레이터

 

기존 윈도우(Simple Window) 클래스를 감싸(implements) 스크롤이 추가된 클래스를 새로 재정의함.

(기존 클래스에 장식(데코레이팅)된 형태로 클래스를 정의)

 

데코레이터 패턴의 단점 :

- 잡다한 클래스가 많아지고, 겹겹이 에워싼 형태의 구조로 구조가 복잡해지면 객체의 정체를 알기 어려움.

 

데코레이터 패턴의 장점 :

- 기존 코드는 수정하지 않고, 확장 및 추가가 가능함.

 

 

패턴의 종류에는 옵저버 패턴, 데코레이터 패턴, 프록시 패턴, 팩토리 패턴, 싱글턴 패턴, 커맨드 패턴, 어댑터 패턴, 퍼사드 패턴, 템플릿 패턴 등등 다양하며 좀 더 자세히 알고 싶은 경우에 Head First Design Patterns (저자 에릭 프리먼) 서적을 추천함.

데코레이터 패턴 위키피디아 : http://en.wikipedia.org/wiki/Decorator_pattern

 

 



출처: http://ooz.co.kr/206 [이러쿵저러쿵]

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
49 org.eclipse.ui.navigator Class CommonViewer 졸리운_곰 2019.07.04 398
48 MessageBox Example : Dialog « SWT JFace Eclipse « Java file 졸리운_곰 2019.06.16 362
47 Add Table Selection Listener and Get Selected TableItem : Table Event « SWT « Java Tutorial file 졸리운_곰 2019.06.16 372
46 Building and delivering a table editor with SWT/JFace file 졸리운_곰 2019.06.16 430
45 Demonstrates CellEditors : Table « SWT JFace Eclipse « Java file 졸리운_곰 2019.06.16 279
44 Demonstrates TableViewers : Table « SWT JFace Eclipse « Java file 졸리운_곰 2019.06.16 323
43 Eclipse RCP Tutorial: How to Add a Progress Bar file 졸리운_곰 2015.11.07 457
42 SWT Custom Widgets - Tutorial file 졸리운_곰 2015.08.23 379
41 Show a tool tip inside a rectangle : ToolTip « SWT « Java Tutorial file 졸리운_곰 2015.08.22 285
40 [SWT, Java] Tooltip example 졸리운_곰 2015.08.22 270
39 [SWT, Java], Button by Image, 이미지로 버튼 생성 졸리운_곰 2015.08.22 416
38 [SWT] Image Button 졸리운_곰 2015.08.22 277
37 [SWT] eventListener에서 부모 class (이벤트발생 클래스) 얻기 졸리운_곰 2015.08.16 340
36 [SWT] MessageBox Example file 졸리운_곰 2015.08.16 494
35 [SWT] How to create your own dialog classes file 졸리운_곰 2015.08.16 347
34 [SWT] Number Input Dialog file 졸리운_곰 2015.08.16 275
33 [SWT] Demonstrates a Canvas file 졸리운_곰 2015.08.12 268
32 SWT Control in One Example file 졸리운_곰 2015.08.10 403
31 SWT Tree With Multi columns file 졸리운_곰 2015.08.06 373
30 SWT Tree Composite 졸리운_곰 2015.08.06 289
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED