Appearance mode: Also known as facade mode: Appearance Facade provides a consistent interface for a set of interfaces of the subsystem, making this set of subsystems easy to use (reduces the complexity of the original system by introducing a new appearance role, while reducing the coupling between the client class and the subsystem).
Image source: Design Pattern: The basis of reusable object-oriented software.
accomplish
Case requirements: Rent a house
Students who have experienced finding a house and renting a house can realize that finding a house is very painful. They not only have to run from community to community, but also have to bargain with the landlord (second). So they learned to be smart and no longer talk to each other door-to-door, but directly find a real estate agent like Lianjia and I Love My Family. They have a certain amount of housing supply in their hands. We only need to pay them a commission, so they can bargain with the landlord on our behalf. Most of them are very professional, saving time and money. At this time, the real estate agent is a facade, and the tenant of the house is the subsystem SubSystem:
Facade
Appearance class: Know which subsystems are responsible for handling requests, and proxies the client's request to the appropriate subsystem object:
public class MediumFacade { private CuiYuanApartment cuiyuan; private XiXiApartment xixi; private XiHuApartment xihu; public MediumFacade() { cuiyuan = new CuiYuanApartment("Cuiyuan Community", 900, 1); xixi = new XiXiApartment("Xixi Garden", 1200, 1); xihu = new XiHuApartment("Xihu Community", 2600, 1); } public void rentingHouse(double price) { // The price is appropriate and there is a house to form if (price >= cuiyuan.getPrice() && cuiyuan.getStatus() != 0) { System.out.println("Subscribe" + cuiyuan.getLocation()); cuiyuan.setStatus(0); } else if (price >= xixi.getPrice() && xixi.getStatus() != 0) { System.out.println("Subscribe" + xixi.getLocation()); xixi.setStatus(0); } else if (price >= xihu.getPrice() && xihu.getStatus() != 0) { System.out.println("Reservation" + xihu.getLocation()); xihu.setStatus(0); } else { System.out.println("Bid too low/no listing..."); } }} SubSystem
Subsystem collection: Implement subsystem functions and handle tasks assigned by Facade objects (note that there is no Facade information in the subsystem, that is, there is no Facade object reference):
/** * @author jifang * @since 16/8/23 10:12 am. */public class XiHuApartment { private String location; private double price; private int status; public XiHuApartment(String location, double price, int status) { this.location = location; this.price = price; this.status = status; } public String getLocation() { return location; } public double getPrice() { return price; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; }}class XiXiApartment { private String location; private double price; private int status; public XiXiApartment(String location, double price, int status) { this.location = location; this.price = price; this.status = status; } public String getLocation() { return location; } public double getPrice() { return price; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; }}class CuiYuanApartment { private String location; private double price; private int status; public CuiYuanApartment(String location, double price, int status) { this.location = location; this.price = price; this.status = status; } public String getLocation() { return location; } public double getPrice() { return price; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; }} Client
In this way, Client simply contacts a real estate agent and gives us a quote, and they will help us contact all the landlords who meet:
public class Client { @Test public void client() { MediumFacade facade = new MediumFacade(); facade.rentingHouse(800); }}summary
Students who have experience in object-oriented development may have used him even if they have never heard of appearance patterns, because he perfectly embodies the idea of the principle of dependence inversion and the law of Dimit, which is one of the most commonly used patterns.
use
First of all, in the early stage of design, you should consciously separate hierarchical levels, such as a classic three-layer architecture, establishing a facade between layers, which can provide a simple interface for complex subsystems and greatly reduce the coupling degree.
Secondly, in the development stage, subsystems often become more and more complex due to continuous reconstruction. Adding Facade can provide a simple interface and reduce dependencies between modules.
Third, when maintaining a legacy system, the system may be very difficult to maintain and expand, but because it contains very important functions, new requirements must rely on it. At this time, a Facade can be developed for the new system, providing a relatively clear and simple interface for designing rough or high-complex legacy code, allowing the new system to interact with Facade, and Facade interacts with the legacy code for all the complicated work.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.