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

What is an adapter pattern in the series of design patterns?

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what is the adapter pattern of the design pattern series". In the daily operation, I believe that many people have doubts about what the design pattern series is about the adapter pattern. The editor consulted all kinds of materials and sorted out the simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what is the adapter pattern in the design pattern series". Next, please follow the editor to study!

1. Overview

The adapter pattern is a structural design pattern that enables objects with incompatible interfaces to cooperate with each other.

2. Applicable scenarios

1) you can use the adapter class when you want to use a class but its interface is not compatible with other code.

2) if you need to use such classes, they are in the same inheritance system, and they have some additional common methods, but these common methods are not common to all subclasses in this inheritance system. These methods can be encapsulated in a decorator.

3. Examples

There are the following scenarios:

The square nail is suitable for the adapter of the round hole, and if the square nail wants to be put into the round hole, the diameter of the round hole is equal to the diagonal length of the square nail.

The square nail is suitable for the adapter of the round hole, and if the square nail wants to be put into the round hole, the diameter of the round hole is equal to the diagonal length of the square nail. Define square nail, round hole round hole: diameter round nail: diameter square nail: side length

Define square nails:

Public class SquareNails {public double getWidth () {return width;} public void setWidth (double width) {this.width = width;} public SquareNails (double width) {this.width = width;} / * * side length * / private double width;}

Define the pin:

Public class RoundNails {/ * diameter * / private double diameter; public double getDiameter () {return diameter;} public void setDiameter (double diameter) {this.diameter = diameter;} public RoundNails (double diameter) {this.diameter = diameter;}}

Define the round hole:

Public class RoundHold {/ * diameter * / private double diameter; public RoundHold (double diameter) {this.diameter = diameter;} public double getDiameter () {return diameter;} public void setDiameter (double diameter) {this.diameter = diameter } / * check whether * @ param roundNails * @ return * / public boolean fits (RoundNails roundNails) {if (diameter > = roundNails.getDiameter ()) {return true;} else {return false;}

Define the adapter:

Public class SquareNailsRoundHoldAdapter {public RoundNails getResult (SquareNails squareNails) {double width = squareNails.getWidth (); double diagonal = width * Math.sqrt (2); RoundNails roundNails = new RoundNails (diagonal); return roundNails;}}

Test class:

@ RunWith (SpringRunner.class) @ SpringBootTest (classes = TestApplication.class) public class TestDemo {@ Test public void test () {/ / define a round hole RoundHold roundHold = new RoundHold (10); / / define a round nail RoundNails roundNails = new RoundNails (10); / / define a square nail, margin 10 SquareNails squareNails10 = new SquareNails (10) / / define the square pin, margin 6 SquareNails squareNails6 = new SquareNails (6); / / Adapter SquareNailsRoundHoldAdapter squareNailsRoundHoldAdapter = new SquareNailsRoundHoldAdapter (); RoundNails result10 = squareNailsRoundHoldAdapter.getResult (squareNails10); RoundNails result6 = squareNailsRoundHoldAdapter.getResult (squareNails6); / / whether the round nail fits if (roundHold.fits (roundNails)) {System.out.println ("this round nails is fits") } whether else {System.out.println ("this round nails is does not fits");} / 10 square nails are suitable for if (roundHold.fits (result10)) {System.out.println ("squareNails10 is fits");} else {System.out.println ("squareNails10 is does not fits") } / / 6 whether the square nail fits if (roundHold.fits (result6)) {System.out.println ("squareNails6 is fits");} else {System.out.println ("squareNails6 is does not fits");}

Results:

This round nails is fits squareNails10 is does not fits squareNails6 is fits

4. Summary

Advantages:

1) single principle: distinguish the process of code or data conversion from the main business logic.

2) Open and close principle: as long as the client code interacts with the adapter through the client interface, you can add a new type of adapter to the program without modifying the existing client code.

Disadvantages:

Increase code complexity. When using it, you need to consider whether it is easier to modify the original function.

At this point, the study of "what is an adapter pattern in the design pattern series" 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