Design Mode Notes - Face Mode
Facade Design Mode
Basic Introduction
The facade design mode is a structural mode. Generally speaking, it refers to the surface of a building, especially the most attractive one. It can represent a behavior and appearance that easily misleads one's true feelings or situations. When people pass through the surface of a building, they are attracted by the beautiful surface in front of them. However, they do not understand the complexity of the internal structure, which is the facade mode. While hiding the internal complexity, they also provide an interface for the client to easily access the system and handle it accordingly.
It's like going to the supermarket to buy something, ** A huge array of goods (complex systems) are placed in a large space, and we (clients) need to come to this supermarket to buy what we need to achieve our ideas, but in the large supermarket we find what we need (clients build their own systems), because we don't know much about these architectures. It will be a very difficult process, at this time we go back to the supermarket salesperson (front)** to ask, very familiar with the whole supermarket distribution of goods will quickly give the specific location of this product (help component system), which will save a lot of time, but also need to know the distribution of the whole supermarket in order to buy a product. This is an example of a facade pattern
- For Face Mode, he actually did the following
- It provides a unified interface to a set of interfaces for a subsystem and defines an advanced interface to help clients use the subsystem in a simpler way
- It solves the problem of how to represent a complex subsystem with a single interface object, which is actually a combination of the underlying subsystems
- Promotes decoupling with multiple clients
UML diagrams
There are three main participants in this model
- Facade: responsible for encapsulating a complex system while providing a comfortable interface to the outside
- System: A set of different subsystems, the whole system mixed together and difficult to observe
- The client interacts with the facade, which makes it easy to interact with the facade without worrying about the complexity of the system
- Facade
An interface that delegates a client's request to the corresponding subsystem until a request from the client is processed by which subsystem
- system
Implements the functionality of a subsystem, represented by a class. Ideally, a system should be represented by a set of classes responsible for different tasks
- Client
Instantiate the class of the facade, issue the corresponding request to the facade, and indirectly call the subsystem to complete the corresponding work
Example
For example, you are a boy and you have met a girl in your life who you want to protect for a lifetime. After several years of getting along with, you find that you have not married her in this lifetime, so you propose to marry. Both parents agree with you happily. Your girl is also very looking forward to the wedding. You want to give her a romantic and grand wedding. So you start to prepare for it. The wedding hotel, music, music, etc. Food, ceremonies, scene layout and so on make you busy and busy. In the process, because you don't know much about this, you're just a dead man who doesn't know romance, but you really want to prepare for it. As a saying goes, professional things have to be done by professional people. So, on the recommendation of friends, You've found a professional wedding planning company and asked them to help you solve these tough problems and get you the best price. A very romantic wedding has come to light under the guidance of a professional team. Your wife is very happy, your mother-in-law calls the kid right, and your parents are happy to think that the kid has grown up. Things are great, but when you prepare for them, it may not be ideal because you lack the expertise to build these systems and prepare for them, which may result in half the effort, but you have made a clear choice to leave things to professional people, prefect!
The following is from the perspective of the facade mode:
Client: You are responsible for pointing out your wedding needs
Face: You deliver to the professional team for the wedding, who are responsible for the implementation and negotiation of specific matters
Subsystem: catering, music, master, venue and other services
Python implementation
# -*- coding=utf-8 -*- class EventManager(object): """ Wedding Planning Company: Passed arrange How to plan the details of a wedding (i.e. build a subsystem) """ def __init__(self): print("Wedding Company:: Let me prepare this wedding~\n") def arrange(self): self.hotelier = Hotelier() self.hotelier.bookHotel() self.florist = Florist() self.florist.setFlowerRequirments() self.caterer = Caterer() self.caterer.setCuisine() self.musician = Musician() self.musician.setMusicType() def __del__(self): print("Wedding Company: All things are ready!") class Hotelier(object): """ Hotel Subsystem __isAvailable: Give if the hotel site is free bookHotel: Reserve Hotel venues """ def __init__(self): print("Arranging the Hotel for Marriage? --") def __isAvailable(self) -> bool: print("Is the Hotel free for the event on given day?") return True def bookHotel(self): if self.__isAvailable(): print("Registered the Booking\n\n") class Florist(object): """ Flower Store Subsystem setFlowerRequirments: Responsible for decorating and preparing wedding flowers """ def __init__(self): print("Flower Decorations for the Event? --") def setFlowerRequirments(self): print("Carnations, Roses and Lilies would be used for Decorations\n\n") class Caterer(object): """ Dining subsystem setCuisine: Responsible for preparing meals at the wedding site """ def __init__(self): print("Food Arrangements for the Event --") def setCuisine(self): print("Chinese & Continental Cusisine to be served\n\n") class Musician(object): """ Music Subsystem setMusicType: Responsible for the wedding scene BGM Get ready """ def __init__(self): print("Musical Arrangements for the Marriage --") def setMusicType(self): print("Jazz and Classical will be played\n\n") class You(object): """ Client: You askEventManager: You ask for the wedding company, and when the company receives it, it prepares for it, and you're in charge Next to the overall plan, what to drink or drink, and finally entertain """ def __init__(self): print("You:: Whoa! Marriage Arrangements??!!!") def askEventManager(self): print("You:: Let's Contact the Event Manager\n\n") em = EventManager() em.arrange() def __del__(self): print("You:: Thanks to you! Let we began! Phew!") if __name__ == "__main__": you = You() you.askEventManager() del you print("\nAn romantic wedding is begin...") print("The bridegroom and the bride of the wedding find the best partner of the world") print("Then there have an baby...") print("Story is not end...")
You:: Whoa! Marriage Arrangements??!!! You:: Let's Contact the Event Manager Wedding Company:: Let me prepare this wedding~ Arranging the Hotel for Marriage? -- Is the Hotel free for the event on given day? Registered the Booking Flower Decorations for the Event? -- Carnations, Roses and Lilies would be used for Decorations Food Arrangements for the Event -- Chinese & Continental Cusisine to be served Musical Arrangements for the Marriage -- Jazz and Classical will be played Wedding Company: All things are ready! You:: Thanks to you! Let we began! Phew! An romantic wedding is begin... The bridegroom and the bride of the wedding find the best partner of the world Then there have an baby... Story is not end...
C++ Implementation
#include<iostream> using namespace std; class Hotelier{ public: Hotelier(){ cout<<"Arranging the Hotel for Marriage? --"<<endl; } private: bool isAvailable(){ cout<<"Is the Hotel free for the event on given day?"<<endl; return true; } public: void bookHotel(){ if(isAvailable()) cout<<"Registered the Booking\n\n"<<endl; } }; class Florist{ public: Florist(){ cout<<"Flower Decorations for the Event? --"<<endl; } public: void setFlowerRequirments(){ cout<<"Carnations, Roses and Lilies would be used for Decorations\n\n"<<endl; } }; class Caterer{ public: Caterer(){ cout<<"Food Arrangements for the Event --"<<endl; } public: void setCuisine(){ cout<<"Chinese & Continental Cusisine to be served\n\n"<<endl; } }; class Musician{ public: Musician(){ cout<<"Musical Arrangements for the Marriage --"<<endl; } public: void setMusicType(){ cout<<"Jazz and Classical will be played\n\n"<<endl; } }; class EventManager{ public: EventManager(){ cout<<"Wedding Company: Let me prepare this wedding~"<<endl; } ~EventManager(){ cout<<"Wedding Company: All things are ready!"<<endl; } public: void arrange(){ Hotelier hotelier; hotelier.bookHotel(); Florist florist; florist.setFlowerRequirments(); Caterer caterer; caterer.setCuisine(); Musician musician; musician.setMusicType(); } }; class You{ public: You(){ cout<<"You: Whoa! Marriage Arrangements?!!!"<<endl; } public: void askEventManager(){ cout<<"You: Let's me Contact the Event Manager\n\n"<<endl; { EventManager em; em.arrange(); } cout<<"You: Thanks to you! Let we began! Phew!"<<endl; } }; int main(int argc, char *argv[]){ You you; you.askEventManager(); cout<<"\nAn romantic wedding is begin..."<<endl; cout<<"The bridegroom and the bride of the wedding find the best partner of the world"<<endl; cout<<"Then there have a baby..."<<endl; cout<<"Story is not end..."<<endl; }
You: Whoa! Marriage Arrangements?!!! You: Let's me Contact the Event Manager Wedding Company: Let me prepare this wedding~ Arranging the Hotel for Marriage? -- Is the Hotel free for the event on given day? Registered the Booking Flower Decorations for the Event? -- Carnations, Roses and Lilies would be used for Decorations Food Arrangements for the Event -- Chinese & Continental Cusisine to be served Musical Arrangements for the Marriage -- Jazz and Classical will be played Wedding Company: All things are ready! You: Thanks to you! Let we began! Phew! An romantic wedding is begin... The bridegroom and the bride of the wedding find the best partner of the world Then there have a baby... Story is not end...
The principle of minimum knowledge
Facade provides us with a unified system, making subsystems easier to use, decoupling the client from the subsystem, and its design principle is the principle of minimum knowledge
- When designing a system, for each object created, you should look at the number of classes that interact with it in an interactive manner
- By following this principle, you can avoid creating many closely coupled classes
- If there are a lot of dependencies between classes, the system will become difficult to maintain. If any part of the system is modified, it may cause other parts of the system to be unintentionally changed, which means that the system will degenerate and should be avoided resolutely