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

Sample Analysis of Java Adapter pattern

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

Share

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

This article focuses on "sample Analysis of Java Adapter patterns". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "Java Adapter pattern sample Analysis".

Define

The adapter pattern, that is, the interface of one class is converted into the representation of another interface expected by the client, the main purpose is to achieve compatibility, so that the two classes that cannot work together because of the interface mismatch can work together.

classification

Class adapter

Object adapter

Interface adapter

Case requirement

Charge the mobile phone and adapt the 220V voltage to 5V through the mobile phone charger

Solution 1: class adapter

Define 220V AC (the role of the adaptor)

/ * 220V AC (role of the adaptor) * @ author:liyajie * @ createTime:2022/2/17 21:41 * @ version:1.0 * / public class Ac {public int outputAc () {int srcV = 220; System.out.println (srcV + "V AC"); return srcV;}}

Define 5v DC (the role of the target object)

/ * 5v DC (role of the target object) * @ author:liyajie * @ createTime:2022/2/17 21:44 * @ version:1.0 * / public interface Dc {int outputDc ();}

Define the adapter

/ * * Mobile phone charging adapter * @ author:liyajie * @ createTime:2022/2/17 21:45 * @ version:1.0 * / public class PhoneAdapter extends Ac implements Dc {@ Override public int outputDc () {/ / get 220V AC int srcV = outputAc (); / / simulate the adapter process and convert it to 5V DC int targetV = srcV / 44 System.out.println ("Voltage has been adapted to" + targetV + "V"); return targetV;}}

Define mobile phone class

/ * * Mobile phones * @ author:liyajie * @ createTime:2022/2/17 21:48 * @ version:1.0 * / public class Phone {public void charge (Dc dc) {if (dc.outputDc () = = 5) {System.out.println ("normal voltage, safe to charge");} else {System.out.println ("abnormal voltage, dangerous!") ;}

Define test classes

/ * author:liyajie * @ createTime:2022/2/18 10:56 * @ version:1.0 * / public class Test {public static void main (String [] args) {new Phone () .test (new PhoneAdapter ());}}

View test results

Solution 2: object adapter

This solution only needs to modify the mobile phone adapter class, as follows:

/ * Mobile charging adapter * @ author:liyajie * @ createTime:2022/2/17 21:45 * @ version:1.0 * / public class PhoneAdapter implements Dc {private Ac ac; public PhoneAdapter (Ac ac) {this.ac = ac;} @ Override public int outputDc () {/ / get 220V AC int srcV = ac.outputAc () / / simulate the adapter process, convert to 5v DC int targetV = srcV / 44; System.out.println ("voltage has been adapted to" + targetV + "V"); return targetV;}}

Transform the test class

/ * * Test class * @ author:liyajie * @ createTime:2022/2/18 11:12 * @ version:1.0 * / public class Test {public static void main (String [] args) {new Phone () .charge (new PhoneAdapter (new Ac ();}}

View test results

Solution 3: interface adapter

What needs to be modified is as follows: define a default adapter to implement multiple methods of Dc so that other custom adapters can be used to inherit and extend

/ * * default adapter * @ author:liyajie * @ createTime:2022/2/17 21:45 * @ version:1.0 * / public class DefaultAdapter implements Dc {@ Override public int outputDc () {return 0;}}

Define mobile phone adapter

/ * Mobile charging adapter * @ author:liyajie * @ createTime:2022/2/17 21:45 * @ version:1.0 * / public class PhoneAdapter extends DefaultAdapter {private Ac ac; public PhoneAdapter (Ac ac) {this.ac = ac;} @ Override public int outputDc () {/ / get 220V AC int srcV = ac.outputAc () / / simulate the adapter process, convert to 5v DC int targetV = srcV / 44; System.out.println ("voltage has been adapted to" + targetV + "V"); return targetV;}}

Define mobile phone class

/ * * Mobile phones * @ author:liyajie * @ createTime:2022/2/17 21:48 * @ version:1.0 * / public class Phone {public void charge (int v) {if (v = = 5) {System.out.println ("normal voltage, safe to charge");} else {System.out.println ("abnormal voltage, dangerous!") ;}

Define test classes

/ * * Test class * @ author:liyajie * @ createTime:2022/2/18 11:57 * @ version:1.0 * / public class Test {public static void main (String [] args) {new Phone () .charge (new PhoneAdapter (new Ac ()) .outputDc ());}}

View test results

Comparative analysis scheme 1: class adapter

Advantage: because it inherits the adapted, it can rewrite the method of the adapted according to the demand, and it is more flexible.

Disadvantages: because inheritance is a single inheritance attribute, the target object must be an interface, which has some limitations.

Solution 2: object adapter

Transform the adaptation class, change the inheritance of the adapted to hold the adapted, follow the principle of synthetic reuse, use association relations instead of inheritance relations, further decouple and improve expansibility.

Solution 3: interface adapter

The interface adapter pattern, also known as the default adapter pattern, designs an intermediate abstract class to implement the interface, provides a default implementation for each method in the interface, and then defines a concrete adapter to inherit the default adapter. you only need to rewrite the methods you need to rewrite.

At this point, I believe you have a deeper understanding of "Java Adapter pattern sample Analysis". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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