From the Java Design Pattern & quot; I 'd like to invite a secretary! & Quot; Facade Mode

Source: Internet
Author: User

When running A business in A bank, you will often encounter the following situations: first fill in A table A, then fill in A table B, and then fill in the Table C according to the content of table A and table B ...... All kinds of procedures were overwhelmed and I couldn't stand it anymore. Ask the bank: Do you have a one-stop service here ?! I really want to ask a secretary! Let him do all these messy jobs!

The preceding example is similar to the appearance mode. For example, we have many subsystems. in order to complete a task, the calling between them is very complicated. To reduce its complexity, we introduce a Facade, just like the secretary in the example above), let it achieve unified management of the subsystem, as shown in.

For example, in an RPG Game, we need to implement a file reading function to read the equipment and position of the task, determine the player's map through the position, and draw the map. Equipment is managed by the equipment system, and maps are managed by the map system. They are correlated. For example, a map system must know the location information of a person to construct a map, the equipment system must know the equipment of the task before constructing the equipment. Therefore, we have constructed a reading tool (that is, the appearance object), which encapsulates the steps of reading the file, and finally, we only need to call this reader to control the equipment system and map system. The Java code is as follows:

Class CharacterInfo {private float positionX; private float positionY; private int equipmentID; public CharacterInfo (float posX, float posY, int equipmentID) {setPositionX (POS); setPositionY (posY ); setEquipmentID (equipmentID);} public void setPositionX (float positionX) {this. positionX = positionX;} public float getPositionX () {return positionX;} public void setEquipmentID (int equipmentID) {this. equipmentID = equipmentID;} public int getEquipmentID () {return equipmentID;} public void setPositionY (float positionY) {this. positionY = positionY;} public float getPositionY () {return positionY;} class Map {private CharacterInfo characterInfo; public Map (CharacterInfo c) {characterInfo = c;} public String getMap () {if (characterInfo. getPositionX ()> 50 & characterInfo. getPositionX () <100 & characterInfo. getPositionY ()> 50 & characterInfo. getPositionY () <100) {return "suddenly";} else return "Unknown Region" ;}} class MapResource {private Map map; public MapResource (Map m) {map = m;} public void load () {System. out. printf ("reading % s map information \ n", map. getMap () ;}} class Equipment {private int equipmentID; public Equipment (int id) {equipmentID = id;} public void load () {if (equipmentID = 0) {System. out. println ("reading weapon-Dagger");} else {System. out. println ("reading other weapons") ;}} class CharacterLoader {static void load (CharacterInfo info) {Map map = new Map (info ); mapResource mapResource = new MapResource (map); mapResource. load (); Equipment equipment = new Equipment (info. getEquipmentID (); equipment. load () ;}} class Facade {public static void main (String [] args) {CharacterInfo character = new CharacterInfo (75, 65, 0); CharacterLoader. load (character );}}

CharacterLoader is an appearance class that integrates all the methods required for reading files. The appearance mode defines a high-level interface to make these subsystems easier to use. I think this mode is very understandable.

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.