java設計モードの責任連鎖モード


責任連鎖パターンも行為型モードの一つであり、上の各オブジェクトは次のオブジェクトに対する参照を持っていて、チェーンを形成しています。最上階のオブジェクトは次のオブジェクトに対する参照を通じて、チェーンの中のいずれかのオブジェクトに要求を伝達してもいいです。
責任連鎖の用途を説明するために、ここでまず責任連鎖を使っていない例を書きます。
public interface GVnment {

	/**
	 *     
	 */
	public void HandleMess();
}
public class People {

	int things;
	
	public People(int things){
		this.things = things;
	}

	public int getThings() {
		return things;
	}

	public void setThings(int things) {
		this.things = things;
	}
	
}
public class TownGVment implements GVnment{

	@Override
	public void HandleMess(People p) {
		System.out.println("      :"+p.getThings());
		System.out.println("         ");
	}

}
public class CityGVment implements GVnment{

	@Override
	public void HandleMess(People p) {
		System.out.println("      :"+p.getThings());
		System.out.println("         ");
	}

}
上でデモンストレーションするのは、一人で役所に行って仕事をします。この人がやるべきことによって、郷鎮府でやるか、市役所でやるかを決めます。
public class test {
	public static void main(String[] args) {
		People weakP = new People(0);
		if(weakP.getThings()==0){
			GVnment tgVnment = new TownGVment();
			tgVnment.HandleMess(weakP);
		}else if(weakP.getThings()==1){
			GVnment cgVnment = new CityGVment();
			cgVnment.HandleMess(weakP);
		}
		People weakPS = new People(1);
		if(weakPS.getThings()==0){
			GVnment tgVnment = new TownGVment();
			tgVnment.HandleMess(weakPS);
		}else if(weakPS.getThings()==1){
			GVnment cgVnment = new CityGVment();
			cgVnment.HandleMess(weakPS);
		}
	}
}
ここでは、私達は転用者が使うのが面倒くさいと思います。一番かわいそうなのはこのpeopleです。自分のことはどこでできますか?試してみます。だから、責任連鎖に助けてもらいましょう。
責任連鎖のモードの中で私達はpeopleを手渡すだけでいいです。誰が実現するかは気にしないでください。
public abstract class GVnment {

	/**
	 *     
	 */
	private int wiCan;
	private GVnment nextGVnment;
	
	public GVnment(int canDo){
		wiCan = canDo;
	}
	/**
	 *     
	 */
	public void HandleMess(People p){
		if(p.getThings()==wiCan){
			Handle(p);
		}else{
			
			if(nextGVnment!=null){				
				nextGVnment.HandleMess(p);
			}else{
				System.out.println("      ");
			}
		}
	}
	
	public GVnment getNextGVnment() {
		return nextGVnment;
	}
	public void setNextGVnment(GVnment nextGVnment) {
		this.nextGVnment = nextGVnment;
	}
	protected abstract void Handle(People p);
}
public class TownGVment extends GVnment{

	public TownGVment() {
		super(0);
	}

	@Override
	protected void Handle(People p) {
		System.out.println("      :"+p.getThings());
		System.out.println("         ");
	}

}
public class CityGVment extends GVnment{

	public CityGVment() {
		super(1);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void Handle(People p) {
		System.out.println("      :"+p.getThings());
		System.out.println("         ");
	}

}
people類は変化していません。これからテスト種類の使い方を見ます。
public class test {
	public static void main(String[] args) {
		People p = new People(0);
		People happyP = new People(1);
		GVnment tGv = new TownGVment();
		GVnment cGv = new CityGVment();
		tGv.setNextGVnment(cGv);
		tGv.HandleMess(p);
		tGv.HandleMess(happyP);
	}
}
コールが簡単になりました。チェーンを作って使えば、誰が実現するか気にしなくてもいいです。この責任連鎖の欠点はチェーンが長すぎると論理が複雑になり、維持しにくいということです。