"Appearance Mode" of Design Patterns

What is Appearance Mode

Facade mode is a structural design mode that provides a unified interface for the subsystem to simplify its complex internal structure, making it easier for clients to access the functions of the subsystem.

Why Use Appearance Mode

When the internal structure of a system is relatively complex, or there is a strong coupling between multiple modules of the system, the use of appearance mode can hide the complexity of the system behind the subsystem, thereby reducing the complexity of the system and making the system Easier to maintain and expand.
Where is it used in work Take android as an example
In Android, the appearance mode can be widely applied to the interaction between various components, such as:

  1. The interaction between Activity and Fragment.
  2. Interaction between Fragment and Fragment.
  3. Interaction between Service and Activity or Fragment.
  4. Interaction between various Views.

Design ideas

When using the facade pattern, we need to define a facade class that will encapsulate the complexity of all subsystems and provide a simple interface to clients.
In this façade class, we can refer to the classes of all subsystems, and then encapsulate their methods, so that clients can access all subsystems through a simple call.

Code

In Android, we can use the following methods to implement the facade mode:
1. Define a facade class, for example: MyFacade:

public class MyFacade {
    
    

    private Activity mActivity;
    private Fragment mFragment;
    private Service mService;

    public MyFacade(Activity activity) {
    
    
        mActivity = activity;
    }

    public MyFacade(Fragment fragment) {
    
    
        mFragment = fragment;
    }

    public MyFacade(Service service) {
    
    
        mService = service;
    }

    public void doSomething() {
    
    
        if (mActivity != null) {
    
    
            // 执行Activity相关的操作
        }

        if (mFragment != null) {
    
    
            // 执行Fragment相关的操作
        }

        if (mService != null) {
    
    
            // 执行Service相关的操作
        }
    }
}

2. Where the subsystem needs to be used, use the MyFacade class to encapsulate the complexity of the subsystem:

public class MainActivity extends AppCompatActivity {
    
    

    private MyFacade mFacade;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mFacade = new MyFacade(this);

        // 使用MyFacade来调用子系统的方法
        mFacade.doSomething();
    }
}

Summarize

Facade pattern is a design pattern that can reduce the complexity of the system. It provides a simple interface to the client and hides the complexity of the subsystem behind the subsystem. In Android development, we can use appearance mode to simplify the interaction between various components, making the whole system easier to maintain and expand.

Guess you like

Origin blog.csdn.net/weixin_45112340/article/details/129893296