需求:上个月我们公司对接了E签宝,在对接后我对设计模式进一步加深,考虑到保密协议,就不上传我写好的代码了,我重新整理了一份dome
上个月我们公司对接了E签宝,在对接后我对设计模式进一步加深,考虑到保密协议,就不上传我写好的代码了,我重新整理了一份dome。
场景模拟:假设我们现在要保存订单,OrderService接口定义要做什么,而NoSqlSaveOrderStragegy以及MySqlSaveOrderStrategy则提供了二种策略,分别是保存到nosql数据库,以及传统的mysql关系型数据库,最后在OrderServiceExecutor中通过构造函数注入最终要使用的策略。
OrderService接口:
public interface OrderService {
void saveOrder(String orderNo);
}
Mysql策略实现:
public class MySqlSaveOrderStrategy implements OrderService {
@Override
public void saveOrder(String orderNo) {
System.out.println("order:" + orderNo + " save to mysql");
}
}
Nosql策略实现:
public class NoSqlSaveOrderStrategy implements OrderService {
@Override
public void saveOrder(String orderNo) {
System.out.println("order:" + orderNo + " save to nosql");
}
}
使用策略的辅助"容器"
public class OrderServiceExecutor {
private final OrderService service;
public OrderServiceExecutor(OrderService service) {
this.service = service;
}
public void save(String orderNo) {
this.service.saveOrder(orderNo);
}
}
运行测试类:
public class OrderServiceTest {
public static void main(String[] args) {
OrderServiceExecutor executor1 = new OrderServiceExecutor(new MySqlSaveOrderStrategy());
executor1.save("001");
OrderServiceExecutor executor2 = new OrderServiceExecutor(new NoSqlSaveOrderStrategy());
executor2.save("002");
}
}
public static void main(String[] args) {
OrderServiceExecutor executor1 = new OrderServiceExecutor((String orderNo) -> System.out.println("order:" + orderNo + " save to mysql"));
executor1.save("001");
OrderServiceExecutor executor2 = new OrderServiceExecutor((String orderNo) -> System.out.println("order:" + orderNo + " save to nosql"));
executor2.save("002");
}
思路:核心思路是把一些通用的标准方法,在抽象父类里仅定义方法签名,实现逻辑交给子类。
场景模拟:比如:会员系统中,每个商家都会有一些营销活动,需要推送某种信息给会员,但是不同的商家推送的内容可能不同,有些需要推送优惠券,有些需要积分通知。
抽象模板类:
public abstract class AbstractPushTemplate {
public void push(int customerId, String shopName) {
System.out.println("准备推送...");
execute(customerId, shopName);
System.out.println("推送完成\n");
}
abstract protected void execute(int customerId, String shopName);
}
优惠券的具体模板:
public class PushCouponTemplate extends AbstractPushTemplate {
@Override
protected void execute(int customerId, String shopName) {
System.out.println("会员:" + customerId + ",你好," + shopName + "送您一张优惠券");
}
}
积分的具体模板:
public class PushScoreTemplate extends AbstractPushTemplate {
@Override
protected void execute(int customerId, String shopName) {
System.out.println("会员:" + customerId + ",你好," + shopName + "送您10个积分");
}
}
使用示例:
AbstractPushTemplate template1 = new PushCouponTemplate();
template1.push(1, "糖果店");
AbstractPushTemplate template2 = new PushScoreTemplate();
template2.push(1, "服装店");
显然如果模板的实现方式越多,子类就越多。使用java8重构后,可以把上面的3个模板(包括抽象类模板)减少到1个,参考下面:
public class PushTemplateLambda {
public void push(int customerId, String shopName, Consumer<Object[]> execute) {
System.out.println("准备推送...");
Object[] param = new Object[]{customerId, shopName};
execute.accept(param);
System.out.println("推送完成\n");
}
}
借助Consumer这个function interface,可以省去实现子类,具体的实现留到使用时再来决定,如:
new PushTemplateLambda().push(1, "糖果店", (Object[] obj) -> {
System.out.println("会员:" + obj[0] + ",你好," + obj[1] + "送您一张优惠券");
});
new PushTemplateLambda().push(1, "服装店", (Object[] obj) -> {
System.out.println("会员:" + obj[0] + ",你好," + obj[1] + "送您10个积分");
});
思路:基于某个Subject主题,然后一堆观察者Observer注册到主题上,有事件发生时,subject根据注册列表,去通知所有的observer。
Observer接口:
public interface Observer {
void notify(String orderNo);
}
Subject接口:
public interface Subject {
void registerObserver(Observer o);
void notifyAllObserver(String orderNo);
}
Subject接口实现:
public class SubjectImpl implements Subject {
private final List<Observer> list = new ArrayList<>();
@Override
public void registerObserver(Observer o) {
list.add(o);
}
@Override
public void notifyAllObserver(String orderNo) {
list.forEach(c -> c.notify(orderNo));
}
}
OrderObserver:
public class OrderObserver implements Observer {
@Override
public void notify(String orderNo) {
System.out.println("订单 " + orderNo + " 状态更新为【已支付】");
}
}
StockObserver:
public class StockObserver implements Observer {
@Override
public void notify(String orderNo) {
System.out.println("订单 " + orderNo + " 已通知库房发货!");
}
}
测试一把:
static void test1() {
Subject subject = new SubjectImpl();
subject.registerObserver(new OrderObserver());
subject.registerObserver(new StockObserver());
subject.notifyAllObserver("001");
}
public interface NewSubject {
List<Observer> list = new ArrayList<>();
default void registerObserver(Observer o) {
list.add(o);
}
default void nofityAllObserver(String orderNo) {
list.forEach(c -> c.notify(orderNo));
}
}
使用:
static void test2() {
NewSubject subject = new NewSubject() {
};
subject.registerObserver((String orderNo) -> System.out.println("订单 " + orderNo + " 状态更新为【已支付】"));
subject.registerObserver((String orderNo) -> System.out.println("订单 " + orderNo + " 已通知库房发货!"));
subject.nofityAllObserver("002");
}
学海无涯苦做舟,我在调用E签宝的时候用的就是模板方法
,不过相对这个简单的dome会更复杂一些,不过代码嘛就是积木一样,高大上的积木都是小小的方块搭建起来的。