码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式之策略模式

时间:2015-09-04 17:10:45      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

策略模式

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

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

类结构图:

技术分享

示例代码:

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--

 

设计模式之策略模式

标签:

原文地址:http://www.cnblogs.com/chengzi123/p/4781903.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!