策略设计模式:把可变的行为抽象出来形成一个算法簇

/**
策略设计模式:
			把可变的行为抽象出来,想成一个算法簇,这样可以让我们的应用更利于变化。
			在使用抽象类和接口时,我们优先选择接口
			OO思想:面向接口编程,低耦合,高内聚
*/

interface GoHomeable
{
	public void goHome(String job);
}

class GoHomeableImplByFly implements GoHomeable
{
	public void goHome(String job){
		System.out.println(job + "穷的就剩钱了,要坐飞机回家。。。");
	}
}

class GoHomeableImplByFoot implements GoHomeable
{
	public void goHome(String job){
		System.out.println(job + "把钱花光了,只能步行回家。。。");
	}
}

class GoHomeableImplByTrain implements GoHomeable
{
	public void goHome(String job){
		System.out.println(job + "是穷屌丝,站火车回家。。。");
	}
}

class Person
{
	private String job;
	private GoHomeable gh;

	public Person(String job){
		this.job = job;
	}

	public void setGH(GoHomeable gh){
		this.gh = gh;
	}

	public void setJob(String job){
		this.job = job;
	}

	public String getJob(){
		return job;
	}

	public void goHome(){
		gh.goHome(job);
	}
}

class Student extends Person
{
	public Student(String job){
		super(job);
	}
}

class Worker extends Person
{
	public Worker(String job){
		super(job);
	}
}

class Boos extends Person
{
	public Boos(String job){
		super(job);
	}
}

class  CeLueMoShi
{
	public static void main(String[] args) 
	{
		Student student = new Student("学生");
		student.setGH(new GoHomeableImplByTrain());
		student.goHome();

		Worker worker = new Worker("工人");
		worker.setGH(new GoHomeableImplByFoot());
		worker.goHome();

		Boos boos = new Boos("大老板");
		boos.setGH(new GoHomeableImplByFly());
		boos.goHome();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值