T HE U LTIMATE D ETAIL (รายละเอียดขั้นสุดท้าย)

Main component คือ รายละเอียดขั้นสุดท้าย—policy (นโยบาย) ระดับต่ำที่สุดของระบบ มันคือ entry point แรกเริ่มของระบบ ไม่มีสิ่งใดต้องพึ่งพามันนอกจาก operating system หน้าที่ของมันคือสร้าง Factory, Strategy และ facility ระดับ global อื่น ๆ ทั้งหมด จากนั้นก็ส่งต่อการควบคุมให้กับส่วนที่เป็นนามธรรมระดับสูงของระบบ

การ inject dependencies เข้ามาควรเกิดขึ้นใน Main component นี้ โดยใช้ framework สำหรับ Dependency Injection และเมื่อ dependencies ถูก inject เข้าไปใน Main แล้ว Main ก็ควรกระจาย dependencies เหล่านั้นออกไปตามปกติโดยไม่ต้องใช้ framework อีก

ลองนึกถึง Main ว่าเป็น component ที่สกปรกที่สุดในบรรดา component ที่สกปรกทั้งหลาย

ลองพิจารณา Main component ต่อไปนี้จาก version ล่าสุดของเกม Hunt the Wumpus สังเกตว่ามันโหลด string ทั้งหมดที่เราไม่ต้องการให้ส่วนหลักของ code ต้องรู้จักเข้ามาไว้

public class Main implements HtwMessageReceiver {
  private static HuntTheWumpus game;
  private static int hitPoints = 10;
  private static final List<String> caverns = new   ArrayList<>();
  private static final String[] environments = new String[]{
    "bright",
    "humid",
    "dry",
    "creepy",
    "ugly",
    "foggy",
    "hot",
    "cold",
    "drafty",
    "dreadful"
  };
 
  private static final String[] shapes = new String[] {
    "round",
    "square",
    "oval",
    "irregular",
    "long",
    "craggy",
    "rough",
    "tall",
    "narrow"
  };
 
  private static final String[] cavernTypes = new String[] {
    "cavern",
    "room",
    "chamber",
    "catacomb",
    "crevasse",
    "cell",
    "tunnel",
    "passageway",
    "hall",
    "expanse"
  };
 
  private static final String[] adornments = new String[] {
   "smelling of sulfur",
    "with engravings on the walls",
    "with a bumpy floor",
    "",
    "littered with garbage",
    "spattered with guano",
    "with piles of Wumpus droppings",
    "with bones scattered around",
    "with a corpse on the floor",
    "that seems to vibrate",
    "that feels stuffy",
    "that fills you with dread"
  };

ทีนี้มาดู function main กันบ้าง สังเกตว่ามันใช้ HtwFactory ในการสร้างเกม โดยส่งชื่อ class คือ htw.game.HuntTheWumpusFacade เข้าไป เพราะ class นั้นสกปรกยิ่งกว่า Main เสียอีก วิธีนี้ช่วยป้องกันไม่ให้การเปลี่ยนแปลงใน class นั้นทำให้ Main ต้อง recompile/redeploy ใหม่

public static void main(String[] args) throws IOException {
   game = HtwFactory.makeGame("htw.game.HuntTheWumpusFacade",
                                 new Main());
   createMap();
   BufferedReader br =
     new BufferedReader(new InputStreamReader(System.in));
   game.makeRestCommand().execute();
   while (true) {
     System.out.println(game.getPlayerCavern());
     System.out.println("Health: " + hitPoints + " arrows: " +
                           game.getQuiver());
     HuntTheWumpus.Command c = game.makeRestCommand();
      System.out.println(">");
      String command = br.readLine();
      if (command.equalsIgnoreCase("e"))
        c = game.makeMoveCommand(EAST);
      else if (command.equalsIgnoreCase("w"))
        c = game.makeMoveCommand(WEST);
      else if (command.equalsIgnoreCase("n"))
        c = game.makeMoveCommand(NORTH);
      else if (command.equalsIgnoreCase("s"))
        c = game.makeMoveCommand(SOUTH);
      else if (command.equalsIgnoreCase("r"))
        c = game.makeRestCommand();
      else if (command.equalsIgnoreCase("sw"))
        c = game.makeShootCommand(WEST);
      else if (command.equalsIgnoreCase("se"))
        c = game.makeShootCommand(EAST);
      else if (command.equalsIgnoreCase("sn"))
        c = game.makeShootCommand(NORTH);
      else if (command.equalsIgnoreCase("ss"))
        c = game.makeShootCommand(SOUTH);
      else if (command.equalsIgnoreCase("q"))
        return;
 
      c.execute();
    }
  }

สังเกตอีกอย่างว่า main เป็นคนสร้าง input stream และเป็นเจ้าของ main loop ของเกม คอยตีความคำสั่ง input แบบง่าย ๆ แต่จากนั้นก็ส่งต่องานประมวลผลทั้งหมดไปให้ component อื่นที่อยู่ในระดับสูงกว่า

สุดท้ายนี้ สังเกตว่า main เป็นคนสร้าง map ของเกมขึ้นมา

private static void createMap() {
   int nCaverns = (int) (Math.random() * 30.0 + 10.0);
   while (nCaverns-- > 0)
     caverns.add(makeName());
 
    for (String cavern : caverns) {
      maybeConnectCavern(cavern, NORTH);
      maybeConnectCavern(cavern, SOUTH);
      maybeConnectCavern(cavern, EAST);
      maybeConnectCavern(cavern, WEST);
    }
 
    String playerCavern = anyCavern();
    game.setPlayerCavern(playerCavern);
    game.setWumpusCavern(anyOther(playerCavern));
    game.addBatCavern(anyOther(playerCavern));
    game.addBatCavern(anyOther(playerCavern));
    game.addBatCavern(anyOther(playerCavern));
 
    game.addPitCavern(anyOther(playerCavern));
    game.addPitCavern(anyOther(playerCavern));
    game.addPitCavern(anyOther(playerCavern));
 
    game.setQuiver(5);
  }
 
  // much code removed…
}

ประเด็นก็คือ Main เป็น module ระดับต่ำที่สกปรก ซึ่งอยู่ในวงแหวนนอกสุดของ clean architecture มันเตรียมทุกอย่างไว้ให้ระบบระดับสูง แล้วก็ส่งต่อการควบคุมให้กับระบบนั้น