상용 디자인 모드 - 교체 기 및 조합 모드

4522 단어 디자인 모드
package org.design.patterns;

import java.util.ArrayList;
import java.util.List;

/**
 *     :
 *                      ,          。 
 *
 */

//   java.uitil.Iterator      
public interface Iterator<E> {
    boolean hasNext();
    E next();
    void remove();
}

/*
 *    java.util.AbstractList,    Iterator   ,java              ,        
*/
abstract class AbstractList<E> extends AbstractCollection<E> implements List<E> {
 protected AbstractList() {
 }
 public boolean add(E e) {
  add(size(), e);
  return true;
 }
 abstract public E get(int index);
 public E set(int index, E element) {
  throw new UnsupportedOperationException();
 }
 public void add(int index, E element) {
  throw new UnsupportedOperationException();
 }
 public E remove(int index) {
  throw new UnsupportedOperationException();
 }
 
 ......
 
 // Iterators
 public Iterator<E> iterator() {
  return new Itr();
 }
 
 private class Itr implements Iterator<E> {
  int cursor = 0;

  int lastRet = -1;
  int expectedModCount = modCount;

  public boolean hasNext() {
   return cursor != size();
  }

  public E next() {
   checkForComodification();
   try {
    E next = get(cursor);
    lastRet = cursor++;
    return next;
   } catch (IndexOutOfBoundsException e) {
    checkForComodification();
    throw new NoSuchElementException();
   }
  }

  public void remove() {
   if (lastRet == -1)
    throw new IllegalStateException();
   checkForComodification();

   try {
    AbstractList.this.remove(lastRet);
    if (lastRet < cursor)
     cursor--;
    lastRet = -1;
    expectedModCount = modCount;
   } catch (IndexOutOfBoundsException e) {
    throw new ConcurrentModificationException();
   }
  }

  final void checkForComodification() {
   if (modCount != expectedModCount)
    throw new ConcurrentModificationException();
  }
 }
 ......
}
*/

//====================================================================
 /**
  *    :
  *               "  /  "    。
  *                         ,
  *"  "            。
  *
  */
//     
abstract class Component{
 abstract void operation();
 
 void add(Component component){
  throw new UnsupportedOperationException();
 }
 void remove(Component component){
  throw new UnsupportedOperationException();
 }
 Component getChild(int i){
  throw new UnsupportedOperationException();
 }
 
}
//    
class Leaf extends Component{
 @Override
 void operation() {
        System.out.println("Leaf.operation()");  
 }
}
//    
class Composite extends Component{
    List<Component> comList;
    public Composite() {
      comList=new ArrayList<Component>();
    }
    
 @Override
 void add(Component component) {
       comList.add(component);
 }
 @Override
 void remove(Component component) {
      comList.remove(component);
 }
 @Override
 Component getChild(int i) {
  return comList.get(i);
 }
 
 @Override
 void operation() {
  System.out.println("Composite.operation()");
//         for(Component com:comList){
//          com.operation();
//         }  
  //       (     java.util )
  java.util.Iterator<Component>  iterator = comList.iterator();
  while(iterator.hasNext()){
   Component component=iterator.next();
   component.operation();
  }
 }
}
//  ,Leaf Composite  Component  ,       
//  Menu    MenuItem          
//=========================================================
/*
 *      :
 *   :                  (  ),            
 * 
 *    :                
 * 
 *    :               ,          
 * 
 *   :                  
 * 
 *   :                               
 * 
 *    :             ,            
 */

좋은 웹페이지 즐겨찾기