Learning php design patterns php synthesis patterns (composite)

Source: Internet
Author: User
This article mainly introduces the synthesis mode in the php design mode, and uses php to implement the synthesis mode. if you are interested, refer to it. I. intention
Combine objects into a tree structure to represent the "part-whole" hierarchy. Composite allows you to use a single object and a Composite object in a consistent manner.
Composite changes the structure and composition of an object.
II. main roles in merging mode
Abstract Component role:Abstract roles to specify an interface for the objects that participate in the combination. When appropriate, implement the default behavior of all class interfaces. Declare an interface to access and manage child components of Component
Leaf component (Leaf) role:Indicates the leaf node object in the combination. the leaf node does not have any subnodes. Defines the behavior of the element object in the combination.
Composite role:Storage sub-parts. Define the behavior of the parts with child parts. Perform operations related to sub-parts in the Component interface.
Client ):Widget object control through Component interface
III. Advantages and disadvantages of the merging mode
Advantages of the Composite mode
1. Simplified customer code
2. make it easier to add new types of components

Disadvantages of the Composite mode: to make your design more general and easily add components will also cause some problems, that is, it is difficult to limit components in the combination.
IV. applicable scenarios of the merging mode
1. you want to represent the part of the object-the overall hierarchy
2. if you want to ignore the differences between a composite object and a single object, you will use all objects in the composite structure in a unified manner.
V. merging mode and other modes
Decorator mode:The Decorator mode is often used with the Composite mode. When decoration and synthesis are used together, they usually have a common parent class. Therefore, decoration must support Component interfaces with add, remove, and getChild operations.
Metadata mode:The Flyweight mode allows you to share components without referencing their parent parts.
Iterator mode:Itertor can be used to traverse Composite
Visitor mode:Visitor will be originally distributed in the Composite and Leaf classes for operation and behavior localization.
6. safe synthesis mode
Declare all methods used to manage subclass objects in the Composite class. This approach is safe. Because leaf objects do not have any subclass management methods at all, if the client uses these methods for leaf objects, the program will encounter errors during compilation. No running errors will occur after Compilation and Translation.
This disadvantage is that it is not transparent, because the leaf class and the synthesis class will have different interfaces.
VII. structure of a secure synthesis mode

8. safe PHP synthesis mode example

<? Php/*** abstract Component role */interface Component {/*** return your instance */public function getComposite (); /*** example method */public function operation ();}/*** tree branch Component role */class Composite implements Component {private $ _ composites; public function _ construct () {$ this-> _ composites = array ();} public function getComposite () {return $ this;}/*** example method, call the operation method */public function operation () {echo 'composite operation begin:
'; Foreach ($ this-> _ composites as $ composite) {$ composite-> operation ();} echo 'composite operation end:

';}/*** Add a sub-object by using the aggregation management method * @ param Component $ component sub-object */public function add (Component $ component) {$ this-> _ composites [] = $ component ;} /*** delete a sub-object using the aggregation management method * @ param Component $ component sub-object * @ return boolean: whether the sub-object is deleted successfully */public function remove (Component $ component) {foreach ($ this-> _ composites as $ key => $ row) {if ($ component = $ row) {unset ($ this-> _ composites [$ key]); return TRUE ;}} return FALSE ;} /*** the aggregation management method returns all sub-objects */public function getChild () {return $ this-> _ composites ;}} class Leaf implements Component {private $ _ name; public function _ construct ($ name) {$ this-> _ name = $ name;} public function operation () {echo 'leaf operation', $ this-> _ name,'
';} Public function getComposite () {return null;}/*** Client */class Client {/*** Main program. */public static function main () {$ leaf1 = new Leaf ('first'); $ leaf2 = new Leaf ('second'); $ composite = new Composite (); $ composite-> add ($ leaf1); $ composite-> add ($ leaf2); $ composite-> operation (); $ composite-> remove ($ leaf2 ); $ composite-> operation () ;}} Client: main () ;?>

The above is the code that uses php to implement the merging mode. There are also some concepts about the merging mode, and I hope it will be helpful for everyone's learning.

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.