策略模式

①定义规范

 

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();
}
}

你可能感兴趣的:(C++,c,C#)