Programming/c++ Design Pattern14 [C++ Design Pattern] Adapter Adapter는 사실 중간자 역할을 해주는 간접층을 하나 생성하는 것이라고 생각하면 된다. #include #include #include class TextView { std::string data; public: TextView(const std::string& s) : data(s) {} void show() { std::cout 2023. 11. 30. [C++ Design Pattern] Decorator Decorator 패턴은 객체 지향 디자인 패턴 중 하나. => 기능을 동적으로 추가하거나 확장 할 수 있게 도와주는 패턴 상속을 사용해서 기능을 확장하고, 객체를 감싸서 포장 하는 느낌으로 기능을 추가할 수 있다. Decorator 패턴은 크게 3가지의 구성 요소로 나뉘는데 1. Component Class => Interface를 제공 2. Concrete Component Class => 기능을 추가하고자 하는 객체 3. Decorator class => 기능을 추가시켜주는 class Picture를 하나 찍을건데, picture에 frame도 씌우고 emoticon도 추가하고 싶다. 아래 코드를 보면 #include #include struct Component { virtual void draw.. 2023. 11. 29. [C++ Design Pattern] Composite Composite 패턴은 객체들을 트리 구조로 구성하여 개별 객체와 복합 객체(객체들의 집합)를 동일하게 취급할 수 있도록 하는 디자인 패턴인데, 객체들 간에 계층 구조를 만들어 트리로 표현하면서 개별 객체와 복합 객체를 일관성 있게 다룰 수 있게 도와줘. #include #include #include class Item { std::string name; public: Item(const std::string& name) : name(name) {} virtual ~Item() {} virtual int get_size() = 0; }; class File : public Item { int size; public: File(const std::string& name, int size) : Item(.. 2023. 11. 29. [C++ Design Pattern] Strategy Pattern strategy pattern(전략 패턴) template struct IAllocator { virtual T* allocate(int size) = 0; virtual void deallocate(T* p, int size) = 0; virtual ~IAllocator() {} }; template class vector { T* buff; IAllocator* ax; public: void set_allocator(IAllocator* p) { ax = p; } void resize(int n) { buff = ax->allocate(n); ax->deallocate(buff, n); } }; template class newAllocator : public IAllocator { public: T.. 2023. 11. 29. 이전 1 2 3 4 다음