4차산업혁명의 일꾼/Java&Spring웹개발과 서버 컴퓨터

디자인패턴 4 동일시하기-equate : (Decorator)와 위임하기(Composite)

르무엘 2023. 3. 3. 07:15

https://github.com/MyoungSoo7/DesignPattern

 

GitHub - MyoungSoo7/DesignPattern

Contribute to MyoungSoo7/DesignPattern development by creating an account on GitHub.

github.com

 

디자인패턴 4 동일시하기(Decorator)와 위임하기(Composite)

 

Decorator Pattern

  • 장식과 실제 내용물을 동일시
  • 객체에 동적으로 책임을 추가

 

  • 상속을 사용하지 않고 기능의 유연한 확장이 가능한 패턴
  • 객체에 동적으로 새로운 서비스를 추가 할 수 있음
  • 전체가 아닌 개별적인 객체에 새로운 기능을 추가 할 수 있다

 

 

  • 단순한 상속보다 설계의 융통성을 증대
  • Decorator의 조합을 통해 새로운 서비스를 지속적으로 추가할 수 있음
  • 필요없는 경우 Decorator를 삭제할 수 있음
  • Decorator와 실제 컴포넌트는 동일한 것이 아님
  • 작은 규모의 객체들이 많이 생성될 수 있음
  • 자바의 I/O 스트림 클래스는 Decorator 패턴임

 

package com.backend.bakckend.designpattern.equate;


abstract class Coffee{

    public abstract void brewing();
}

class EtiopiaAmericano extends Coffee{

    @Override
    public void brewing() {
        System.out.println("EtiopiaAmericano");
    }
}

class KeynaAmericano extends Coffee{

    @Override
    public void brewing() {
        System.out.println("KeynaAmericano");
    }
}

abstract class DecoratorPattern extends Coffee{
    Coffee coffee;

    public DecoratorPattern(Coffee coffee){
        this.coffee = coffee;
    }

    @Override
    public void brewing() {
        coffee.brewing();
    }
}

class Latte extends DecoratorPattern{

    public Latte(Coffee coffee) {
        super(coffee);
    }

    @Override
    public void brewing() {
        super.brewing();
        System.out.println("Adding Milk");
    }
}

class Mocha extends DecoratorPattern{

    public Mocha(Coffee coffee) {
        super(coffee);
    }

    @Override
    public void brewing() {
        super.brewing();
        System.out.println("Adding Mocha Syrup");
    }
}

class WhippedCream extends DecoratorPattern{

    public WhippedCream(Coffee coffee) {
        super(coffee);
    }

    @Override
    public void brewing() {
        super.brewing();
        System.out.println("Adding Whipped Cream");
    }
}

public class Decorator {

    public static void main(String[] args) {

        Coffee kenyaAmerikano = new KeynaAmericano();
        kenyaAmerikano.brewing();
        System.out.println();

        Coffee kenyaLatte = new Latte(kenyaAmerikano);
        kenyaLatte.brewing();
        System.out.println();

        Mocha kenyaMocha = new Mocha(new Latte(new KeynaAmericano()));
        kenyaMocha.brewing();
        System.out.println();

        WhippedCream etiopiaWhippedMocha = new WhippedCream(new Mocha(new Latte(new EtiopiaAmericano())));
        etiopiaWhippedMocha.brewing();
        System.out.println();


    }


}

 

 

Composite Pattern

 

  • -- 전체와 부분 객체에서 공통적으로 사용할 인터페이스 선언
  • -- 전체 클래스가 부분요소들을 관리하기 위해 필요한 인터페이스 선언
  • -- 전체와 부분 객체에서 공통으로 사용할 기능 구현

 

 

package com.backend.bakckend.designpattern.equate;


import java.util.ArrayList;

abstract class ProductCategory {
    int id;
    String name;
    int price;

    public ProductCategory(int id, String name, int price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    public abstract void addProduct(ProductCategory productCategory);

    public abstract void removeProduct(ProductCategory productCategory);

    public abstract int getCount();

    public abstract int getPrice();

    public abstract int getId();

    public abstract String getName();
}

class Product extends ProductCategory {

    public Product(int id, String name, int price) {
        super(id, name, price);
    }

    @Override
    public void addProduct(ProductCategory productCategory) {
        System.out.println("Can't add product");
    }

    @Override
    public void removeProduct(ProductCategory productCategory) {
        System.out.println("Can't remove product");
    }

    @Override
    public int getCount() {
        return 1;
    }

    @Override
    public int getPrice() {
        return price;
    }

    @Override
    public int getId() {
        return id;
    }

    @Override
    public String getName() {
        return name;
    }
}


class Category extends ProductCategory {
    ArrayList<ProductCategory> productCategories;

    public Category(int id, String name, int price) {
        super(id, name, price);
        productCategories = new ArrayList<ProductCategory>();
    }

    @Override
    public void addProduct(ProductCategory productCategory) {
        productCategories.add(productCategory);
    }

    @Override
    public void removeProduct(ProductCategory productCategory) {
        for (ProductCategory temp : productCategories) {
            if (temp.getId() == productCategory.getId()){
                productCategories.remove(temp);
                return;
            }

        }
    }

    @Override
    public int getCount() {
        int count = 0;
        for (ProductCategory temp : productCategories) {
            count += temp.getCount();
        }
        return count;
    }

    @Override
    public int getPrice() {
        int price = 0;
        for (ProductCategory temp : productCategories) {
            price += temp.getPrice();
        }
        return price;
    }


    @Override
    public int getId() {
        return id;
    }

    @Override
    public String getName() {
        return name;
    }

}


public class Composite {

    public static void main(String[] args) {
        ProductCategory womanCategory = new Category(1, "lady", 0);
        ProductCategory manCategory   = new Category(2, "man", 0);

        ProductCategory clothesCategoryW = new Category(2345,"clothes",0);
        ProductCategory bagCategoryW = new Category(2346,"bag",0);
        ProductCategory shoesCategoryW = new Category(2347,"shoes",0);

        womanCategory.addProduct(clothesCategoryW);
        womanCategory.addProduct(bagCategoryW);
        womanCategory.addProduct(shoesCategoryW);

        ProductCategory clothesCategoryM = new Category(2348,"clothes",0);
        ProductCategory bagCategoryM = new Category(2349,"bag",0);
        ProductCategory shoesCategoryM = new Category(2350,"shoes",0);

        manCategory.addProduct(clothesCategoryM);
        manCategory.addProduct(bagCategoryM);
        manCategory.addProduct(shoesCategoryM);

        ProductCategory shoes1 = new Product(2351,"nike",1);
        ProductCategory shoes2 = new Product(2352,"adidas",2);
        ProductCategory shoes3 = new Product(2353,"gucci",3);
        ProductCategory shoes4 = new Product(2354,"puma",4);
        ProductCategory shoes5 = new Product(2355,"prada",5);
        ProductCategory shoes6 = new Product(2356,"chanel",6);

        shoesCategoryW.addProduct(shoes1);
        shoesCategoryW.addProduct(shoes2);
        shoesCategoryW.addProduct(shoes3);

        shoesCategoryM.addProduct(shoes4);
        shoesCategoryM.addProduct(shoes5);
        shoesCategoryM.addProduct(shoes6);

        ProductCategory bag1 = new Product(2351,"lv",100);
        ProductCategory bag2 = new Product(2352,"gucci",200);
        ProductCategory bag3 = new Product(2353,"chanel",300);
        ProductCategory bag4 = new Product(2354,"dior",400);
        ProductCategory bag5 = new Product(2355,"prada",500);
        ProductCategory bag6 = new Product(2356,"puma",600);

        bagCategoryW.addProduct(bag1);
        bagCategoryW.addProduct(bag2);
        bagCategoryW.addProduct(bag3);

        bagCategoryM.addProduct(bag4);
        bagCategoryM.addProduct(bag5);
        bagCategoryM.addProduct(bag6);

        System.out.println(womanCategory.getCount());
        System.out.println(womanCategory.getPrice());
        System.out.println(manCategory.getCount());
        System.out.println(manCategory.getPrice());

    }
}
LIST