Design Pattern (5) — Builder pattern

Next: Design pattern (7) – bridge mode

A summary,

Official explanation: Convert the inface of a class into another interface clients expect.Adapter lets classes work together that couldn’t Otherwise because of the incompatible interface. (Transforming the interface of one class into the expected interface of the client, thus enabling two classes to work together that otherwise would not work together due to interface mismatch.)

My understanding:

There are three elements of an Adapter mode: Adaptee, Adapter, and Target

There are two implementations: class adapter and object adapter

Participants: Target Target class, Adaptee Adapter class, and Adapter Adapter class

Class diagram:

Second, the code

2.1 Do not use the adapter mode

class Adaptee {
	public void display(a) {
		System.out.println("This is an Adaptee"); }}interface Target {
	 void _target(a);
}
Copy the code

As above, there is a source Adaptee and a Target Target.

How to use the Adaptee method at Target? Use inheritance adaptors or injection reference methods, but implement them in an intermediate class.

Because Target is an interface or abstract class (objects cannot be instantiated on the client side), this intermediate class cannot be acted for by Target in order to use the Adaptee method at Target.

Then write a string of code to implement the requirements of the intermediate class:

package mypackage;

public class TestAdapterPattern {

	public static void main(String[] args) {
		ConcreteTarget _tTarget = new ConcreteTarget();
		_tTarget.set_adaptee(newAdaptee()); _tTarget._target(); }}class Adaptee {
	public void display(a) {
		System.out.println("This is an Adaptee"); }}interface Target {
	void _target(a);
}

class ConcreteTarget implements Target {
	protected Adaptee _adaptee;

	@Override
	public void _target(a) {
		_adaptee.display();
	}

	public Adaptee get_adaptee(a) {
		return _adaptee;
	}

	public void set_adaptee(Adaptee _adaptee) {
		this._adaptee = _adaptee; }}Copy the code

Output:

This is an Adaptee
Copy the code

After writing it, you realize that you have actually used the adapter pattern, which is the idea of the object adapter (design pattern is like this, you write it when you think you don’t use it, but you actually use it).

Instead of using the adapter pattern, write an intermediate class to inject the Adaptee reference into it and call its methods. The result is to use the object adapter, which fully shows that the adapter pattern is inescapable. (That’s the great thing about design patterns. They’re more of an idea than a form. They don’t force you to use them, but when you’re writing or refactoring code, you can’t get around them and use them unconsciously.)

Class adapters and object adapters are described below:

2.2 Class Adapters

code

package mypackage;

public class TestAdapterPattern {

	public static void main(String[] args) {
		Adapter _tAdapter = newAdapter(); _tAdapter._target(); }}class Adaptee {
	public void display(a) {
		System.out.println("This is an Adaptee"); }}interface Target {
	void _target(a);
}

class Adapter extends Adaptee implements Target {

	@Override
	public void _target(a) {
		super.display(); }}Copy the code

Output:

This is an Adaptee
Copy the code

In order for Target to call a method in Adaptee, there are only two methods: inheritance and composition. The class Adapter uses the method that inherits Adaptee. Adaptee becomes the parent of the intermediate class Adapter, and Adapter can call its method at any time.

2.3 Object Adapter

Code:

package mypackage;

public class TestAdapterPattern {

	public static void main(String[] args) {
		Adapter _tAdapter = new Adapter();
		_tAdapter.set_adaptee(newAdaptee()); _tAdapter._target(); }}class Adaptee {
	public void display(a) {
		System.out.println("This is an Adaptee"); }}interface Target {
	void _target(a);
}

class Adapter implements Target {
	protected Adaptee _adaptee;

	@Override
	public void _target(a) {
		_adaptee.display();
	}

	public Adaptee get_adaptee(a) {
		return _adaptee;
	}

	public void set_adaptee(Adaptee _adaptee) {
		this._adaptee = _adaptee; }}Copy the code

Output:

This is an Adaptee
Copy the code

Summary: The object Adapter uses a composite approach, injecting the Adaptee into an intermediate class Adapter to make it easy to call its methods.

Third, summary

The Adaptee pattern is very simple, is to call another class in a Target method or access its members, there can be inheritance and composition methods. If Target is an interface or an abstract class, it cannot instantiate an object on the client side. All targets must have their own implementation class, which is a non-abstract Adapter. It is also in this Adapter class that Adaptee is inherited or combined to form a class Adapter and an object Adapter. Target calls Adaptee because Target is the parent of Adapter in the middle class.

 

Design Pattern (5) — Builder pattern

Next: Design pattern (7) – bridge mode