策略模式

①定义规范

 

package org.fanzone.design.strategy;

 

/** 

 * Title: base<br> 

 * Description: Just a rule, which called interface.<br> 

 * Copyright: Copyright (c) 2011 <br> 

 * Create DateTime: Jun 22, 2011 7:53:11 PM <br> 

 * @author wangmeng

 */

public interface IRobort {

/**

* Be one's baby.

*/

void performTrust();

}


-------------------------------------------------------------------------------------------------------------------

②规范的不同实现
package org.mars.design.strategy;

/** 
 * Title: base<br> 
 * Description: Another implementation.<br> 
 * Copyright: Copyright (c) 2011 <br> 
 * Create DateTime: Jun 22, 2011 8:02:27 PM <br> 
 * @author wangmeng
 */
public class ProgramControlledRobort implements IRobort {

public void performTrust() {
System.out.println("According to the requirements of the numerical control in order and conditions, which in turn control of the robot mechanical action.");
}

}

package org.mars.design.strategy;

/** 
 * Title: base<br> 
 * Description: One implementation.<br> 
 * Copyright: Copyright (c) 2011 <br> 
 * Create DateTime: Jun 22, 2011 8:06:17 PM <br> 
 * @author wangmeng
 */
public class NumericalControlRobort implements IRobort {

public void performTrust() {
System.out.println("Don't make robots action, through the numerical, language on the robot, robot. Teaching according to the information and after teaching work.");
}

}

----------------------------------------------------------------------------------------

③构建策略
package org.mars.design.strategy;

/** 
 * Title: base<br> 
 * Description: The Executor of the Strategy.<br> 
 * Copyright: Copyright (c) 2011 <br> 
 * Create DateTime: Jun 22, 2011 8:13:21 PM <br> 
 * @author wangmeng
 */
public class StrategyExecutor {

private IRobort robort;
public StrategyExecutor(){}
/**
* Constructor's rebuilding.
* @param robort -- The implementation class of the specified interface.
*/
public StrategyExecutor(IRobort robort){
this.robort = robort;
}
/**
* The action of the executor.
*/
public void operate(){
robort.performTrust();
}
}

-------------------------------------------------------------

④根据不同的应用执行不同的策略
package org.mars.design.strategy;

/** 
 * Title: base<br> 
 * Description: Just for the testing.<br> 
 * Copyright: Copyright (c) 2011 <br> 
 * Create DateTime: Jun 22, 2011 8:10:57 PM <br> 
 * @author wangmeng
 */
public class Commissioning {
public static void main(String[] args){
/**To Build two executor of the strategy with the specified implementation class of the interface.*/
StrategyExecutor executorOne = new StrategyExecutor(new NumericalControlRobort());
StrategyExecutor executorTwo = new StrategyExecutor(new ProgramControlledRobort());
/**Just do the action.*/
executorOne.operate();
executorTwo.operate();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值