In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "what factors need to be considered when using Adapter mode". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "what factors need to be considered when using Adapter mode" can help you solve the problem.
Effect.
There are different tradeoffs between class adapters and object adapters. Class adapter
Match Adaptee and Target with a concrete Adapter class. The result is that when we want to match a class and all its subclasses, the class Adapter will not be able to do the job.
This is the disadvantage of class adapters.
Enables Adapter to redefine part of the behavior of Adaptee because Adapter is a subclass of Adaptee.
As a derived class, you can simply modify the behavior of the base class and apply it directly to Adapter.
Only one object is introduced, and no additional pointers are needed to get the adaptee indirectly.
Takes up less space for a pointer variable than an object adapter, and there is no need to establish a separate relationship between Adapter and Adaptee.
Object adapter
Allow an Adapter to work with multiple Adaptee-, that is, Adaptee itself and all its subclasses, if any. Adapter can also add functionality to all Adaptee at once.
This is very convenient!
It makes it difficult to redefine the behavior of Adaptee. This requires generating a subclass of Adaptee and having Adapter reference this subclass instead of Adaptee itself.
The Adaptee object referenced by Adapter needs to be replaced.
In comparison, the object adapter uses one more pointer variable, and more procedural operations gain more flexibility. The author recommends the object adapter.
Other factors to consider when using the Adapter pattern are:
1) the matching degree of Adapter
The amount of work required to match the interface of Adaptee with that of Target may vary from Adapter to Adapter. The scope of work may be from simple interface transformations (such as changing operation names) to supporting completely different sets of operations. The workload of Adapter depends on the degree of similarity between the Target interface and the Adaptee interface.
The reason is simple: the more differences there are, the greater the workload of adaptation.
2) pluggable Adapter
When other classes use a class, the less assumptions required, the more reusable the class will be. If you build an interface match as a class, you don't need to assume that the same interface is visible to other classes. That is, interface matching allows us to add our own classes to some existing systems, which may have different interfaces to this class. Object-Work / Smalltalk [P a r 9 0] uses the word pluggable adapter to describe classes that have internal interface adaptations.
This passage is not easy to understand, so the author tries to explain it. When we design a class, there are generally some assumptions. For example, memory can be applied at will, there is no need for multitasking security and so on. The fewer such assumptions, the more scenarios the class can apply, that is, the more reusable it is. One of the many assumptions is the use of classes, that is, interfaces. Using adapters means that classes can be applied in any way.
Consider the TreeDisplay window component, which can graphically display the tree structure. If this is a special-purpose window component that is only used in an application, we may require that the objects it displays have a special interface, that is, they are all subclasses of the abstract class Tree. If we want to make TreeDisplay reusable (for example, we want it to be part of the toolkit of available window components), then this requirement will be unreasonable. The application will define the tree structure class itself, rather than necessarily using our abstract class Tree. Different tree structures have different interfaces.
Generally speaking, TreeDisplay components need to work with some kind of Tree class. If this TreeDisplay is only used in an application, then we can design the data that needs to be represented by TreeDisplay as a subclass of Tree. However, if you want TreeDisplay to be used in a variety of situations and applications, you should not have this requirement. Because for an existing class, this means a huge change in the class structure; for a newly designed class, it is too much to change the original inheritance relationship of the class for the sake of a representation function.
For example, in a directory hierarchy, subdirectories can be accessed through GetSubdirectories operations, while in an inherited hierarchy, the corresponding operation may be called GetSubclasses. Although the two hierarchies use different interfaces, a reusable TreeDisplay window component must be able to display both structures. In other words, TreeDisplay should have the function of interface adaptation.
From the point of view of getting the next element, GetSubdirectories and GetSubclasses do the same thing. The TreeDisplay of our design needs to meet the above two requirements. This belongs to the category of active adaptation.
3) A potential problem with using two-way adapters to provide transparent operations using adapters is that they are not transparent to all customers. The adapted object is no longer compatible with Adaptee's interface, so not all Adaptee objects can be used. Two-way adapters provide such transparency. Bidirectional adapters are especially useful when two different customers need to view the same object in different ways.
Generally speaking, the adapter is concerned with allowing the functionality of Adaptee to be used in the context of Adapter. In the eyes of Adaptee users, Adapter is either invisible (object adapter) or not necessarily useful (class adapter). When we need to use a function from both directions, we need a two-way adapter.
Consider a two-way adapter that integrates the graphics editing framework Unidraw [VL90] with the constraint solving toolkit QOCA [H H M V 9 2]. Both systems have classes that explicitly represent variables: Unidraw contains class StateVariable,QOCA that contains class ConstraintVariable, as shown in the following figure. In order to make Unidraw and QOCA work together, we must first match the class ConstraintVariable with the class StateVariable, and in order to transfer the result of the solution of QOCA to Unidraw, we must match StateVariable and ConstraintVariable.
The two-way adapter works in both directions.
This scheme includes a bidirectional adapter ConstraintStateVariable, which is a common subclass of class ConstraintVariable and class StateVariable, and ConstraintStateVariable makes the two interfaces match each other. In this case, multiple inheritance is a feasible solution because the interfaces of the adapted classes are quite different. The two-way adapter is compatible with both matched classes, and it works in both systems.
Class adapters are used in this example, and ConstraintStateVariable is both ConstraintVariable and StateVariable;. In fact, object adapters can also be used, so ConstraintVariable and StateVariable share a ConstraintStateVariable.
This is the end of the content about "what factors need to be considered when using Adapter mode". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.