Java design Mode _ (structured) _ Adapter mode __java

Source: Internet
Author: User
Tags wrapper

Quote Encyclopedia

In computer programming, adapter patterns (sometimes referred to as wrapper styles or wrappers) fit the interface of a class to what the user expects. A fitting allows a class that is usually not working together because the interface is incompatible, by wrapping its own interface in an existing class.


Basic Information

There are two types of adapter modes: Class Adapter Mode-This adapter mode, the adapter inherits from the implemented Class (general multiple inheritance). Object adapter Pattern--In this adapter mode, the adapter holds an instance of the class it wraps. In this case, the adapter invokes the physical entity of the wrapped object. The roles involved in the pattern are:

destination (target) role: the desired result.

Source (otherobject) role: An adaptive role is required.

Adapter (adaper) Role: The adapter class is the core of the pattern. The adapter converts the source interface to the target interface. Obviously, this role can not be an interface, but must be a concrete class.


For example, a requirement is to add a new method method2 to a target class, and the target class is preceded by only one method method1, namely: the final implementation result is to provide two methods method1 and METHOD2, and to implement as much as possible without modifying the target source method. You can then use the adapter mode for implementation.

Class Adapter Pattern implementation:


Specific code:
1, define the final provision of the target method targetinterface the following is the code for the target role, which is implemented as a Java interface. As you can see, this interface adds a new method: Method2 (). The source role Otherobject is a concrete class that has a method2 () method, but no method1 () method.

Target method Public
interface Targetinterface {public
	String method2 ();
}
2, specific categories:
Concrete class public
class Otherobject {public
	String method1 () {return
		' string ';
	}
}
3, Adapter: Adapter role Adapter Extended Otherobject, but also achieved the target (Targetinterface) interface. Because Otherobject does not provide a method2 () method, and the target interface requires this method, the adapter role adapter implements this method.
Adapter class public class
Adapter extends Otherobject implements Targetinterface {
	//adapter implementation Otherobject Not implemented method
	@Override public
	String method2 () {return
		"";
	}
}
The above code implements the extension of the class through the adapter pattern.
Object Adapter Mode implementation: As with the adapter pattern of the class, the object's adapter pattern transforms the API of an adapter class into an API for the target class, unlike the adapter pattern of the class, where the object's adapter pattern is not connected to the Otherobject class using an inheritance relationship, but instead uses a delegated relationship to connect to the adapter class
Concrete class public
class Otherobject {public
	String method1 () {return
		' string ';
	}
}
Adapter class public
class Adapter {

	//associated combination object
	private otherobject otherobject;

	Public Adapter (Otherobject otherobject) {
		this.otherobject = otherobject;
	}

	/**
	 * Source Class Adaptee has a method method1 so the adapter class can be directly delegated
	/public String method1 () {return
		This.otherObject.method1 ();
	}

	The adapter class needs to supplement this method with public
	void Method2 () {
		//TODO auto-generated methods stub

	}
}
As you can see from the diagram above, the Otherobject class does not have a method2 () method, and the client expects it. To enable clients to use the Adaptee class, you need to provide a wrapper (wrapper) class adapter. This wrapper class wraps a Otherobject instance so that the wrapper class can connect the Otherobject API with the target class API. Adapter and Otherobject are delegated relationships.

Comparison of class adapters and object adapters 1 The class adapter is statically defined by the way object is inherited, whereas object adapters use the combination of objects in a dynamic way. 2)

For an object adapter, an adapter can fit a variety of different sources to the same target. In other words, the same adapter can fit both the source class and its subclasses to the target interface.

3 for class adapters, the adapter can redefine part of the behavior of the adaptee, which is equivalent to the partial implementation of the subclass covering the parent class.

So:

It is recommended that you use the implementation of the object adapter as much as possible, using compositing/aggregation and less inheritance.



Advantages of Adapter mode 1) Better reusability

The system needs to use existing classes, and the interfaces of this class do not conform to the requirements of the system. Then the adapter mode allows these functions to be better reused.

2) Better Scalability

When implementing the adapter function, you can call your own developed functions to naturally extend the functionality of the system


Disadvantages of Adapter mode

Excessive use of adapters, will make the system very messy, not easy to grasp the overall. If you need to fit a lot of components, you can not use the adapter, but directly to the system refactoring.








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.