1. 程式人生 > >利用java8對設計模式的重構

利用java8對設計模式的重構

java8中提供的很多新特性可以用來重構傳統設計模式中的寫法,下面是一些示例:

一、策略模式

上圖是策略模式的類圖,假設我們現在要儲存訂單,OrderService介面定義要做什麼,而NoSqlSaveOrderStragegy以及MySqlSaveOrderStrategy則提供了二種策略,分別是儲存到nosql資料庫,以及傳統的mysql關係型資料庫,最後在OrderServiceExecutor中通過建構函式注入最終要使用的策略。

傳統寫法,這個場景至少得4個類,程式碼如下:

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

重構後,可以省去2個策略實現類,程式碼如下:

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

用java8重構後,介面可以提供預設實現方法,我們弄一個新的主題介面

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

只用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);
        }
    }
}

使用示例:

    static void test1() {
        Processor p1 = new ProcessorImpl1(null);
        Processor p2 = new ProcessorImpl2(p1);
        p2.process("something happened");
    }

用java8重構後,只需要一個新介面

@FunctionalInterface
public interface NewProcessor {
    Consumer<String> process(String param);
}

同樣的效果,可以寫得很簡潔:

    static void test2() {
        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來重構,以便讓程式碼更簡潔易讀,如果處理邏輯很複雜,應該還是用“類”。