1. 程式人生 > >門面模式-Facade Pattern(Java實現)

門面模式-Facade Pattern(Java實現)

RKE 代碼 調用 trac make pat sys ride 工作

門面模式-Facade Pattern

為一個復雜的模塊或子系統提供一個簡單的供外界訪問的接口

本文中代碼的例子如下: 一個礦場有很多礦工, 礦工的職責也都不一樣. 但一樣的是什麽呢? 一樣的就是每個礦工每天都在重復一樣的事情....起床, 上班, 工作, 下班, 睡覺......

要想管理這麽多礦工的這麽多事情可不簡單(好麻煩啊...挨個調用的時候開發者也有可能會忘掉某個人, 或者忘掉某一個方法).,

再舉個栗子, 比如操作數據庫也一樣, 要加載驅動, 建立連接, 操作數據庫, 關閉連接. 這麽多步驟忘掉一個都會是個問題.

MineWorker抽象類

這裏是礦場中工人的統一抽象定義.

public abstract class MineWorker {
    protected void goToSleep() {
        System.out.printf("%s 睡覺.\n", name());
    }

    protected void wakeUp() {
        System.out.printf("%s 早上醒過來.\n", name());
    }

    protected void goHome() {
        System.out.printf("%s 回家.\n", name());
    }

    protected void goToMine() {
        System.out.printf("%s 去礦場.\n", name());
    }

    protected abstract void work();

    protected abstract String name();

    public static enum Action {
        GO_TO_SLEEP, WAKE_UP, GO_HOME, GO_TO_MINE, WORK
    }

    public void action(Action... actions) {
        for (Action action : actions) {
            switch (action) {
                case GO_TO_SLEEP:
                    goToSleep();
                    break;
                case WAKE_UP:
                    wakeUp();
                    break;
                case GO_HOME:
                    goHome();
                    break;
                case GO_TO_MINE:
                    goToMine();
                    break;
                case WORK:
                    work();
                    break;
                default:
                    System.out.println("Undefined action");
                    break;
            }
        }
    }
}

CartOperator類

推車工人.

public class CartOperator extends MineWorker {
    @Override
    public void work() {
        System.out.printf("%s 運輸礦石 \n", name());
    }

    @Override
    public String name() {
        return "推車工人";
    }
}  

GoldDigger類

挖金礦工人

public class GoldDigger extends MineWorker {

    @Override
    public void work() {
        System.out.printf("%s 挖金礦 .\n", name());
    }

    @Override
    public String name() {
        return "挖礦金工人";
    }
}

TunnelDigger類

挖隧道工人.

public class TunnelDigger extends MineWorker {

    @Override
    public void work() {
        System.out.printf("%s 挖隧道.\n", name());
    }

    @Override
    public String name() {
        return "挖隧道工人";
    }
}

MineFacade類

本系統的門面, 用於對外提供一個簡單的接口.

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class MineFacade {

    private final List<MineWorker> mineWorkers;

    public MineFacade() {
        mineWorkers = new ArrayList<>();
        mineWorkers.add(new GoldDigger());
        mineWorkers.add(new CartOperator());
        mineWorkers.add(new TunnelDigger());
    }

    public void startNewDay() {
        makeActions(mineWorkers, MineWorker.Action.WAKE_UP, MineWorker.Action.GO_TO_MINE);
    }

    public void digOutGold() {
        makeActions(mineWorkers, MineWorker.Action.WORK);
    }

    public void endDay() {
        makeActions(mineWorkers, MineWorker.Action.GO_HOME, MineWorker.Action.GO_TO_SLEEP);
    }

    private static void makeActions(Collection<MineWorker> mineWorkers,
                                    MineWorker.Action... actions) {
        for (MineWorker mineWorker : mineWorkers) {
            mineWorker.action(actions);
        }
    }
}

Main

在這運行

public class Main {
    public static void main(String[] args) {
        // 實例化一個門面, 這是礦場對外提供的一個簡單的接口
        MineFacade mineFacade = new MineFacade();

        // 開始新的一天
        mineFacade.startNewDay();

        // 大家起來幹活
        mineFacade.digOutGold();

        // 一天結束啦
        mineFacade.endDay();
    }
}

運行結果如下:

技術分享圖片

門面模式-Facade Pattern(Java實現)