PHP Adapter Mode Introduction _php Tips

Source: Internet
Author: User
Tags inheritance

Points:

1. The adapter pattern is mainly used to "want to reuse some existing classes, but the interface is inconsistent with the requirements of the reuse environment", in the legacy code reuse, class library migration is very useful.

2. Adapter mode has the object adapter and class adapter in two forms of implementation structure, but the class adapter in the "Multiple inheritance" implementation, resulting in poor high coupling, so generally not recommended. Object adapters take the form of "object Combination", which is more consistent with the spirit of loose coupling.

Realize:

Adapter pattern structure diagram for Class (inheritance)

Adapter pattern diagram (combination) of objects

(Code implementation of the object adapter)

Target: Defines a specific domain-related interface used by the client

void request ();}

Adaptee: Existing interfaces that need to fit now

void Specificrequest () {}}

Adapter: The interface to the Adaptee and the target interface are fitted

Private Adaptee Adaptee;}

Applicability:

1. The system needs to use existing classes, which do not conform to the requirements of the system.

2. Want to create a reusable class that works with some classes that don't have too much association with one another, including some that might be introduced in the future. These source classes do not necessarily have very complex interfaces.

3. (For object adapters) in the design, you need to change the interface of multiple existing subclasses, and if you use the adapter pattern of the class, make an adapter for each subclass, which is impractical.

Effect and advantages and disadvantages:

For class adapters:

1. Match adaptee and Taget with a specific adapter class. The result is that class adapter will not work when we want to match a class and all its subclasses.

2. Allow adapter to override (redefine) adaptee part of the behavior, because adapter is a subclass of Adaptee.

For object adapters:

1. Allows a adapter to work with multiple adaptee, that is, the adaptee itself and all its subclasses (if any). Adapter can also add functionality to all adaptee at once.

2. Make override (redefine) Adaptee behavior more difficult. If you must override the Adaptee method, you have to do a adaptee subclass to override the Adaptee method, and then use the subclass as a true adaptee source.

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.