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

Adapter pattern for design pattern (structural)

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

Share

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

Adapter pattern definition: converts the interface of one class to another interface that the customer wants. The Adapter pattern allows classes that cannot work together because of interface incompatibility.

The adapter pattern is divided into class adapter pattern and object adapter pattern. The only difference is whether the adaptation of the adapter role to the adapted role is accomplished through inheritance or combination. Because multiple inheritance is not supported in java, and inheritance is suspected of breaking encapsulation, many books advocate the use of composition instead of inheritance. In order to implement the class adapter pattern, it needs to be implemented through multi-inheritance.

1. UML schematic diagram

1) inherit the original interface class (class adapter pattern)

2) combine the original interface classes (object adapter pattern)

2. Composition:

1) inherit the original interface class (class adapter pattern):

A, Target role: this is the expected interface. Note that for java, since the class adapter pattern is discussed here, the target cannot be a class.

B, source (Adaptee) role: existing interfaces that need to be adapted.

C, Adapter role: the adapter class is the core of this pattern. The adapter converts the source interface to the destination interface. Obviously, this role cannot be an interface, but must be a concrete class.

2) combine the original interface classes (object adapter pattern):

A, Target role: this is the expected interface. Note that for java, the target can be a concrete or abstract class.

B, Adaptee role: this role has an interface that already exists and is used, and this interface requires us to adapt.

C, the Adapter role: the core of this adapter pattern. It converts the interface that the source role already has into the interface that the target role wants. For java, this role must be a concrete class.

Third, code implementation:

1) inherit the original interface class (class adapter pattern):

JAVA:

ClassAdapter.java:

Interface Target {

Public void operation1 ()

Public void operation2 ()

}

Class Adaptee {

Public void operation2 () {

System.out.println ("operation2 come from Adaptee")

}

}

Class Adapter extends Adaptee implements Target {

@ Override

Public void operation1 () {

/ / TODO Auto-generated method stub

System.out.println ("operation1")

}

}

Public class ClassAdapter {

Public static void main (String [] args) {

Adapter ad = new Adapter ()

Ad.operation1 ()

Ad.operation2 ()

}

}

Running result:

Operation1

Operation2 come from Adaptee

Caterpillar:

/ / =

/ / Name: ClassAdapter.cpp

/ / Author: Yan Chao

/ / Version:

/ / Copyright: Your copyright notice

/ / Description: Hello World in Clippers, Ansi-style

/ / =

# include

Using namespace std

Class Target {

Public:

Virtual void operation1 () = 0

Virtual void operation2 () = 0

Virtual ~ Target () {}

}

Class Adaptee {

Public:

Void operation1 () {

Cout operation2 ()

Return 0

}

Running result:

Operation1 from Adaptee!

Operation2!

2) combine the original interface classes (object adapter pattern):

JAVA:

ObjectAdapter.java:

Interface Target {

Public void operation1 ()

Public void operation2 ()

}

Class Adaptee {

Public void operation2 () {

System.out.println ("operation2 come from Adaptee")

}

}

Class Adapter implements Target {

Private Adaptee adaptee

Public Adapter (Adaptee adaptee) {

This.adaptee = adaptee

}

@ Override

Public void operation1 () {

/ / TODO Auto-generated method stub

System.out.println ("operation1")

}

@ Override

Public void operation2 () {

/ / TODO Auto-generated method stub

Adaptee.operation2 ()

}

}

Public class ObjectAdapter {

Public static void main (String [] args) {

Adaptee ae = new Adaptee ()

Adapter adapter = new Adapter (ae)

Adapter.operation1 ()

Adapter.operation2 ()

}

}

Running result:

Operation1

Operation2 come from Adaptee

Caterpillar:

/ / =

/ / Name: ObjcetAdapter.cpp

/ / Author: Yan Chao

/ / Version:

/ / Copyright: Your copyright notice

/ / Description: Hello World in Clippers, Ansi-style

/ / =

# include

Using namespace std

Class Target {

Public:

Virtual void operation1 () = 0

Virtual void operation2 () = 0

Virtual ~ Target () {}

}

Class Adaptee {

Public:

Void operation1 () {

Cout

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

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report