Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the appearance pattern of a design pattern

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use the appearance pattern of the design pattern". In the daily operation, I believe that many people have doubts about how to use the appearance pattern of the design pattern. The editor consulted all kinds of data and sorted out the simple and easy-to-use operation method. I hope it will be helpful to answer the doubt of "how to use the appearance pattern of the design pattern"! Next, please follow the editor to study!

1. Overview

Appearance pattern is a structural design pattern that provides a simple interface for libraries, frameworks, or other complex classes.

Avoid a variety of unrelated functions to pollute a single appearance, making it another complex structure. Additional skins can be used by clients and other skins.

2. Applicable scenarios

1) if you need a direct interface to a complex subsystem and the functionality of the interface is limited, you can use the appearance mode. The appearance will provide shortcuts to the most commonly used functions in the subsystem to meet most of the client's needs.

2) if you need to organize the subsystem into a multi-tier structure, you can use appearance. You can create an appearance for each layer, and then require the classes of each layer to interact through these appearances.

3. Examples

There are the following scenarios:

At present, there is a student subsystem, the system has three interfaces, query student name, query student age, query student home address.

There is a teaching system that calls these three interfaces separately.

There is a score system that calls three interfaces respectively.

There is an examination system, but also need to call these three systems.

3.1When the appearance mode is not used / * Student * / public class Student {private String name = "Wolf King"; private int age = 25; private String address = "Shanghai"; public Student (String name, int age, String address) {this.name = name; this.age = age; this.address = address } public Student () {} public String getName () {return name;} public void setName (String name) {this.name = name;} public int getAge () {return age;} public void setAge (int age) {this.age = age;} public String getAddress () {return address } public void setAddress (String address) {this.address = address;}} / * Student * / public class Student {private String name = "Wolf King"; private int age = 25; private String address = "Shanghai"; public Student (String name, int age, String address) {this.name = name; this.age = age; this.address = address } public Student () {} public String getName () {return name;} public void setName (String name) {this.name = name;} public int getAge () {return age;} public void setAge (int age) {this.age = age;} public String getAddress () {return address } public void setAddress (String address) {this.address = address;}} / * Age Interface * / @ Service public class StudentAgeService implements IStudentAge {@ Override public int getAge () {Student student = new Student (); return student.getAge ();} @ Service public class StudentNameService implements IStudentName {@ Override public String getName () {Student student = new Student () Return student.getName ();} three external services / * * Education Services * / @ Service public class EduService {@ Autowired private StudentNameService studentNameService; @ Autowired private StudentAgeService studentAgeService; @ Autowired private StudentAddressService studentAddressService; public void getStudentName () {System.out.println ("Student name is:" + studentNameService.getName ()) } public void getStudentAge () {System.out.println ("student age is:" + studentAgeService.getAge ());} public void getStudentAddress () {System.out.println ("student address is:" + studentAddressService.getAddress ());}} / * examination service * / @ Service public class ExamService {@ Autowired private StudentNameService studentNameService; @ Autowired private StudentAgeService studentAgeService @ Autowired private StudentAddressService studentAddressService; public void getStudentName () {System.out.println ("student name is:" + studentNameService.getName ());} public void getStudentAge () {System.out.println ("student age is:" + studentAgeService.getAge ());} public void getStudentAddress () {System.out.println ("student address is:" + studentAddressService.getAddress ()) }} / * Achievement Service * / @ Service public class ScoreService {@ Autowired private StudentNameService studentNameService; @ Autowired private StudentAgeService studentAgeService; @ Autowired private StudentAddressService studentAddressService; public void getStudentName () {System.out.println ("Student name is:" + studentNameService.getName ()) } public void getStudentAge () {System.out.println ("student age is:" + studentAgeService.getAge ());} public void getStudentAddress () {System.out.println ("student address is:" + studentAddressService.getAddress ());}} 3.2 use appearance mode

Add a facade service to the student service

/ * appearance mode service * / @ Service public class StudentFacedService {@ Autowired private StudentNameService studentNameService; @ Autowired private StudentAgeService studentAgeService; @ Autowired private StudentAddressService studentAddressService; public String getStudentName () {return studentNameService.getName ();} public int getStudentAge () {return studentAgeService.getAge ();} public String getStudentAddress () {return studentAddressService.getAddress () }} three calling services only need to introduce appearance service / * education service * / @ Service public class EduService {@ Autowired private StudentFacedService studentFacedService; public void getStudentName () {System.out.println ("student name is:" + studentFacedService.getStudentName ());} public void getStudentAge () {System.out.println ("student age is:" + studentFacedService.getStudentAge () } public void getStudentAddress () {System.out.println ("Student address is:" + studentFacedService.getStudentAddress ());}} / * examination Service * / @ Service public class ExamService {@ Autowired private StudentFacedService studentFacedService; public void getStudentName () {System.out.println ("Student name is:" + studentFacedService.getStudentName ()) } public void getStudentAge () {System.out.println ("student age is:" + studentFacedService.getStudentAge ());} public void getStudentAddress () {System.out.println ("student address is:" + studentFacedService.getStudentAddress ());}} / * * grades service * / @ Service public class ScoreService {@ Autowired private StudentFacedService studentFacedService Public void getStudentName () {System.out.println ("student name is:" + studentFacedService.getStudentName ());} public void getStudentAge () {System.out.println ("student age is:" + studentFacedService.getStudentAge ());} public void getStudentAddress () {System.out.println ("student address is:" + studentFacedService.getStudentAddress ()) }} 4. Analyze the above two methods. The code structure is as follows:

As you can see from the above two figures, for external services, the code complexity is greatly reduced and only one interface of the student service needs to be called.

5. Summarize the advantages:

Make the client code independent of the complex subsystem and reduce the dependence on the subsystem.

Disadvantages:

If the appearance is too large, it will become a God object, resulting in the coupling of all classes, through which all class functions can be operated.

At this point, the study on "how to use the appearance pattern of the design pattern" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report