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 understand the structural pattern of design pattern

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to understand the structural pattern of design pattern". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn "how to understand the structural pattern of design pattern".

Cognitive structural model

Structural pattern describes how to combine classes and objects to form a larger structure. it describes two different things: classes and objects. according to this, it can be divided into class structural patterns and object structural patterns. The class structural pattern is concerned with the combination of classes, in which multiple classes can be combined into a larger system, in which there are generally only inheritance relations and implementation relations, while the object structural patterns are concerned with the combination of classes and objects. through the association relationship, the instance object of another class is defined, and then its method is called through that object. According to the principle of synthetic reuse, association relations are used instead of inheritance relations in the system as far as possible, so most structural patterns are object structural patterns.

An example of a structural pattern

Adapter pattern: convert the interface of one class into another interface that the customer wants, so that the existing interfaces can be reused. Adapters are mainly implemented in two ways: class adapter and object adapter. in general, it is recommended to give priority to object adapter.

Bridging pattern: separate the abstract part from the implementation part so that they can both change independently. It is mainly used to deal with the problem of multi-dimensional change points. through the combination of objects, it can greatly reduce the number of subclasses, and at the same time, it can also make different dimensions expand and change independently.

Combination mode: combine objects into a tree structure to represent the "integration-part" hierarchy, so that the user's use of a single object and a combined object is consistent, that is, the client can operate both transparently and indiscriminately.

Decoration mode: dynamically add some additional responsibilities to an object, and in terms of adding functionality, decoration mode is more flexible than generating subclasses. If you use multi-inheritance to add responsibilities, it will inevitably lead to an "explosive" growth in the number of subclasses, in addition, because it is static, it is impossible to dynamically add or remove additional responsibilities in the running state.

Appearance pattern: provides a consistent interface for a set of interfaces in the subsystem, and the appearance pattern defines a high-level interface that makes the subsystem easier to use. This used to require customers to directly deal with and interact with complex subsystems, but now this process will be completely completed by the appearance object, which greatly facilitates the call of the client.

Sharing meta-pattern: using sharing technology to effectively support a large number of fine-grained objects. The key of the sharing meta-mode is to separate the internal state of the object from the external state, to share the "stable" internal state as much as possible, and the state that will change with the application scene is passed in through the external state.

Proxy mode: provides a proxy for other objects to control access to this object. The main purpose is to add an indirect layer between the client and the target object, through this indirect layer to complete all kinds of control operations on the target object, so different functional types of agents are formed. such as remote agent, protection agent and virtual agent and so on.

Take the adapter pattern as an example, code parsing

When it comes to adapters, we are most familiar with the power adapter, that is, the charging head of the mobile phone. It is an application of the adapter pattern.

You can imagine that if you have a USB cable that connects a computer to a mobile phone, one end of the connected computer receives a voltage of 5V from the computer interface, and one end of the connected phone outputs 5V voltage to the phone, and they all work well.

Our common household voltage is 220V, so the USB data cable cannot be used to charge the phone directly. At this time, we have two solutions:

First, make a mobile phone charger separately, receiving 220V household voltage and outputting 5V voltage.

Second, add an adapter to convert the 220V home voltage into a 5V voltage similar to the computer interface, and then connect the data cable to charge the mobile phone.

If you have ever used an early mobile phone, you will know that the previous mobile phone manufacturers adopted the first solution: the early mobile phone chargers were made separately, and the charging head and the charging wire were connected together. but today's mobile phones use a power adapter and a data cable.

Now I'm going to talk about the adapter pattern, which is to convert the interface of one class into another interface that the customer wants, so that those classes that can't work together because of interface incompatibility can work together.

Adaptation means adaptation and matching. Generally speaking, the adapter pattern is suitable for related but incompatible structures, and the source interface can only be applied to the target interface through a middleware transformation. This transformation process is adaptation, which is called the adapter.

The household power supply is related to the USB data line: the output voltage of the household power supply and the input voltage of the USB data line. However, the two interfaces are not compatible because one output 220V and one input 5V can work together only after the output 220V is converted into output 5V by the adapter.

Next, I use a program to simulate this process:

Home power supply provides a voltage of 220V

HomeBattery class:

Class HomeBattery {int supply () {/ / Home power supply provides an output voltage of 220V return 220;}}

USB data line only receives a charging voltage of 5V

USBLine class:

Class USBLine {void charge (int volt) {/ / if the voltage is not 5V, throw an exception if (volt! = 5) throw new IllegalArgumentException ("only receive 5V voltage"); / / if the voltage is 5V, charge System.out.println normally ("charge normally");}}

First, let's take a look at how users charge their phones directly with home power before adaptation:

User class

Public class User {@ Test public void chargeForPhone () {HomeBattery homeBattery = new HomeBattery (); int homeVolt = homeBattery.supply (); System.out.println ("the voltage provided by the home power supply is" + homeVolt + "V"); USBLine usbLine = new USBLine (); usbLine.charge (homeVolt);}}

The running results are as follows:

The voltage provided by the home power supply is 220V

Java.lang.IllegalArgumentException: can only receive 5V voltage

At this point, if you add a power adapter:

Adapter class

Class Adapter {int convert (int homeVolt) {/ / adaptation process: using resistors, capacitors and other devices to reduce it to output 5V int chargeVolt = homeVolt-215; return chargeVolt;}}

The user then uses the adapter to convert the voltage provided by the home power supply into the charging voltage:

User class

Public class User {@ Test public void chargeForPhone () {HomeBattery homeBattery = new HomeBattery (); int homeVolt = homeBattery.supply (); System.out.println ("the voltage provided by the home power supply is" + homeVolt + "V"); Adapter adapter = new Adapter (); int chargeVolt = adapter.convert (homeVolt) System.out.println ("use adapter to convert home voltage to" + chargeVolt + "V"); USBLine usbLine = new USBLine (); usbLine.charge (chargeVolt);}}

The running results are as follows:

The voltage provided by the home power supply is 220V

Using an adapter to convert the home voltage to 5V

Normal charging

This is the adapter pattern. In our daily development, we often use a variety of Adapter, all of which belong to the application of adapter pattern.

However, multipurpose adapter patterns are not recommended. Because you are prepared in advance to fix it, if you can prevent the problems of different interfaces in advance, the mismatch will not occur, and you should consider using the adapter only if the source interface cannot be changed. For example, many modern power sockets have added special charging interfaces, so that we no longer need to use the adapter conversion interface, which is another progress of the society.

Thank you for your reading. the above is the content of "how to understand the structural pattern of design pattern". After the study of this article, I believe you have a deeper understanding of the problem of how to understand the structural pattern of design pattern. the specific use of the situation also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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