Integration and combination in design patterns-implementation in code

Source: Internet
Author: User
● Introduction:


I have been reading the design pattern recently. I have written an article about the implementation of the relationship in the design pattern in the code.


Then they asked me how aggregation and combination show different lifecycles in code. Because I did not really understand it at the time and did not dare to answer it. Now I have a little bit of experience to share with you.


● Definition:


Aggregation: the weak relationship between two objects is the whole and the part, and the part lifecycle can surpass the whole. Such as a computer and a mouse.


? Aggregation in big words:



? Big talk combination:


Indicates that the two objects are integral and partial.StrongLink. Some lifecycles cannot go beyond the whole, or exist without the whole. The "part" of the composite relationship cannot be shared across the whole.



● Similarities and Differences


Commonalities: they are all associated, and the relationship between the whole and the part.

Difference: different lifecycles, aggregation is independent, and does not survive as a whole (the lifecycle is not synchronized ). The combination is the opposite. It is the same as the overall survival (synchronization of life cycle ).


● Implementation in code


? Aggregation


The implementation in the code is more flexible. The geese are aggregated into geese, as long as the geese are members of the geese.


Code can be aggregated in either of the following ways:

Method 1: This method is generally used when Dayan widegoose is an abstract class (parent class). At this time, different subclasses can be passed in, which makes it flexible to call.


Class wirdgooseaggregate {private widegoose; // define the member variable Dayan public setwidegoose (widegoose W) // obtain the object of Dayan by passing parameters {This. widegoose = W ;}}

The second method: This method is hard to write and cannot be flexible, but it also has the advantage of defining an initial value. This method is used in the state mode to define an initial object.


Class wirdgooseaggregate {private widegoose; // define the member variable Dayan public setwidegoose () // get the object of Dayan by passing the parameter {widegoose = new wideblackgoose (); // wideblackgoose is a subclass of widegoose }}

? Combination


In the Code, there is no such flexibility. It is strongly coupled, and its lifecycle is the same as the relationship between birth and death. We know that an object is generated in our sense when it is instance, so we put the combined object in the constructor of the combined object:


Class bird {private wing; // member variable defines the Wing Public bird () // instantiate the wing object in the constructor {wing = new wing ();}}

● Summary


When we learn things, we really want to understand it all at once. In fact, learning is a constant thought and research. What we need to do is know it, study it, use it, and communicate with it.






Integration and combination in design patterns-implementation in code

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.