设计模式之策略模式

策略模式

分别封装行为接口,实现算法族,超类里放行为接口对象,在子类里具体设定行为对象。

原则就是:分离变化部分,封装接口,基于接口编程各种功能。
此模式让行为算法的变化独立于算法的使用者。

类结构图:

示例代码:

package com.flybehavior;

public interface FlyBehavior {
	void fly();
}

 

package com.flybehavior;

public class BadFlyBehavior implements FlyBehavior {

	@Override
	public void fly() {
		// TODO Auto-generated method stub
		System.out.println("--Bad Fly--");
	}

}

 

package com.flybehavior;

public class GoodFlyBehavior implements FlyBehavior {

	@Override
	public void fly() {
		// TODO Auto-generated method stub
		System.out.println("--Good FlyBehavior--");
	}

}

 

package com.flybehavior;

public class NoFlyBehavior implements FlyBehavior {

	@Override
	public void fly() {
		// TODO Auto-generated method stub
		System.out.println("--No FlyBehavior--");
	}

}

 

package com.quackbehavior;

public interface QuackBehavior {
	void quack();
}

 

package com.quackbehavior;

public class GaGaQuackBehavior implements QuackBehavior {

	@Override
	public void quack() {
		// TODO Auto-generated method stub
		System.out.println("--GaGa--");
	}
}

 

package com.quackbehavior;

public class GeGeQuackBehavior implements QuackBehavior {

	@Override
	public void quack() {
		// TODO Auto-generated method stub
		System.out.println("--Ge Ge--");
	}
}

 

package com.quackbehavior;

public class NoQuackBehavior implements QuackBehavior {

	@Override
	public void quack() {
		// TODO Auto-generated method stub
		System.out.println("--No Quack--");
	}
}

 

package com.duck;

import com.flybehavior.FlyBehavior;
import com.quackbehavior.QuackBehavior;

public abstract class Duck {
	FlyBehavior mFlyBehavior;
	QuackBehavior mQuackBehavior;
	
	public Duck() {
		
	}
	
	public void Fly() {
		mFlyBehavior.fly();
	}
	
	public void Quack() {
		mQuackBehavior.quack();
	}
	public abstract void display();

	public void setmFlyBehavior(FlyBehavior mFlyBehavior) {
		this.mFlyBehavior = mFlyBehavior;
	}

	public void setmQuackBehavior(QuackBehavior mQuackBehavior) {
		this.mQuackBehavior = mQuackBehavior;
	}
	
	public void swim() {
		System.out.println("--I'm swimming--");
	}
}

 

package com.duck;

import com.flybehavior.GoodFlyBehavior;
import com.quackbehavior.GaGaQuackBehavior;

public class GreenHeadDuck extends Duck {

	public GreenHeadDuck() {
		mFlyBehavior = new GoodFlyBehavior();
		mQuackBehavior = new GaGaQuackBehavior();
	}

	@Override
	public void display() {
		// TODO Auto-generated method stub
		System.out.println("--RedHeadDuck--");
	}

}

 

package com.duck;

import com.flybehavior.BadFlyBehavior;
import com.quackbehavior.GeGeQuackBehavior;

public class RedHeadDuck extends Duck {

	public RedHeadDuck() {
		mFlyBehavior = new BadFlyBehavior();
		mQuackBehavior = new GeGeQuackBehavior();
	}

	@Override
	public void display() {
		// TODO Auto-generated method stub
		System.out.println("--RedHeadDuck--");
	}

}

 

package com.duck;

import com.flybehavior.NoFlyBehavior;
import com.quackbehavior.NoQuackBehavior;

public class StimulateDuck {
	public static void main(String[] args) {
		Duck mGreenHeadDuck = new GreenHeadDuck();
		Duck mRedHeadDuck = new RedHeadDuck();
		
		System.out.println("******GreenHeadDuck******");
		mGreenHeadDuck.display();
		mGreenHeadDuck.Fly();
		mGreenHeadDuck.Quack();
		mGreenHeadDuck.swim();
		
		System.out.println("******RedHeadDuck******");
		mRedHeadDuck.display();
		mRedHeadDuck.Fly();
		mRedHeadDuck.Quack();
		mRedHeadDuck.swim();
		
		System.out.println("******after change******");
		mRedHeadDuck.setmFlyBehavior(new NoFlyBehavior());
		mRedHeadDuck.Fly();
		mRedHeadDuck.setmQuackBehavior(new NoQuackBehavior());
		mRedHeadDuck.Quack();
	}
}

 输出

******GreenHeadDuck******
--RedHeadDuck--
--Good FlyBehavior--
--GaGa--
--I'm swimming--
******RedHeadDuck******
--RedHeadDuck--
--Bad Fly--
--Ge Ge--
--I'm swimming--
******after change******
--No FlyBehavior--
--No Quack--

 

posted @ 2015-09-04 16:26  橙子123  阅读(175)  评论(0编辑  收藏  举报