package com.strategy;
public interface SendStrategy {
public boolean send(Message msg);
}
package com.strategy;
public class OnceADay implements SendStrategy{
@Override
public boolean send(Message msg) {
System.out.println("每天一次");
return false;
}
}
package com.strategy;
public class OnceAweek implements SendStrategy{
@Override
public boolean send(Message msg) {
System.out.println("每周一次");
return false;
}
}
package com.strategy;
public class SmsSender {
private SendStrategy sendStrategy;
public SmsSender(SendStrategy sendStrategy){
super();
this.sendStrategy = sendStrategy;
}
/**
* 发送信息
* @param message
*/
public void sendSMS(Message message){
sendStrategy.send(message);
}
}
package com.strategy;
public class Message {
}
package com.strategy;
/**
* 客户端 调用
* @author xinjiatao
*
*/
public class Clinet {
public static void main(String[] args) {
new SmsSender(new OnceADay()).sendSMS(new Message());
new SmsSender(new OnceAweek()).sendSMS(new Message());
new SmsSender(new SendStrategy() {
public boolean send(Message msg) {
System.out.println("一天两次");
return true;
}
}).sendSMS(new Message());
}
}