This is the 23rd day of my participation in Gwen Challenge

The appearance model

Appearance pattern can also be called facade pattern. In the development SDK, there will be a lot of use of appearance mode, through a appearance class to make the whole system interface only one unified high-level interface, external shielding implementation details, reduce the call cost.

Facade pattern definition: a subsystem communicates externally and internally through a series of objects.

Appearance Mode role:

  1. Facade Unified external interface of the system
  2. System1, System2, System3 subsystem interfaces

In actual combat

MobilePhone corresponds to the Facade; Phone and Camera correspond to System1 and System2. MobilePhone is a cosmetic mode that integrates various MobilePhone functions: phone, camera, SMS and so on. All of these functions are integrated into the phone, so that users are faced with the phone as an object rather than a single module. When using the camera or on the phone to open the camera function, the phone call is also on the phone to open the phone function, instead of making a phone call must be a phone, taking pictures must be a camera. Similarly, MobilePhone is the main system, PhoneImpl and CameraImpl are subsystems, which encapsulate MobilePhone to provide unified operation interface for users. For users, they do not know that the camera subsystem functions are called when taking photos, and the phone system is called when making calls. Or it could be both.


public interface Phone{

    public void hangUp(a);
}

public PhoneImpl implements Phone{
    @override
    public void hangUp(a){}}public interface Camera{

    public void takePhoto(a);
    public void closeCamera(a);
}

public CameraImpl implements Camera{
    @override
    public void takePhoto(a){}@override
    public void closeCamera(a){}}public class MobilePhone{
    private Phone phone = new PhoneImpl();
    private Camera camera = new CameraImpl();
    
    public void hangUp(a){
        phone.hangUp();
    }
    
    public void takePhoto(a){
        camera.open();
        camera.takePhoto();
    }
    
    public void closeCamera(a){ camera.close(); }}Copy the code

conclusion

Appearance patterns are relatively frequently used design patterns, primarily for encapsulation. Through an external structure to provide a set of unified API entry to the external, so that users can operate the entire SDK through a type, reduce the user’s starting cost, provide system flexibility. The advantage of appearance mode is that the subsystem details are invisible to the user, which reduces the user’s coupling to the subsystem and encapsulates the interface for easy use. The disadvantage of the appearance mode is interface expansion. If the subsystem is fully functional, the more required functions it provides, the more the number of interfaces will be, and at the same time, the cost will be raised.