Github source address

An overview of 23 design patterns

  • Java Language Design – An overview of 23 design patterns

Creation pattern

  • Factory Method Pattern
  • Abstract Factory Pattern
  • Builder Mode
  • Prototype mode
  • Singleton

Structural mode

  • Facade Pattern
  • Adapter mode (Adapter)
  • Proxy mode
  • Composite mode
  • Flyweight Mode
  • Decorator pattern
  • Bridge mode (Bridge)

Behavioral pattern

  • Mediator Mode
  • Observer Model
  • Command mode
  • Iterator pattern (Iterator)
  • Template Method
  • Strategy Pattern
  • State mode
  • Memento Mode
  • Interpreter mode
  • Chain of Responsibility model
  • Visitor Pattern

Definition 1.

It is required that communication between the outside and the inside of a subsystem must be carried out through a unified object. The facade pattern provides a high-level interface that makes subsystems easier to use.

2. Introduction

  • Appearance pattern is a structural pattern.
  • Appearance mode is also called appearance mode.
  • We often use appearance mode when wrapping our apis, but we may not know it. The appearance mode makes the whole system structure only have a unified high-level interface through a appearance class, which can reduce the user’s use cost.

3. Application scenarios

  • Provide a simple interface for a complex subsystem to hide the implementation of the subsystem and isolate changes.
  • Using facade patterns can separate a subsystem from the clients that use it and other subsystems, which increases subsystem independence and portability.
  • When building a hierarchical structure, a facade pattern can be used to define the interface for each level to interact with the outside world. This simplifies layer-to-layer dependencies by simply communicating with each other through appearances.

4. To achieve

When we play games, we usually have operations such as login and recharge, which are usually done by third-party SDKS. Game development generally requires only a simple access to use the login recharge function. Let’s implement a simple game SDK:

4.1 Creating Appearance Roles

Here as long as the package game SDK external interface, for the game to develop to call. There are only two interfaces: login and recharge.

    public class GameSdk {
        public void login(a) {// Login interface
            // Invoke the interface for the login subsystem
            LoginManager loginManager = new LoginManager();
            loginManager.login();
        }

        public void pay(int momey) {// The payment interface
            // Call the interface of the payment subsystem
            PayManager payManager = newPayManager(); payManager.pay(momey); }}Copy the code

4.2 subsystem

    // Log in to the system
    public class LoginManager {
        public void login(a) {
            System.out.println("Open login screen");
            System.out.println("Perform login operation");
            System.out.println("Login successful"); }}// Payment system
    public class PayManager {
        public void pay(int momey) {
            System.out.println("Generate order information");
            System.out.println("Choose payment method");
            System.out.println("Payment successful:" + momey + "Yuan"); }}Copy the code

4.3 Client Test:

     private void test(a) {
        // Here is the game development, through the call login() and pay() can be activated login and payment, do not care whether the payment using Alipay or weixin, etc., this is the game SDK to do.
        GameSdk gameSdk = new GameSdk();
        gameSdk.login();
        gameSdk.pay(6);
    }
Copy the code

Output result:

Open the login page and perform login operations. Login succeeded. Generate the order information.6yuanCopy the code

Advantages of 5.

  • The coupling degree between client and subsystem class is reduced, and the loose coupling relationship between subsystem and client is realized.
  • The facade class encapsulates the interface of the subsystem, making the system easier to use.
  • Increased flexibility, no matter how the subsystem changes, as long as it does not affect the facade object, can be modified freely.

6. Shortcomings

  • Adding a new subsystem may require modifying the source code of the appearance class, violating the “open close principle.”
  • The functionality of all subsystems is provided through an interface that can become complex.

7. Source code analysis in Android

Appearance patterns are also widely used in Android, such as the Context class, which encapsulates many methods, again using the startActivity() method. In fact, startActivity() is implemented using ActivityManagerService, which we’ve all heard of but don’t use in development. By wrapping the Context class, the Context class hides these details. We can start a new Activity with a simple callback. This is an example of appearance mode in An Android application. Of course, this kind of application is everywhere, we usually develop also often use to.