PHP Adapter Mode Introduction _php Tutorial

Source: Internet
Author: User
Points:

1. The adapter pattern is primarily used in situations where "you want to reuse some existing classes, but interfaces are inconsistent with the reuse environment", which is useful in legacy code reuse, class library migrations, and so on.

2. Adapter mode has two forms of object adapter and class adapter implementation structure, but the class adapter with "Multi-inheritance" implementation, resulting in poor high coupling, it is generally not recommended. Object adapters Use the "object combination" approach, more in line with the loose coupling spirit.

Realize:

class's adapter pattern structure diagram (inheritance)

The adapter mode structure of the object (combination)

(Code implementation of the object adapter)

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

 Public Interface void request ();

Adaptee: An existing interface that needs to be adapted now

 Public class  Public void specificrequest () {}}

Adapter: Adapting the interface of the adaptee to the target interface

 Public class Implements  Public Super  This  Public void Private Adaptee Adaptee;}

Applicability:

1. The system needs to use the existing classes, and the interfaces of this class do not meet the needs of the system.

2. Want to create a reusable class that works with some classes that are not too much related to each other, 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 interfaces of several existing subclasses, and if you use the class's adapter pattern, you should make an adapter for each subclass, which is not practical.

Effects and pros and cons:

For class adapters:

1. Match the Adaptee and Taget with a specific adapter class. The result is that when we want to match a class and all of its subclasses, class adapter will not be able to do the job.

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

For object adapters:

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

2. Making the override (redefinition) of adaptee more difficult. If you must override the Adaptee method, then you have to do a adaptee subclass to override the Adaptee method, and then the subclass as a true adaptee source to adapt.

http://www.bkjia.com/PHPjc/325744.html www.bkjia.com true http://www.bkjia.com/PHPjc/325744.html techarticle point: 1. Adapter mode is mainly used in the "want to reuse some existing classes, but the interface and reuse environment requirements inconsistent," in the legacy code reuse, class library migration and other parties ...

  • 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.