In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the relevant knowledge of "how to implement the adapter pattern for Java design pattern parsing". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
First, what is the adapter pattern:
The adapter pattern is mainly used to transform the interface of a class into the target class format desired by the client, so that the previously incompatible classes can work together to decouple the target class from the adaptor class; at the same time, it also conforms to the "open and closed principle". You can add a new adapter class without modifying the original code Encapsulating the specific implementation in the adaptor class is transparent to the client class and improves the reusability of the adaptor, but the disadvantage is that the implementation process of replacing the adapter is more complex.
Therefore, the adapter pattern is more suitable for the following scenarios:
(1) the system needs to use existing classes, and the interfaces of these classes do not conform to the interface of the system.
(2) use third-party components, component interface definition is different from their own definition, do not want to modify their own interface, but to use the functions of third-party component interfaces.
Here are two very vivid examples of what an adapter pattern is:
Second, three ways to realize the adapter pattern:
Adapter patterns are mainly divided into three categories: class adapter patterns, object adapter patterns, and interface adapter patterns.
1. The adapter pattern of the class:
Target interface (Target): the interface expected by the customer. The target can be a concrete or abstract class or an interface.
Classes that need to be adapted (Adaptee): classes or adapters that need to be adapted.
Adapter: converts the original interface to the target interface by wrapping an object that needs to be adapted.
/ / existing class class Adaptee {public void specificRequest () {System.out.println ("adapted class has special function...") that does not conform to our existing standard interface;}} / / target interface, or standard interface interface Target {public void request () } / / specific target class, which only provides common functions class ConcreteTarget implements Target {public void request () {System.out.println ("ordinary classes have common functions...");}} / / adapter class, inherits the adapted class and implements the standard interface class Adapter extends Adaptee implements Target {public void request () {super.specificRequest () }} / / Test class public class Client {public static void main (String [] args) {/ / use normal function class Target concreteTarget = new ConcreteTarget (); concreteTarget.request (); / / use special function class, that is, adaptation class Target adapter = new Adapter () Adapter.request ();}}
Running result:
Ordinary classes have general functions. The adapted class has a special function.
2. Adapter mode of the object:
/ / adapter class, which is directly associated with the adapted class, while implementing the standard interface class Adapter implements Target {/ / directly associating the adapted class private Adaptee adaptee; / / you can pass in the adapted class object public Adapter (Adaptee adaptee) {this.adaptee = adaptee through the constructor. } public void request () {/ / here is to complete the special function this.adaptee.specificRequest () by delegating;}} / / the test class public class Client {public static void main (String [] args) {/ / use the normal function class Target concreteTarget = new ConcreteTarget () ConcreteTarget.request (); / / use a special function class, that is, an adaptation class. / / you need to create an object of the adaptation class as the parameter Target adapter = new Adapter (new Adaptee ()); adapter.request ();}}
The test results are consistent with the above. We also know from the class diagram that only the internal structure of the Adapter class needs to be modified, that is, Adapter itself must first have an object to be adapted to the class, and then delegate specific special functions to this object to implement. Using the object adapter pattern, we can make the Adapter class (adaptation class) adapt to multiple different adapted classes according to the incoming Adaptee objects. of course, we can extract an interface or abstract class for multiple adapted classes. In this way, it seems that the object adapter pattern is a little more flexible.
3. Adapter mode of the interface:
Sometimes we write an interface with multiple abstract methods, and when we write the implementation class of the interface, we have to implement all the methods of the interface, which is obviously sometimes wasteful, because not all the methods are what we need. sometimes only some are needed. in order to solve this problem, we introduce the adapter pattern of the interface, with the help of an abstract class, which implements the interface. All the methods are implemented, and we don't deal with the original interface, we only get in touch with the abstract class, so we write a class, inherit the abstract class, and rewrite the methods we need. Take a look at the class diagram:
This is easy to understand, and in actual development, we often encounter so many methods defined in this interface that sometimes we don't need all of them in some implementation classes. Look at the code:
Public interface Sourceable {public void method1 (); public void method2 ();}
Abstract class Wrapper2:
Public abstract class Wrapper2 implements Sourceable {public void method1 () {} public void method2 () {}} public class SourceSub1 extends Wrapper2 {public void method1 () {System.out.println ("the sourceable interface's first Sub1!");}} public class SourceSub2 extends Wrapper2 {public void method1 () {System.out.println ("the sourceable interface's second Sub2!") }} public class WrapperTest {public static void main (String [] args) {Sourceable source1 = new SourceSub1 (); Sourceable source2 = new SourceSub2 (); source1.method1 (); source1.method2 (); source2.method1 (); source2.method2 ();}}
Running result:
This is the end of the sourceable interface's first Sub1 implementing the sourceable interface's second Sub2! "how to implement adapter patterns for Java design pattern resolution". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.