Appearance mode: Provides a consistent interface for a set of interfaces in a subsystem that defines a high-level interface that makes this subsystem easier to use.
In the computer room charge system, the appearance mode is used to lift the coupling between the U layer and B layer, according to the previous practice, in the U layer function calls the method in layer B, you need to fully understand the layer B layer of the methods are what, their own u layer is needed to use which method, and then call the method in layer B. This way, the B-layer is completely exposed to the U-layer, and increase the U-layer and B-layer coupling degree, B-layer changes to take into account the U-layer call problem, not conducive to the security of the system. Add the appearance mode, put a group of methods in B layer into the Appearance class Fa?ade class, when the U layer needs to call the method of layer B, directly through the Fa?ade class, without having to fully understand the B layer of the methods are what their own use. This defines a high-level interface between the U-layer and the B-layer, providing a consistent interface for a set of methods in layer B, making the B-layer approach more secure and easier to use.
In layman's terms, the appearance pattern is the classification and nesting, the B-layer methods are categorized into the Fa?ade class, Fa?ade class themselves do not define the method, are nested in the B-layer method, to achieve the function of high-level interface. When you need to use the layer, you can go to the Fa?ade class to find all the method name, do not need to know who provides these methods, do not need to know how these methods are implemented, directly with the Fa?ade class inside the method name can achieve their own function, which greatly relieved the U layer and other layers of the coupling , so that you can concentrate on the U-layer to do what you should do, but also improve the performance of the system, these benefits are also the advantages of design patterns.
Examples in the book: Fry stocks and funds. There is no design pattern when the implementation process is this:
Class program { static void Main (string[] args) { Stock1 stock1 = new Stock1 (); Stock2 Stock2 = new Stock2 (); Stock1. Buy (); Stock2. Buy (); Stock1. Sell (); Stock2. Sell (); Console.read (); } } Class Stock1 {public void Sell () { Console.WriteLine ("Stock 1 sold"); } public void Buy () { Console.WriteLine ("Stock 1 buy"); } } Class Stock2 {public void Sell () { Console.WriteLine ("Stock 2 Sold"); } public void Buy () { Console.WriteLine ("Stock 2 buy"); } }
The result is this:
The code that follows the appearance pattern is this:
class program {static void Main (string[] args) {facade Facad E = new facade (); Façade. Buyfacade (); Façade. Sellfacade (); Console.read (); }} class Facade {Stock1 stock1; Stock2 Stock2; Public facade () {stock1 = new Stock1 (); Stock2 = new Stock2 (); } public void Buyfacade () {stock1. Buy (); Stock2. Buy (); } public void Sellfacade () {stock1. Sell (); Stock2. Sell (); }} class Stock1 {public void Sell () {Console.WriteLine ("Stock 1 sell"); } public void Buy () {Console.WriteLine ("Stock 1 buy"); }} class Stock2 {public void Sell () {Console.WriteLine ("Stock 2 sell"); } public void Buy () {Console.WriteLine ("Stock 2 buy"); } }
As a result, this example is very concise, so I am only used to explain the reason, if there is a wrong, please understand. Appearance class facade methods in the execution of the method according to their own needs, if the above example does not want to sell, you can put the Sellfacade () method to remove, the application of the method is flexible, their own sentiment is very important.
Design pattern-the understanding of the appearance pattern