Appearance mode: the same statistics interface is provided for a set interface in the subsystem. The facade defines an interface that can make the subsystem better use and better use. Like spring, you can configure the relationship between classes to the configuration file, and the appearance mode is to put their relationship in a Facade class, reducing the coupling between classes.
Let's look at a simple example. The UML class diagram is as follows:
ShapeMaker uses a specific class, which can help you call operations of all classes. FacadeePatternDemo is used as a test class to test the calling function.
Shape. java
public interface Shape {
void draw();
}
Rectangle. java
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Rectangle::draw()");
}
}
Square. java
public class Square implements Shape {
@Override
public void draw() {
System.out.println("Square::draw()");
}
}
Circle. java
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("Circle::draw()");
}
}
Create Fa? Ade class, definition classShapeMaker. java
public class ShapeMaker {
private Shape circle;
private Shape rectangle;
private Shape square;
public ShapeMaker() {
circle = new Circle();
rectangle = new Rectangle();
square = new Square();
}
public void drawCircle(){
circle.draw();
}
public void drawRectangle(){
rectangle.draw();
}
public void drawSquare(){
square.draw();
}
}
Define test classFacadePatternDemo
public class FacadePatternDemo {
public static void main(String[] args) {
ShapeMaker shapeMaker = new ShapeMaker();
shapeMaker.drawCircle();
shapeMaker.drawRectangle();
shapeMaker.drawSquare();
}
}
Output result:
Circle::draw()
Rectangle::draw()
Square::draw()