[μ΄νŽ™ν‹°λΈŒ μžλ°”] Item18 μ™„λ²½κ³΅λž΅. λ°μ½”λ ˆμ΄ν„° νŒ¨ν„΄ Decorator Pattern

2023. 3. 2. 16:24ㆍJAVA/Effective JAVA

728x90

 

item18. μƒμ†λ³΄λ‹€λŠ” μ»΄ν¬μ§€μ…˜μ„ μ‚¬μš©ν•˜λΌ.

" p.119 λ°μ½”λ ˆμ΄ν„° νŒ¨ν„΄"

[μ΄νŽ™ν‹°λΈŒ μžλ°”] Item18. μƒμ†λ³΄λ‹€λŠ” μ»΄ν¬μ§€μ…˜μ„ μ‚¬μš©ν•˜λΌ. (tistory.com)

 

[μ΄νŽ™ν‹°λΈŒ μžλ°”] Item18. μƒμ†λ³΄λ‹€λŠ” μ»΄ν¬μ§€μ…˜μ„ μ‚¬μš©ν•˜λΌ.

[μ΄νŽ™ν‹°λΈŒ μžλ°”] Item18. μƒμ†λ³΄λ‹€λŠ” μ»΄ν¬μ§€μ…˜μ„ μ‚¬μš©ν•˜λΌ. 상속을 μ‚¬μš©ν•˜λ©΄ μ½”λ“œλ₯Ό μž¬μ‚¬μš©ν•  수 μžˆμ§€λ§Œ, 잘λͺ» μƒμ†ν•˜λ©΄ 였λ₯˜κ°€ λ°œμƒν•  수 μžˆλ‹€. μƒμœ„ ν΄λž˜μŠ€μ™€ ν•˜μœ„ ν΄λž˜μŠ€κ°€ λͺ¨λ‘ 같은 ν”„λ‘œκ·Έλž˜

hyejin.tistory.com

 

 

 

λ°μ½”λ ˆμ΄ν„° νŒ¨ν„΄ Decorator Pattern μ΄λž€? 

: κΈ°μ‘΄ μ½”λ“œλ₯Ό λ³€κ²½ν•˜μ§€ μ•Šκ³  λΆ€κ°€ κΈ°λŠ₯을 μΆ”κ°€ν•˜λŠ” νŒ¨ν„΄, 객체의 결합을 톡해 κΈ°λŠ₯을 λ™μ μœΌλ‘œ μœ μ—°ν•˜κ²Œ ν™•μž₯ν•  수 있게 ν•΄μ£ΌλŠ” νŒ¨ν„΄μ΄λ‹€. 

λ°μ½”λ ˆμ΄ν„° νŒ¨ν„΄μ€ 상속이 μ•„λ‹Œ μœ„μž„μ„ μ‚¬μš©ν•΄μ„œ 보닀 μœ μ—°ν•˜κ²Œ(λ™μ μœΌλ‘œ, λŸ°νƒ€μž„μ—) λΆ€κ°€ κΈ°λŠ₯을 μΆ”κ°€ν•˜λŠ” 것도 κ°€λŠ₯ν•˜λ‹€. 

 

 

Component

: κΈ°λ³Έ κΈ°λŠ₯ 

 

ConcreateComponent 

: κΈ°λ³Έ κΈ°λŠ₯을 κ΅¬ν˜„ν•˜λŠ” 클래슀 

 

Decorator 

: λ§Žμ€ μˆ˜κ°€ μ‘΄μž¬ν•˜λŠ” ꡬ체적인 Decorator의 곡톡 κΈ°λŠ₯을 μ •μ˜ 

 

ConcreateDecorator1, ConcreateDecorator2 

: Decorator의 ν•˜μœ„ 클래슀둜, κΈ°λ³Έ κΈ°λŠ₯에 μΆ”κ°€λ˜λŠ” κ°œλ³„μ μΈ κΈ°λŠ₯을 λœ»ν•œλ‹€. 

 

 

public class ForwardingSet<E> implements Set<E>
{
   private final Set<E> s;
   
   public ForwardingSet(Set<E> s)
   {
      this.s = s;
   }
   
   public void clear()
   {
      s.clear();
   }
   
   public boolean contains(Object o)
   {
      return s.contains(o);
   }
   
   public boolean isEmpty()
   {
      return s.isEmpty();
   }
   
   public int size()
   {
      return s.size();
   }
   
   public Iterator<E> iterator()
   {
      return s.iterator();
   }
   
   public boolean add(E e)
   {
      return s.add(e);
   }
   
   public boolean remove(Object o)
   {
      return s.remove(o);
   }
   
   public boolean containsAll(Collection<?> collection)
   {
      return s.containsAll(collection);
   }
   
   public boolean addAll(Collection<? extends E> collection)
   {
      return s.addAll(collection);
   }
   
   public boolean removeAll(Collection<?> c)
   {
      return s.removeAll(c);
   }
   
   public boolean retainAll(Collection<?> collection)
   {
      return s.retainAll(collection);
   }
   
   public Object[] toArray()
   {
      return s.toArray();
   }
   
   public <T> T[] toArray(T[] a)
   {
      return s.toArray(a);
   }
   
   
}

 

public class InstrumentedSet<E> extends ForwardingSet<E> 
{
   private int addCount = 0;
   public InstrumentedSet(Set<E> s)
   {
      super(s);
   }
   
   @Override
   public boolean add(E e)
   {
      addCount++;
      return super.add(e);
   }
   
   @Override
   public boolean addAll(Collection<? extends E> collection)
   {
      addCount += collection.size();
      return super.addAll(collection);
   }
   
   public int getAddCount()
   {
      return addCount;
   }
   
   public static void main(String[] args)
   {
      InstrumentedSet<String> s = new InstrumentedSet<>(new HashSet<>());
      s.addAll(List.of("ν‹±", "탁탁", "νŽ‘"));
      System.out.println("s.getAddCount() = " + s.getAddCount());
   }
}

InstrumentedSet, ForwardingSet, Set, HashSet을 톡해 λ°μ½”λ ˆμ΄ν„° νŒ¨ν„΄μ„ λ³Έλ‹€λ©΄ μš°μ„  

Set이 Component에 ν•΄λ‹Ήν•˜κ³ , HashSet은 이λ₯Ό κ΅¬ν˜„ν•œ ConcreateComponent에 ν•΄λ‹Ήν•œλ‹€. 

그리고 Component에 λΆ€κ°€ κΈ°λŠ₯을 μΆ”κ°€ν•˜κ³  ForwardingSet이 Decorator에 ν•΄λ‹Ήν•˜λ©°, 이λ₯Ό Decorator의 ν•˜μœ„ 클래슀둜  여기에 λΆ€κ°€ κΈ°λŠ₯을 μΆ”κ°€ν•œ ConcreateDecoratorκ°€ InstrumentedSet이 λœλ‹€. 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90