Learning php design patterns php bridge implementation)

Source: Internet
Author: User
Tags abstract definition php example
This article mainly introduces the bridge mode in the php design mode and uses php to implement the bridge mode. if you are interested, refer to it. I. Structure of bridge mode

II. main roles in the bridge model
Abstract action role: defines the abstract class interface and saves a reference to the implemented object.
Modify abstract roles: expand abstract roles, and modify the abstract definitions of the parent class.
Implementor role: defines the interface for implementing classes and does not provide specific implementations. This interface is not necessarily the same as the abstract role interface definition. In fact, these two interfaces can be completely different. An implemented role should only provide underlying operations, while an abstract role should only provide operations at a higher level based on underlying operations.
Concrete Implementor: implements the role interface and defines its specific implementation.
III. advantages of the bridge model
1. separation interface and its implementation
Sharing pull action with Implementor helps reduce dependencies on partial compilation times.
Sharing interfaces and implementations helps to create better structured systems.
2. Improved Scalability
3. the implementation details are transparent to the customer.
IV. applicable scenarios of the bridge model
1. if a system requires more flexibility between the abstraction of components and the specific roles, it will avoid establishing static connections between the two layers.
2. any change to the role should not affect the client, or the change to the role should be completely transparent to the client.
3. a component has more than one abstract role and actual role, and the system needs dynamic coupling between them.
4. although there is no problem in using inheritance in the system, abstract roles and specific roles need to be changed independently, and design requirements need to be managed independently.
V. Bridge mode and other modes
Abstract factory mode (abstract factory mode ):Abstract Factory mode can be used to create and configure a specific Bridge mode.
Adapter mode (adapter mode ):The adapter mode is used to help irrelevant classes work collaboratively. It is usually used after the system is designed. However, the bridge mode is used at the beginning of the system, so that the abstract interfaces and implementations can be changed independently.
Status mode ):The bridge mode describes the relationship between two levels, and the State mode describes the relationship between an object and a state object. The State mode is a special case of degradation in the bridge mode.
VI. PHP example of bridge mode

<? Php/*** abstract role ** abstract definition, and save a reference to the implementation object. */Abstract class define action {/* reference to the implemented object */protected $ imp;/*** operation method */public function operation () {$ this-> imp-> operationImp () ;}/ *** modify abstract roles * extend abstract roles, and modify the abstract definition of the parent class. */Class RefinedAbstraction extends action {public function _ construct (Implementor $ imp) {$ this-> imp = $ imp ;} /*** implementation of the operation method in correcting abstract roles */public function operation () {echo 'refined?action operation'; $ this-> imp-> operationImp ();}} /***** the implemented role provides the interface for implementing the role, but does not provide the specific implementation. */Abstract class Implementor {/*** implementation declaration of operation methods */abstract public function operationImp ();} /*** specify role A ** to provide specific implementation of the role interface */class ConcreteImplementorA extends Implementor {/*** implementation of the operation method */public function operationImp () {echo 'concrete implementor A operation
';}} /*** Specify role B ** to provide specific implementation of the role interface */class ConcreteImplementorB extends Implementor {/*** implementation of the operation method */public function operationImp () {echo 'concrete implementor B operation
';}}/*** Client */class Client {/*** Main program. */public static function main () {$ response action = new RefinedAbstraction (new ConcreteImplementorA (); $ response action-> operation (); $ response action = new refined1_action (new ConcreteImplementorB (); $ response action-> operation () ;}} Client: main ();?>

The above is the code for implementing the bridge mode using php, and there are some concepts about the decoration mode, I hope to help you learn.

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.