Adapter pattern of java design pattern (5)

In life, there is no permanent pain, no matter how deep the pain is, the wound will always heal when it is cut. In life, there are no hurdles that you cannot pass. You can't sit by the hurdle and wait for it to disappear. You can only find a way to go through it. Life, without eternal love, feelings without ending, will always end; those who cannot have it will always forget.

Design pattern learning, I will blog about 23 design patterns in the near future , so stay tuned~
--1/21/2021

Structural pattern classification

  • Adapter mode
  • Agency model
  • Bridge mode
  • Decorative pattern
  • Combination mode
  • Appearance mode
  • Flyweight model

Structural model definition

Realize loose coupling from the structure of the program, thereby expanding the overall structure to solve larger problems

Baidu Encyclopedia

Adapter mode definition

To implement the interface of a class into the interface that the client wants, the adapter mode is to group two unrelated classes together, acting as a'middleman', allowing them to work together

Baidu Encyclopedia

analysis

效果图(1.1):


Now that science and technology are getting more and more advanced, computers are getting thinner and thinner, causing the network cable to be plugged into the computer. This is equivalent to using the adapter mode. The network cable and the computer are merged together to allow the computer to connect to the network!

Code:

Computer:

public class Computer {
    
    
    //电脑需要转接线才能上网
    public void net(Adapter adapter){
    
    
        adapter.net();
    }
}

Netting network cable:

public class Netting {
    
    
    public void ConnectNetwork(){
    
    
        Log.i("适配器模式:","已经连接网络");
    }
}

INettingToComputer network cable interface (in order to meet the opening and closing principle)

public interface INettingToComputer {
    
    
    public void net();
}

Adapter converter:

public class Adapter implements INettingToComputer{
    
    

    private  Netting netting;
    //将网线组合进来
    public Adapter(Netting netting) {
    
    
        this.netting = netting;
    }
    @Override
    public void net() {
    
    
        netting.ConnectNetwork();
    }
}

Code:

 //电脑
Computer computer = new Computer();

//网络
Netting netting = new Netting();

//转换器
Adapter adapter = new Adapter(netting);

//电脑通过转换器连接网络
computer.net(adapter);

效果图(1.2):

analysis:

  • Adapter
  • Computer
  • Netting cable
  • INettingToComputer network cable interface (Adapter implementation)

advantage:

  • An object adapter adapts multiple different adapters to the same class
  • A subclass of an adaptor can be adapted. Since the adapter and the adaptor were previously associated , the subclass of the adaptor can also be adapted through the adaptor according to the Richter substitution principle.

This is a simple adapter pattern

Complete code

Recent articles:

Construction type of java design pattern: singleton pattern (1)

Construction type of java design pattern: factory method/abstract factory pattern (2)

Construction type of java design pattern: prototype pattern (3)

The construction type of java design pattern: builder pattern (4)

Go to the design pattern/design principle catalog page

Originality is not easy, remember to like and support it~

Guess you like

Origin blog.csdn.net/weixin_44819566/article/details/112365209