public static void main(String[] args) { MediatorImpl mediator = new MediatorImpl(); System.out.println("-----------採購人員採購電腦-----------"); Purchase purchase = new Purchase(mediator); purchase.buyIBMComputer(100); System.out.println("-----------銷售人員銷售電腦-----------"); Sale sale = new Sale(mediator); sale.sellIBMComputer(1); System.out.println("-----------庫房管理人員清倉處理-----------"); Stock stock = new Stock(mediator); stock.clearStock(); // 用一個中介物件封裝一系列的物件交互作用,中介者使各物件不需要直接的互動,從而使其偶和鬆散,而且可以獨立的改變他們之間的交互作用 // Define an object that encapsulates how a set of objects interact. Mediator promotes loose // coupling by keeping objects from referring to each other explicitly. and it lets you vary // their interaction independently. }
private void sellComputer(int number) { if (stock.getStockNumber() < number) { purchase.buyIBMComputer(number); } stock.decrease(number); }