策略模式 ?
package com.thunisoft.t3.la.service.ds.impl;
public interface Strategy {
/**
* 计算应报的价格,即计算报价
* @param goodsPrice 商品销售原价
* @return 报价
*/
double calcPrice(double goodsPrice);
}
---------------------------------------------------------------------------------------------
package com.thunisoft.t3.la.service.ds.impl;
public class Price {
//虽然是一个接口,但实际是持有具体的策略实现类对象
private Strategy strategy;
/**
* 构造方法,传入一个具体的策略对象
* @param strategy 具体的策略对象
*/
public Price(Strategy strategy){
this.strategy = strategy;
}
/**
* 计算报价
* @param goodsPrice
* @return
*/
public double quote(double goodsPrice){
return this.strategy.calcPrice(goodsPrice);
}
}
---------------------------------------------------------------------------------------------
package com.thunisoft.t3.la.service.ds.impl;
//新客户或普通客户
public class NormalCustomerStrategy implements Strategy {
@Override
public double calcPrice(double goodsPrice) {
return goodsPrice;
}
}
package com.thunisoft.t3.la.service.ds.impl;
public class OldCustomerStrategy implements Strategy {
@Override
public double calcPrice(double goodsPrice) {
return goodsPrice*(1-0.05);
}
}
package com.thunisoft.t3.la.service.ds.impl;
public class BigCustomerStrategy implements Strategy {
@Override
public double calcPrice(double goodsPrice) {
return goodsPrice*(1-0.1);
}
}
---------------------------------------------------------------------------------------------
package com.thunisoft.t3.la.service.ds.impl;
public class Client {
public static void main(String[] args){
//1:选择并创建需要使用的策略对象
Strategy strategy = new BigCustomerStrategy();
//2:创建上下文
Price price = new Price(strategy);
//3:计算报价
double quote = price.quote(1000);
System.out.println("应该向客户报价:" + quote);
}
}
模板模式
package com.thunisoft.t3.la.service.ds.impl;
public abstract class Game {
//模板方法
public final void play(){
//初始化游戏
initialize();
//开始游戏
startPlay();
//结束游戏
endPlay();
}
//具体实现抽象出来交给子类实现
abstract void initialize();
abstract void startPlay();
abstract void endPlay();
}
------------------------------------------------------------------------------
package com.thunisoft.t3.la.service.ds.impl;
//足球
public class Football extends Game {
@Override
void initialize() {
System.out.println("Football Game Initialized! Start playing.");
}
@Override
void startPlay() {
System.out.println("Football Game Started. Enjoy the game!");
}
@Override
void endPlay() {
System.out.println("Football Game Finished!");
}
}
------------------------------------------------------------------------------
package com.thunisoft.t3.la.service.ds.impl;
//羽毛球
public class Badminton extends Game {
@Override
void initialize() {
System.out.println("Badminton Game Initialized! Start playing.");
}
@Override
void startPlay() {
System.out.println("Badminton Game Started. Enjoy the game!");
}
@Override
void endPlay() {
System.out.println("Badminton Game Finished!");
}
}
------------------------------------------------------------------------------
package com.thunisoft.t3.la.service.ds.impl;
public class Client {
public static void main(String[] args){
Football a = new Football();
a.play();
}
}
?
|