Adapter mode and appearance mode in Java Design Mode

Source: Internet
Author: User

Adapter Mode

The so-called adapter mode converts an interface of a class to another interface that the customer expects. The adapter allows classes that are not compatible with the original interface to work together. The main components of the adapter include: Target Interface: the interface adapter desired by the Client: This is to convert the adapter to the Target Interface, generally to achieve the target interface, the adapter contains the accessory Member, which is used for transformation. It is provided to the adapter for conversion. It is usually required to bind the adapter to the adapter for conversion. Generally, this mode converts an interface to another interface without modifying the existing code. It mainly includes two methods: Object Adapter: this is to use a combination in the adapter for processing, the adapter will be used as a member attribute of the adapter, in addition, the adapter implements or inherits the target interface code instance required by the customer. For example, if there is two interfaces for a puppy and a kitten, the customer now needs a kitten, but only the puppy interface, the adapter can be used for conversion. Here, only the adapter code is written: package test;
Publicclass AnimalAdapter implements Cat {
Private Dog; // Adapter
Public AnimalAdapter (Dog dog)
{
This. dog = dog;
}

Publicvoid mimi (){
Dog. wow ();
}
Publicvoid run (){
Dog. run ();
}

Publicvoid sleep (){
Dog. sleep ();
}
}

Appearance Mode So-called appearance ModeProvides a unified interface to access a group of interfaces in the subsystem. It defines a high-level interface to make subsystems easier to use. The appearance mode does not encapsulate sub-system classes. The appearance only provides simpler interfaces. It still contains the objects of interfaces in the sub-system, in addition, all interfaces of the subsystem are included in the combination. It not only provides a simplified interface, but also exposes the complete functions of the system. Code example: if you want to go home and enter the room for various operations, you can view these operations in a sub-system. All operations can be simplified. Appearance mode package test;

Import com. whut. command. Light;

Publicclass Facede {
// Perform operations on various subsystems
Private Light light;
Private Door;
Private Window window;

Publicvoid goHome ()
{
Light. on ();
Door. open ();
Window. open ();
}

} In the adapter mode, one or more class interfaces are converted into the desired interfaces, instead of an interface. The appearance mode can also provide simplified interfaces for only one class with complex interfaces. The main purpose of the adapter pattern design is to change the existing interface to the interface appearance pattern that meets the customer's expectation. The intention is to simplify all interfaces of a complicated subsystem, to provide a convenient and convenient interface for the customer to design the pattern principle: the minimum knowledge principle, reduce the interaction between objects, leaving only a few "close friends ". To follow this principle, we should generally only call methods that fall within the following scope: 1. the object itself 2 is passed in as a method parameter 3 any component of any object 4 object created or instantiated by this method, that is, do not call all member variables or methods in the object. When an object calls other methods, the returned results are returned. Two modes: 1) when you need to use an existing class but its interface does not meet your needs, you can use the adapter Mode 2) when you need to simplify and unify a large interface or a group of complex interfaces, 3) the adapter changes the interface so that it meets the customer's expectations. 4) appearance decouples customers from a complex Subsystem

5) the adapter wraps an object to change its interface.

The modifier packs an object to add new behaviors and responsibilities. The appearance wraps a group of objects to simplify their interfaces.

This article from the "dream in the cloud" blog, please be sure to keep this source http://computerdragon.blog.51cto.com/6235984/1165985

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.