디자인 모델 의 복합 모델 과 총 결 편

19377 단어 디자인 모드
/**
 * 
 *     -->                  ,               
 *   -->       ,              
 *              
 *     :                       
 * 
 * OO  
 *     
 *          
 *            
 *                 
 *            
 *             
 *         
 *      -->        
 *             
 * 
 *     
 *    (5 ):    (            ,           )、    (                     
 * )、    (              )、    (         
 * ,          )、     (           ,        )
 * 
 *    (7 ):    (      ,        )、    (    ,           
 * )、    (                 )、
 *     (        )、     (    ,       )、    (            )、    (         )
 * 
 *    (11 ):    (          
 * ,           )、    (        ,          )、    (               
 * )、     (       ,           
 * )、    (       )、     (              )、     (          
 * )、     (        )、     (           )、     (         )、     (         )
 * 
 *    :                      
 * 
 *       :                                                                          
 *              
 * 
 * @author Administrator
 * 
 */

 위 는 바로 OO 디자인 원칙 과 23 중의 디자인 모델 이다. 예제 데모 복합 모드 코드
다음 디자인 모델 서적 인
먼저 관찰자 모드 의 테마 인터페이스 입 니 다.
/**
 * 
 */
package com.undergrowth.compound;



/**
 *            
 *     、  、     
 * 
 * @author Administrator
 *
 */
public interface QuackObservable {
	public void registerObserver(Observer observer);
	public void removeObserver(Observer observer);
	public void notifyObservers();
}

구체 적 인 주제
/**
 * 
 */
package com.undergrowth.compound;

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


/**
 *                            
 * @author Administrator
 *
 */
public class ObservableAssist implements QuackObservable {

	List<Observer> listObservers=new ArrayList<>();
	QuackObservable quackObservable;
	/**
	 * 
	 */
	public ObservableAssist(QuackObservable quackObservable) {
		// TODO Auto-generated constructor stub
		this.quackObservable=quackObservable;
	}

	/* (non-Javadoc)
	 * @see com.undergrowth.compound.QuackObservable#registerObserver(java.util.Observer)
	 */
	@Override
	public void registerObserver(Observer observer) {
		// TODO Auto-generated method stub
		listObservers.add(observer);
	}

	/* (non-Javadoc)
	 * @see com.undergrowth.compound.QuackObservable#removeObserver(java.util.Observer)
	 */
	@Override
	public void removeObserver(Observer observer) {
		// TODO Auto-generated method stub
		if (listObservers.contains(observer)) {
			listObservers.remove(observer);
		}
	}

	/* (non-Javadoc)
	 * @see com.undergrowth.compound.QuackObservable#notifyObservers()
	 */
	@Override
	public void notifyObservers() {
		// TODO Auto-generated method stub
		for (Observer observer : listObservers) {
			//     
			observer.update(quackObservable);
		}
	}

}

오리 의 인터페이스 
package com.undergrowth.compound;

/**
 * 
 *     -->                  ,               
 *   -->       ,              
 *              
 *     :                       
 * 
 * OO  
 *     
 *          
 *            
 *                 
 *            
 *             
 *         
 *      -->        
 *             
 * 
 *     
 *    (5 ):    (            ,           )、    (                     
 * )、    (              )、    (         
 * ,          )、     (           ,        )
 * 
 *    (7 ):    (      ,        )、    (    ,           
 * )、    (                 )、
 *     (        )、     (    ,       )、    (            )、    (         )
 * 
 *    (11 ):    (          
 * ,           )、    (        ,          )、    (               
 * )、     (       ,           
 * )、    (       )、     (              )、     (          
 * )、     (        )、     (           )、     (         )、     (         )
 * 
 *    :                      
 * 
 *       :                                                                          
 *              
 * 
 * @author Administrator
 * 
 */

public interface Quackable extends QuackObservable {
	public void quack();
}

구체 적 인 오리 실현 류
북경 오리 주제 의 추가, 제거, 관찰자 에 게 알 리 는 작업 을 주제 의 보조 류 에 맡 겨 처리 합 니 다.
package com.undergrowth.compound;

/**
 *    
 * @author Administrator
 *
 */
public class BeiJingDuck implements Quackable {

	//   、  、                  
	QuackObservable quackObservable;
	
	public BeiJingDuck() {
		this.quackObservable = new ObservableAssist(this);
	}

	@Override
	public void quack() {
		// TODO Auto-generated method stub
		System.out.println(BeiJingDuck.class.getName()+"\t"+"   ,quack");
		 notifyObservers();
	}

	@Override
	public void registerObserver(Observer observer) {
		// TODO Auto-generated method stub
		quackObservable.registerObserver(observer);
	}

	@Override
	public void removeObserver(Observer observer) {
		// TODO Auto-generated method stub
		quackObservable.registerObserver(observer);
	}

	@Override
	public void notifyObservers() {
		// TODO Auto-generated method stub
		quackObservable.notifyObservers();
	}

}

푸 른 머리 오리
package com.undergrowth.compound;

/**
 *    
 * 
 * @author Administrator
 * 
 */
public class MallardDuck implements Quackable {

	//    、  、                  
	QuackObservable quackObservable;

	public MallardDuck() {
		this.quackObservable = new ObservableAssist(this);
	}

	@Override
	public void quack() {
		// TODO Auto-generated method stub
		System.out.println(MallardDuck.class.getName() + "\t" + "   ,quack");
		 notifyObservers();
	}

	@Override
	public void registerObserver(Observer observer) {
		// TODO Auto-generated method stub
		quackObservable.registerObserver(observer);
	}

	@Override
	public void removeObserver(Observer observer) {
		// TODO Auto-generated method stub
		quackObservable.registerObserver(observer);
	}

	@Override
	public void notifyObservers() {
		// TODO Auto-generated method stub
		quackObservable.notifyObservers();
	}

}

붉 은 머리 오리
package com.undergrowth.compound;

/**
 *    
 * @author Administrator
 *
 */
public class RedheadDuck implements Quackable {

	//   、  、                  
		QuackObservable quackObservable;
		
	
	public RedheadDuck() {
		this.quackObservable = new ObservableAssist(this);
	}

	@Override
	public void quack() {
		// TODO Auto-generated method stub
		System.out.println(RedheadDuck.class.getName()+"\t"+"   ,quack");
	   notifyObservers();
	}

	@Override
	public void registerObserver(Observer observer) {
		// TODO Auto-generated method stub
		quackObservable.registerObserver(observer);
	}

	@Override
	public void removeObserver(Observer observer) {
		// TODO Auto-generated method stub
		quackObservable.removeObserver(observer);
	}

	@Override
	public void notifyObservers() {
		// TODO Auto-generated method stub
		quackObservable.notifyObservers();
	}

}

오리 떼 조합 모드
/**
 * 
 */
package com.undergrowth.compound;

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

/**
 *      
 *             
 *                ,        ,        
 *                           ,                   
 * @author Administrator
 *
 */
public class FlockDuck implements Quackable {

	List<Quackable> flockQuackables=new ArrayList<>();
	/**
	 * 
	 */
	public FlockDuck() {
		// TODO Auto-generated constructor stub
		
	}

	public void add(Quackable quackable){
		flockQuackables.add(quackable);
	}
	
	/* (non-Javadoc)
	 * @see com.undergrowth.compound.Quackable#quack()
	 */
	@Override
	public void quack() {
		// TODO Auto-generated method stub
		//          
		for (Iterator iterator = flockQuackables.iterator(); iterator.hasNext();) {
			Quackable quackable = (Quackable) iterator.next();
			quackable.quack();
			
		}
	}

	/**
	 *                       
	 * 
	 * 
	 * 
	 * */
	@Override
	public void registerObserver(Observer observer) {
		// TODO Auto-generated method stub
		for (Quackable quackable : flockQuackables) {
			quackable.registerObserver(observer);
		}
	}

	@Override
	public void removeObserver(Observer observer) {
		// TODO Auto-generated method stub
		for (Quackable quackable : flockQuackables) {
			quackable.removeObserver(observer);
		}
	}

	/**
	 *                 
	 */
	@Override
	public void notifyObservers() {
		// TODO Auto-generated method stub
		
	}

}

오리 의 장식 류 장식 모드
/**
 * 
 */
package com.undergrowth.compound;

/**
 *              
 * @author Administrator
 *
 */
public class QuackCount implements Quackable {

	private static int count=0;
	Quackable quackable;
	/**
	 * 
	 */
	public QuackCount() {
		// TODO Auto-generated constructor stub
	}

	public QuackCount(Quackable quackable) {
		// TODO Auto-generated constructor stub
		this.quackable=quackable;
	}

	/* (non-Javadoc)
	 * @see com.undergrowth.compound.Quackable#quack()
	 */
	@Override
	public void quack() {
		// TODO Auto-generated method stub
		quackable.quack();
		System.out.println(" "+(++count)+"  ");
	}

	public static int getCount() {
		return count;
	}

	@Override
	public void registerObserver(Observer observer) {
		// TODO Auto-generated method stub
		quackable.registerObserver(observer);
	}

	@Override
	public void removeObserver(Observer observer) {
		// TODO Auto-generated method stub
		quackable.removeObserver(observer);
	}

	@Override
	public void notifyObservers() {
		// TODO Auto-generated method stub
		quackable.notifyObservers();
	}
    
	
}

추상 적 인 공장 에서 오리 와 오리 떼 를 만들다.
package com.undergrowth.compound;

/**
 *          
 * @author Administrator
 *
 */
public abstract class AbstractDuckFactory {
	
	public abstract Quackable createMallardDuck();
	public abstract Quackable createRedheadDuck();
	public abstract Quackable createBeiJingDuck();
	
	public abstract Quackable createFlockDuck();
}

장식 오리
package com.undergrowth.compound;

/**
 *          
 * @author Administrator
 *
 */
public class CountingDuckFactory extends AbstractDuckFactory {

	@Override
	public Quackable createMallardDuck() {
		// TODO Auto-generated method stub
		return new QuackCount(new MallardDuck());
	}

	@Override
	public Quackable createRedheadDuck() {
		// TODO Auto-generated method stub
		return new QuackCount(new RedheadDuck());
	}

	@Override
	public Quackable createBeiJingDuck() {
		// TODO Auto-generated method stub
		return new QuackCount(new BeiJingDuck());
	}

	@Override
	public Quackable createFlockDuck() {
		// TODO Auto-generated method stub
		return null;
	}

}

오리 공장
package com.undergrowth.compound;

/**
 *               
 * @author Administrator
 *
 */
public class DuckFactory extends AbstractDuckFactory {

	@Override
	public Quackable createMallardDuck() {
		// TODO Auto-generated method stub
		return new MallardDuck();
	}

	@Override
	public Quackable createRedheadDuck() {
		// TODO Auto-generated method stub
		return new RedheadDuck();
	}

	@Override
	public Quackable createBeiJingDuck() {
		// TODO Auto-generated method stub
		return new BeiJingDuck();
	}

	@Override
	public Quackable createFlockDuck() {
		// TODO Auto-generated method stub
		return new FlockDuck();
	}

}

거위 울 음소 리 인터페이스
/**
 * 
 */
package com.undergrowth.compound;

/**
 *    
 * @author Administrator
 *
 */
public interface Creak {
	public void creak();
}

거위
package com.undergrowth.compound;
/**
 *  
 * @author Administrator
 *
 */
public class Goose implements Creak {

	@Override
	public void creak() {
		// TODO Auto-generated method stub
		System.out.println(Goose.class.getName()+"\t"+" ,   ");
	}

}

거위 어댑터
/**
 * 
 */
package com.undergrowth.compound;


/**
 *              
 * @author Administrator
 *
 */
public class GooseAdapter implements Quackable {

	Creak creak;
	public GooseAdapter(Creak creak) {
		// TODO Auto-generated constructor stub
		this.creak=creak;
	}

	/* (non-Javadoc)
	 * @see com.undergrowth.compound.Quackable#quack()
	 */
	@Override
	public void quack() {
		// TODO Auto-generated method stub
		creak.creak();
	}

	@Override
	public void registerObserver(Observer observer) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void removeObserver(Observer observer) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void notifyObservers() {
		// TODO Auto-generated method stub
		
	}

}

관찰자 모드 의 관찰자 인터페이스
/**
 * 
 */
package com.undergrowth.compound;

/**
 *    
 * @author Administrator
 *
 */
public interface Observer {
	public void update(QuackObservable quackObservable);
}

관찰자
/**
 * 
 */
package com.undergrowth.compound;

/**
 *              
 * @author Administrator
 *
 */
public class DuckDoctor implements Observer {

	/**
	 * 
	 */
	public DuckDoctor() {
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see com.undergrowth.compound.Observer#update(com.undergrowth.compound.QuackObservable)
	 */
	@Override
	public void update(QuackObservable quackObservable) {
		// TODO Auto-generated method stub
		System.out.println(DuckDoctor.class.getName()+"\t          :"+quackObservable);
	}

}

테스트 코드
package com.undergrowth.compound.test;

import static org.junit.Assert.*;

import org.junit.Test;

import com.undergrowth.compound.AbstractDuckFactory;
import com.undergrowth.compound.CountingDuckFactory;
import com.undergrowth.compound.Creak;
import com.undergrowth.compound.DuckDoctor;
import com.undergrowth.compound.DuckFactory;
import com.undergrowth.compound.FlockDuck;
import com.undergrowth.compound.Goose;
import com.undergrowth.compound.GooseAdapter;
import com.undergrowth.compound.Observer;
import com.undergrowth.compound.Quackable;

public class DuckTest {

	@Test
	public void test() {
		System.out.println("=====================      ====================");
		AbstractDuckFactory duckFactory = new DuckFactory();
		//    
		Quackable quackable = duckFactory.createMallardDuck();
		quack(quackable);
		//    
		quackable = duckFactory.createRedheadDuck();
		quack(quackable);
		//    
		quackable = duckFactory.createBeiJingDuck();
		quack(quackable);
		System.out.println("=====================      ====================");
		System.out.println("=====================     ====================");
		//        
		Creak creak = new Goose();
		quack(new GooseAdapter(creak));
		System.out.println("=====================     ====================");
		System.out.println("=====================     ====================");
		//             
		duckFactory = new CountingDuckFactory();
		//    
		quackable = duckFactory.createBeiJingDuck();
		quack(quackable);
		//    
		quackable = duckFactory.createMallardDuck();
		quack(quackable);
		//    
		quackable = duckFactory.createRedheadDuck();
		quack(quackable);
		System.out.println("=====================     ====================");
		System.out.println("=====================      ====================");
		duckFactory = new DuckFactory();
		//      
		FlockDuck flockQuackable=(FlockDuck) duckFactory.createFlockDuck();
 		//    
		quackable = duckFactory.createMallardDuck();
		flockQuackable.add(quackable);
		//    
		quackable = duckFactory.createRedheadDuck();
		flockQuackable.add(quackable);
		//    
		quackable = duckFactory.createBeiJingDuck();
		flockQuackable.add(quackable);
		flockQuackable.quack();
		System.out.println("=====================      ====================");
		System.out.println("=====================       ====================");
		//    
		quackable=duckFactory.createMallardDuck();
		//    
		Observer observer=new DuckDoctor();
		quackable.registerObserver(observer);
		quackable.quack();
		//    
		flockQuackable.registerObserver(observer);
		flockQuackable.quack();
		System.out.println("=====================       ====================");

	}

	private void quack(Quackable quackable) {
		// TODO Auto-generated method stub
		quackable.quack();
	}

}

콘 솔 출력
=====================      ====================
com.undergrowth.compound.MallardDuck	   ,quack
com.undergrowth.compound.RedheadDuck	   ,quack
com.undergrowth.compound.BeiJingDuck	   ,quack
=====================      ====================
=====================     ====================
com.undergrowth.compound.Goose	 ,   
=====================     ====================
=====================     ====================
com.undergrowth.compound.BeiJingDuck	   ,quack
 1  
com.undergrowth.compound.MallardDuck	   ,quack
 2  
com.undergrowth.compound.RedheadDuck	   ,quack
 3  
=====================     ====================
=====================      ====================
com.undergrowth.compound.MallardDuck	   ,quack
com.undergrowth.compound.RedheadDuck	   ,quack
com.undergrowth.compound.BeiJingDuck	   ,quack
=====================      ====================
=====================       ====================
com.undergrowth.compound.MallardDuck	   ,quack
com.undergrowth.compound.DuckDoctor	          :com.undergrowth.compound.MallardDuck@b524aa
com.undergrowth.compound.MallardDuck	   ,quack
com.undergrowth.compound.DuckDoctor	          :com.undergrowth.compound.MallardDuck@7a3a30
com.undergrowth.compound.RedheadDuck	   ,quack
com.undergrowth.compound.DuckDoctor	          :com.undergrowth.compound.RedheadDuck@825cf3
com.undergrowth.compound.BeiJingDuck	   ,quack
com.undergrowth.compound.DuckDoctor	          :com.undergrowth.compound.BeiJingDuck@a47cc3
=====================       ====================

  앞의 디자인 모델 의 코드 와 본 고의 모델 코드 는 여기 서 다운로드 합 니 다. 
http://download.csdn.net/detail/undergrowth/8306683

좋은 웹페이지 즐겨찾기