設計模式系列之適配器模式
本文轉載自微信公眾號「狼王編程」,作者狼王。轉載本文請聯(lián)系狼王編程公眾號。
1、概述
適配器模式是一種結構型設計模式, 它能使接口不兼容的對象能夠相互合作。
2、適用場景
1)當你希望使用某個類, 但是其接口與其他代碼不兼容時, 可以使用適配器類。
2)如果您需要使用這樣一些類, 他們處于同一個繼承體系, 并且他們又有了額外的一些共同的方法, 但是這些共同的方法不是所有在這一繼承體系中的子類所具有的共性??梢詫⑦@些方法封裝在一個裝飾器中。
3、實例
有以下場景:
方釘適配圓孔的適配器,方釘想放到圓孔中,則圓孔的直徑等于方釘?shù)膶情L度。
- 方釘適配圓孔的適配器,方釘想放到圓孔中,則圓孔的直徑等于方釘?shù)膶情L度。
 - 定義方釘、圓孔
 - 圓孔:
 - 直徑
 - 圓釘:
 - 直徑
 - 方釘:
 - 邊長
 
定義方釘:
- public class SquareNails {
 - public double getWidth() {
 - return width;
 - }
 - public void setWidth(double width) {
 - this.width = width;
 - }
 - public SquareNails(double width) {
 - this.width = width;
 - }
 - /**
 - * 邊長
 - */
 - private double width;
 - }
 
定義圓釘:
- public class RoundNails {
 - /**
 - * 直徑
 - */
 - private double diameter;
 - public double getDiameter() {
 - return diameter;
 - }
 - public void setDiameter(double diameter) {
 - this.diameter = diameter;
 - }
 - public RoundNails(double diameter) {
 - this.diameter = diameter;
 - }
 - }
 
定義圓孔:
- public class RoundHold {
 - /**
 - * 直徑
 - */
 - private double diameter;
 - public RoundHold(double diameter) {
 - this.diameter = diameter;
 - }
 - public double getDiameter() {
 - return diameter;
 - }
 - public void setDiameter(double diameter) {
 - this.diameter = diameter;
 - }
 - /**
 - * 校驗是否合適
 - * @param roundNails
 - * @return
 - */
 - public boolean fits(RoundNails roundNails){
 - if (diameter >= roundNails.getDiameter()){
 - return true;
 - }else {
 - return false;
 - }
 - }
 - }
 
定義適配器:
- 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;
 - }
 - }
 
測試類:
- @RunWith(SpringRunner.class)
 - @SpringBootTest(classes = TestApplication.class)
 - public class TestDemo {
 - @Test
 - public void test() {
 - //定義個圓孔
 - RoundHold roundHold = new RoundHold(10);
 - //定義圓釘
 - RoundNails roundNails = new RoundNails(10);
 - //定義方釘,邊距10
 - SquareNails squareNails10 = new SquareNails(10);
 - //定義方釘,邊距6
 - SquareNails squareNails6 = new SquareNails(6);
 - //適配器
 - SquareNailsRoundHoldAdapter squareNailsRoundHoldAdapter = new SquareNailsRoundHoldAdapter();
 - RoundNails result10 = squareNailsRoundHoldAdapter.getResult(squareNails10);
 - RoundNails result6 = squareNailsRoundHoldAdapter.getResult(squareNails6);
 - //圓釘是否合適
 - if (roundHold.fits(roundNails)) {
 - System.out.println("this round nails is fits");
 - } else {
 - System.out.println("this round nails is does not fits");
 - }
 - //10方釘是否合適
 - if (roundHold.fits(result10)) {
 - System.out.println("squareNails10 is fits");
 - } else {
 - System.out.println("squareNails10 is does not fits");
 - }
 - //6方釘是否合適
 - if (roundHold.fits(result6)) {
 - System.out.println("squareNails6 is fits");
 - } else {
 - System.out.println("squareNails6 is does not fits");
 - }
 - }
 - }
 
結果:
- this round nails is fits
 - squareNails10 is does not fits
 - squareNails6 is fits
 
4、總結
優(yōu)點:
1)單一原則:將代碼或者數(shù)據(jù)轉換的過程從主要業(yè)務邏輯區(qū)分出來。
2)開閉原則:只要客戶端代碼通過客戶端接口與適配器進行交互, 你就能在不修改現(xiàn)有客戶端代碼的情況下在程序中添加新類型的適配器。
缺點:
增加代碼復雜度。使用時需要考慮是否在原功能上修改更加簡單。















 
 
 










 
 
 
 