Study: Software(SW)/SW: DataStructure & DesignPattern

[Design Pattern] GoF 디자인 패턴 (Design Patterns) 개요

DrawingProcess 2022. 6. 15. 21:10
반응형

GoF 디자인 패턴(Design Patterns)


  • 디자인 패턴은 많은 개발자들이 직면하는 문제를 해결하기 위한 방법을 모아 체계적으로 일반화한 것입니다.
  • 일부 상속이 아닌 구조도 있지만 핵심은 "문제 해결을 위해 다형성을 이용하고 객체 간에 어떤 관계를 만들 것인가"이며, 세분화된 역할이나 모듈들 간의 인터페이스 구현 방식을 설계할때 참조할 수 있는 전형적인 해결 방식을 말한다.
  • 따라서 디자인 패턴을 통해 설계 문제, 해결 방법, 해결 방법을 언제 적용해야 할지, 그 결과는 무엇인지 등을 알 수 있다.
  • 디자인 패턴은 자신이 사용하는 프로그램 언어를 충분히 이해하고 직접 처음부터 끝까지 프로그램을 개발한 경험이 있는 경우 매우 도움이 됩니다.
  • 디자인 패턴은 필요한 것이 아니라 자주 발생하는 상황에 대한 좋은 방법을 알려 주는 것입니다.
  • 물론 디자인 패턴은 대표적으로 빈도가 높은 것일 뿐 실제 프로젝트에서는 조합하고 응용한 형태로 적용됩니다.

 

GoF 디자인 패턴 분류


  • 각각의 패턴이 어떤 일을 하기 위한 것인지(목적)에 따라 분류: '생성(Creational)', '구조(Structural)', '행동(Behavioral)'
    • 생성 패턴: 객체의 생성 과정에 관여
    • 구조 패턴: 객체의 합성에 관여
    • 행동 패턴: 객체가 상호작용하는 방법이나 관심사를 분리하는 방법에 관여
  • 패턴을 주로 클래스에 적용하는지, 객체에 적용하는 지 구분하는 것(범위)에 따라 분류: '클래스(class)', '객체(object)'
    • 클래스 패턴: 클래스와 서브클래스 간의 관련성을 다룬다. 주로 상속을 통해 관련되며, 컴파일 타임에 정적으로 결정된다.
    • 객체 패턴: 객체 간의 관련성을 다루고, 런타임에 변경될 수 있는 동적인 성격을 가진다.

 

생성 패턴(Creational Patterns)


추상 팩토리(Abstract Factory)

  • Lets you produce families of related objects without specifying their concrete classes.
  • 구체적인 클래스를 지정하지 않고 인터페이스를 통해 서로 연관되는 객체들을 그룹으로 표현함

빌더(Builder)

  • Lets you construct complex objects step by step. The pattern allows you to produce different types and representations of an object using the same construction code.
  • 복합 객체의 생성과 표현을 분리하여 동일한 생성 절차에서도 다른 표현 결과를 만들어낼 수 있음

팩토리메서드(Factory Methods)

  • Provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.
  • 객체 생성을 서브클래스로 위임하여 캡슐화함

프로토타입(Prototype)

  • Lets you copy existing objects without making your code dependent on their classes.
  • 원본 객체를 복사함으로써 객체를 생성함

싱글턴(Singleton)

  • Lets you ensure that a class has only one instance, while providing a global access point to this instance.
  • 어떤 클래스의 인스턴스는 하나임을 보장하고 어디서든 참조할 수 있도록 함

 

구조 패턴(Structural Patterns)


어댑터(Adapter)

  • Allows objects with incompatible interfaces to collaborate.
  • 클래스의 인터페이스를 다른 인터페이스로 변환하여 다른 클래스가 이용할 수 있도록 함

브릿지(Bridge)

  • Lets you split a large class or a set of closely related classes into two separate hierarchies—abstraction and implementation—which can be developed independently of each other.
  • 구현부에서 추상층을 분리하여 각자 독립적으로 확장할 수 있게 함

콤포지트(Composite)

  • Lets you compose objects into tree structures and then work with these structures as if they were individual objects.
  • 객체들의 관계를 트리 구조로 구성하여 복합 객체와 단일 객체를 구분없이 다룸

데코레이터(Decorator)

  • Lets you attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors.
  • 주어진 상황 및 용도에 따라 어떤 객체에 다른 객체를 덧붙이는 방식

퍼사드(Facade)

  • Provides a simplified interface to a library, a framework, or any other complex set of classes.
  • 서브시스템에 있는 인터페이스 집합에 대해 하나의 통합된 인터페이스(Wrapper) 제공

플라이웨이트(Flyweight)

  • Lets you fit more objects into the available amount of RAM by sharing common parts of state between multiple objects instead of keeping all of the data in each object.
  • 크기가 작은 여러 개의 객체를 매번 생성하지 않고 가능한 한 공유할 수 있도록 하여 메모리를 절약함

프록시(Proxy)

  • Lets you provide a substitute or placeholder for another object. A proxy controls access to the original object, allowing you to perform something either before or after the request gets through to the original object.
  • 접근이 어려운 객체로의 접근을 제어하기 위해 객체의 Surrogate나 Placeholder를 제공

 

행위 패턴(Behavioral Patterns)


인터프리터(Interpreter)

  • A way to include language elements in a program.
  • 특정 언어의 문법 표현을 정의함

템플릿 메서드(Template Method)

  • Defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure.
  • 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.
  • 상위클래스는 알고리즘의 골격만을 작성하고 구체적인 처리는 서브클래스로 위임함

책임 연쇄(Chain of Responsibility)

  • Lets you pass requests along a chain of handlers. Upon receiving a request, each handler decides either to process the request or to pass it to the next handler in the chain.
  • A way of passing a request between a chain of objects.
  • 요청을 받는 객체를 연쇄적으로 묶어 요청을 처리하는 객체를 만날 때까지 객체 Chain을 따라 요청을 전달함

커맨드(Command)

  • Turns a request into a stand-alone object that contains all information about the request. This transformation lets you pass requests as a method arguments, delay or queue a request's execution, and support undoable operations.
  • Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
  • 요청을 객체의 형태로 캡슐화하여 재사용하거나 취소할 수 있도록 저장함

반복자(iterator)

  • Lets you traverse elements of a collection without exposing its underlying representation (list, stack, tree, etc.).
  • Sequentially access the elements of a collection.
  • 내부를 노출하지 않고 접근이 잦은 어떤 객체의 원소를 순차적으로 접근할 수 있는 동일한 인터페이스 제공

중재자(Mediator)

  • Lets you reduce chaotic dependencies between objects. The pattern restricts direct communications between the objects and forces them to collaborate only via a mediator object.
  • Defines simplified communication between classes.
  • 한 집합에 속해있는 객체들의 상호작용을 캡슐화하여 새로운 객체로 정의

메멘토(Memonto)

  • Lets you save and restore the previous state of an object without revealing the details of its implementation.
  • Capture and restore an object's internal state.
  • 객체가 특정 상태로 다시 되돌아올 수 있도록 내부 상태를 실체화

옵서버(Observer)

  • Lets you define a subscription mechanism to notify multiple objects about any events that happen to the object they're observing.
  • Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
    • Subject: knows its observers.
    • Observer: defines an updating interface for object that should be notified of changes in a subject.
  • 객체 상태가 변할 때 관련 객체들이 그 변화를 통지받고 자동으로 갱신될 수 있게 함

스테이트(State)

  • Lets an object alter its behavior when its internal state changes. It appears as if the object changed its class.
  • Alter an object's behavior when its state changes.
  • 객체의 상태에 따라 동일한 동작을 다르게 처리해야할 때 사용

전략(Strategy)

  • Lets you define a family of algorithms, put each of them into a separate class, and make their objects interchangeable.
  • Define a family of algorithm, encapsulate each one, and make them interchangeable. Strategy lets algorithm vary independently from clients that use it.
    • Example: Duck class with encapsulated behaviors such as Flybehavior and Quackbehavior.
  • 객체의 상태에 따라 동일한 동작을 다르게 처리해야할 때 사용

방문자(Visitor)

  • Lets you separate algorithms from the objects on which they operate.
  • Defines a new operation to a class without change.
  • 객체의 원소에 대해 수행할 연산을 분리하여 별도의 클래스로 구성함

참고



 

반응형