利用java8对设计模式的重构

ddatsh

dev #java

java8中提供的很多新特性可以用来重构传统设计模式中的写法

策略模式

策略模式类图

假设要保存订单,OrderService接口定义要做什么

NoSql/MySql SaveOrderStrategy提供了两种策略,最后在OrderServiceExecutor中通过构造函数注入最终要使用的策略

传统写法至少得4个类

public interface OrderService {

    void saveOrder(String orderNo);
}

public class MySqlSaveOrderStrategy implements OrderService {

    @Override
    public void saveOrder(String orderNo) {

        System.out.println("order:" + orderNo + " save to mysql");
    }
}

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

用 lambda重构后,可以省去2个策略实现类

public class OrderServiceTest {

    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("推送完成");
    }

    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, "服装店");  

显然如果模板的实现方式越多,子类就越多

lambda重构后,可以把上面的3个模板(包括抽象类模板)减少到1个

import java.util.function.*;

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("推送完成");
    }
}

借助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接口实现


import java.util.*;

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) {

        for (Observer observer : list) {
            observer.notify(orderNo);
        }
    }
}

观察者的两个实现

public class OrderObserver implements Observer {

    @Override
    public void notify(String orderNo) {

        System.out.println("订单 " + orderNo + " 状态更新为【已支付】");
    }
}
public class StockObserver implements Observer {

    @Override
    public void notify(String orderNo) {

        System.out.println("订单 " + orderNo + " 已通知库房发货!");
    }
}
 public static void main(String[] args) {

        Subject subject = new SubjectImpl();
        subject.registerObserver(new OrderObserver());
        subject.registerObserver(new StockObserver());
        subject.notifyAllObserver("001");
}

java8 接口可以提供默认实现方法,弄一个新的主题接口

import java.util.*;

public interface NewSubject {

    List<Observer> list = new ArrayList<>();

    default void registerObserver(Observer o) {
        list.add(o);
    }

    default void nofityAllObserver(String orderNo) {

        for (Observer observer : list) {
            observer.notify(orderNo);
        }
    }
}
 public static void main(String[] args) {

        NewSubject subject = new NewSubject() {

        };
        subject.registerObserver((String orderNo) -> System.out.println("订单 " + orderNo + " 状态更新为【已支付】"));
        subject.registerObserver((String orderNo) -> System.out.println("订单 " + orderNo + " 已通知库房发货!"));
        subject.nofityAllObserver("002");
    }

用2个接口实现了观察者模式

责任链/职责链模式

每个处理环节,都有一个“指针”指向下一个处理者,类似链表一样

Processor接口

public interface Processor {
 
    Processor getNextProcessor();
 
    void process(String param);
}

抽象实现类

public abstract class AbstractProcessor implements Processor {
 
    private Processor next;
 
    public AbstractProcessor(Processor processor) {
        this.next = processor;
    }
 
    @Override
    public Processor getNextProcessor() {
        return next;
    }
 
    @Override
    public abstract void process(String param);
}

2个具体的实现


public class ProcessorImpl1 extends AbstractProcessor {

    public ProcessorImpl1(Processor processor) {

        super(processor);
    }

    @Override
    public void process(String param) {

        System.out.println("processor 1 is processing:" + param);
        if (getNextProcessor() != null) {
            getNextProcessor().process(param);
        }
    }
}
public class ProcessorImpl2 extends AbstractProcessor {

    public ProcessorImpl2(Processor next) {

        super(next);
    }

    @Override
    public void process(String param) {

        System.out.println("processor 2 is processing:" + param);
        if (getNextProcessor() != null) {
            getNextProcessor().process(param);
        }
    }
}

java8重构后,只需要一个新接口

import java.util.function.*;

@FunctionalInterface
public interface NewProcessor {

    Consumer<String> process(String param);
}

同样的效果,可以写得很简洁:

Consumer<String> p1 = param -> System.out.println("processor 1 is processing:" + param);
Consumer<String> p2 = param -> System.out.println("processor 2 is processing:" + param);
p2.andThen(p1).accept("something happened");

andThen天然就是getNextProcessor的另一种表达。

重要提示

什么时候该用lambda,什么时候不用,这是要看情况的,如果处理逻辑相对比较简单,可以用lamdba来重构,以便让代码更简洁易读,如果处理逻辑很复杂,应该还是用“类”