Java Design pattern (designer PATTERNS in JAVA) reading abstract part 1th interface Mode--3rd Chapter adapter (Adapter) mode

Source: Internet
Author: User

When the client code provides an interface to write a concrete implementation class, it takes advantage of an existing class that already implements the interface functionality, but the interface's method name is inconsistent with the method name of an existing class, which requires an adapter pattern.

Interface adaptation

The Requiredinterface interface declares the Requiredmethod () method to be called by the client class, and Existingclass's Usefulmethod () provides a concrete implementation of this method, but the names of the two methods are different. This should be adapted to the Existingclass. The adaptive class Newclass inherits the Existingclass class and implements the Requiredinterface interface. The Newclass is an instance of the adapter pattern.

More specific instructions are given below.

Suppose we develop a simulation rocket flight and control program for the Oozinoz company. In Oozinoz company, there is an event emulator interface. Such as

In Oozinoz company, the Physicalrocket class realizes the function behavior required by simulation, we want to put it into the simulation function, at this time, we can use the adaptive pattern, create physicalrocket sub-class, and simultaneously implement Rocketsim interface. Such as

Rocketsim is the interface provided by the client, Physicalrocket implements the method required by the interface, Oozinozrocket inherits from Physicalrocket, realizes the Rocketsim, it provides the function satisfies the client's demand.

When the client defines the behavior it expects in the interface, it can apply the adapter pattern, provide a class that implements the interface, and have the class inherit from the existing class that provides the interface method functionality.

Class and Object adapters

The above adapter is the adapter for the class, but if the client gives us a class instead of an interface, then another adapter, the object adapter, is used. ,

The Newclass in the diagram is an example of an object adapter. It inherits Requiredclass, which is an instance of Requiredclass, and Newclass uses the Existingclass instance object to adapt Existingclass to meet the client's needs. So Newclass satisfies the needs of the client.

The following is a more specific example:

The com.oozinoz.simulation does not provide a rocket interface in this simulation package, but it provides a skyrocket class with general rocket features. However, if we are now simulating rockets with more complex functions, these features are available in another existing class Physicalrocket. In order to fit the Physicalrocket class, we need an adapter class. Such as

As an object adapter, the Oozinozskyrocket class inherits from the Skyrocket class, not the Physicalrocket class. When the emulator client needs to skyrocket an object, you can make the Oozinozskyrocket object replace it. By making the Simtime variable a protected, the Skyrocket class can support its subclasses.

Jtabel adaptation of the data

When data is displayed in a table, the object adapter is typically used. Swing provides the JTable control to display the table. JTable does not know what data we want to display, it defines a Tabelmodel interface, and also gives us a class Abstracttablemodel that implements some TableModel interface methods. The design of these two classes,

Suppose you need to use Swing's user interface to display several rockets in the table. As shown, the Rockettablemodel class can be created to match this set of rockets to the interface expected by TableModel.

The reason we use the object adapter pattern here is because the abstract class provides some of the implementation that the adapter needs, and the interface that the client expects, then we inherit from the abstract class instead of implementing the interface, and the adapter object has multiple rocket objects, so we also have to use a combination of methods, Instead of inheriting.

Summary: The adapter of the class inherits from the existing class, implements the target interface at the same time, and the object adapter inherits from the target class, referencing the existing class.

Summary

The adapter pattern allows us to reuse an existing class to meet the needs of the client.

If the client is using an interface to express its requirements, use the class's adapter, or use an object adapter if the client is not using an interface to represent its requirements, or if the adapter object is to hold instances of more than one existing class.

Java Design pattern (designer PATTERNS in JAVA) reading abstract part 1th interface Mode--3rd Chapter adapter (Adapter) mode

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.