JAVA design Mode (18)-Adapter (Adapter) mode __java

Source: Internet
Author: User
Tags inheritance

definition: Converts the interface of a class into another interface that the customer expects. The adapter mode makes it possible for those classes that could not work together because of incompatible interfaces.

Type: class object Structure pattern

class Diagram:

In the GOF design pattern, there are two types of adapter patterns, the class adapter pattern and the object adapter pattern. Because the class adapter pattern matches one interface to another through multiple inheritance, and languages like C # and Java do not support multiple inheritance, this is only an introduction to object adapters.


The class adapter pattern uses multiple inheritance to match an interface to another interface, as shown in the following illustration:


Object Adapter Patterns depend on the combination of objects , as shown in the following illustration:



In real life, like this adapter we often see, and still often use it, such as: mobile phone (convinced that we have seen), our mobile phone in charge, it is not possible directly in the 220V power supply direct charge, but the mobile phone "charger" to switch to the phone needs of the voltage can be normal charging, Otherwise can not complete charging, this "charger" has played an appropriate role.


structure of adapter patterns

destination Interface (target): The interface that the customer expects. A target can be a specific or abstract class, or it can be an interface. needs to fit the class (Adaptee): Need to fit the class or the adapter class. Adapter (Adapter): Converts the original interface into a target interface by wrapping an object that needs to be fitted. Client: Collaborate with objects that conform to the target interface.


Code Implementation

1, the class adapter mode code implementation

Target
interface Target {public
	void request ();
}

Adaptee
class Adaptee {public
	void Specificrequest () {
		System.out.println ("Specificrequest ...");
	}
}

Adapter 
//Direct inheritance from Adaptee  using inheritance to implement
class Adapter extends Adaptee implements Target {

	@Override Public
	void Request () {
		this.specificrequest ();
	}
}

Client public
class Adapterclient {

	/**
	 * @param args
	/public static void Main (string[) args) {

		target target = new Adapter ();

		Target.request ();

	}

2, the object adapter mode code implementation

Target
interface Target {public
	void request ();
}

Adaptee
class Adaptee {public
	void Specificrequest () {
		System.out.println ("Specificrequest ...");
	}
}

Adapter 
//Implement Target interface to achieve
class Adapter implements target {
	private adaptee madaptee by object combination;
	
	Public Adapter (Adaptee madaptee) {
		this.madaptee = madaptee;
	}

	@Override public
	void request () {
		madaptee.specificrequest ();
	}
}

public class AdapterClient2 {

	/**
	 * @param args
	 *
	/public static void main (string[] args) {

		Adaptee madaptee = new Adaptee ();
		
		Target target = new Adapter (madaptee);
		Target.request ();
		
	}


advantages and disadvantages of adapter patterns

Advantages
With an adapter, the client can invoke the same interface, and thus transparent to the client. This is simpler, more direct, and more compact. The existing class is reused to solve the problem that the existing class and the reuse environment requirements are inconsistent. Decouple the target class and the adapter class and reuse the existing adaptor class by introducing an adaptor class without modifying the original code. An object adapter can fit multiple different adapter classes to the same target, that is, the same adapter can fit the adaptor class and its subclasses to the target interface. Disadvantage
The implementation of the replacement adapter is more complex for an object adapter.


Applicable Scenarios
The system needs to use existing classes, and the interfaces of these classes do not conform to the system's interfaces. Want to build a reusable class that works with some classes that don't have much of an association with one another, including some that might be introduced in the future. Two classes do things the same or similar, but with different interfaces. The old system developed classes have implemented some functionality, but the client can only be accessed in the form of a different interface, but we do not want to manually change the original class. With Third-party components, the component interface definition differs from its own definition and does not want to modify its own interface, but uses the functionality of a Third-party component interface.


Summary
In short, through the use of adapter mode, you can fully enjoy the class library migration, class library reuse of the fun to achieve better reuse.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.