Appearance mode/facade mode (structural type)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.