Question: The boss told the secretary to go to Shanghai on business for 10 days; The boss told the Secretary to invite 8 people to dinner. How to design?
Answer:
1. According to the thinking of the object of the face, there should be a boss class, and secretary Secretary class, in addition, there must be Hotel class hotel, airport airports, hotel class restaurant, driver class chauffeur.
2. Boss object is only to talk with the Secretary and not personally to book air tickets and reservations, etc., so the airport class, hotel class, hotel class and driver class four constitute a subsystem set, Secretary class is an appearance role class , the subsystem of four classes to reference, The boss class does not need to know the various classes within the subsystem, only through the secretary to arrange work.
Airport
package com.shusheng.facade;/**机场*/publicclass Airport { publicvoidbookTicket(String from,String to){ System.out.println("订购了明天xx点从"+from+"到"+to+"的飞机票"); }}
Driver class
package com.shusheng.facade;/**司机类*/publicclass Chauffeur { publicvoiddrive(String to){ System.out.println("司机开车去"+to); }}
Hotel
package com.shusheng.facade;/**宾馆*/publicclass Hotel { /**订住房*/ publicvoidreserve(int days){ System.out.println("订购了"+days+"天的房间"); }}
Hotel
package com.shusheng.facade;/**酒店*/publicclass Restaurant { publicvoidreserve(int num){ System.out.println("订了一桌"+num+"个人的酒席"); }}
Secretary
PackageCom.shusheng.facade;ImportJava.util.Date;/** Secretary * * Public Class secretary { PrivateAirport Airport =NewAirport ();PrivateHotel hotel =NewHotel ();PrivateRestaurant Restaurant =NewRestaurant ();PrivateChauffeur Chauffeur =NewChauffeur ();/** Arrange a business trip * * Public void Trip(String to,intDays) {Airport.bookticket ("Beijing", to);//Book your ticket (the secretary must know where he is, so do not pass in the departure point)Chauffeur.drive ("Airport");//drive to the airportHotel.reserve (days);//Hotel Reservation}/** Arrange Dinner * * Public void repast(intnum) {restaurant.reserve (num); Chauffeur.drive ("Hotel"); }}
Boss
package Com.shusheng.facade; /** boss class */ public class boss { Span class= "Hljs-javadoc" >/** mock boss talking to the Secretary */ public static void main (string[] args) {Secretary secretary = new secretary (); System.out.println ( "Boss told the secretary to go to Shanghai on business 10 Days" ); Secretary.trip ( "Shanghai" , 10 ); System.out.println ( "-----------------" ); System.out.println ( "Boss told the secretary to invite 8 people to dinner" ); Secretary.repast (8 ); }}
Appearance mode/facade mode (structural type)