[Original · tutorial · serialization] "Android's big talk Design Pattern"-design principles Chapter 6: Synthesis, aggregation, reuse principles Liu Bang vs Han Xin

Source: Internet
Author: User
<Big talk design model> description and copyright notice of this tutorial

L this document references and uses free images and content on the Internet, and is released in a free and open manner, hoping to contribute to the mobile Internet and the smart phone age! This document can be reproduced at will, but cannot be used for profit.

L if you have any questions or suggestions about this document, go to the official blog

Http://www.cnblogs.com/guoshiandroid/ (with the following contact information), we will carefully refer to your suggestions and modify this document as needed to benefit more developers!

L The latest and complete content of "big talk design mode" will be regularly updated on the official blog of guoshi studio. Please visit the blog of guoshi studio.

Http://www.cnblogs.com/guoshiandroid/get more updates.

Guoshi studio is a technical team dedicated to enterprise-level application development on the Android platform. It is committed to the best Android Application in China.ProgramDevelopment institutions provide the best Android enterprise application development training services.

Official contact information for Enterprise Training and Development Cooperation:

Tel: 18610086859

Email: hiheartfirst@gmail.com

QQ: 1740415547

QQ: 148325348

Guoshi studio is better for you!

 

  Query

See other sections:Overall description and Chapter index of this tutorial

PDF download link

Principle of merging and aggregation Reuse Liu BangVSHan Xin 

Use Cases:

VS

Once, Liu Bang was idle and remembered Han Xin, the old man. Then he opened QQ and wanted to chat with Han Xin. As a coincidence, Han Xin was also there. When he began to say a few cold words, he entered a serious problem unconsciously, liu Bang asked: "What do I think I can bring at most if I bring troops? ", Han Xin immediately replied: "100,000", and Liu Bang thought that this king could only bring 100,000, so how much can you do with Han Xin? So he pressed down his worries and asked politely: "How much can Han bring at most?" Han Xin immediately replied: "I am, the more it is, the better it will be." Liu was furious when he saw this remark, in addition, the tone is extremely arrogant, and "Ah, la", Liu Bang thought: "Although you believe that Han Xin has a good way to take troops to fight, it is well known that, however, this is too much for the king to speak. "Liu Bang was about to throw a sigh of relief and immediately stopped. Liu Bang, a well-versed man in the world, asked:" The general is brave enough to take millions of troops, but why is it under my leadership? ", Liu Bang thought, "Well, you have a Korean letter. If you say a few times, Brother Han doesn't know who you are. If you cannot answer the question, or you cannot answer well, it depends on how I clean you up! ", After waiting for about three seconds, QQ flashed, And suddenly said, "Although Your Majesty is not good at offering troops, it is good for you ". Liu bangjoy!

Definition: 

Composite aggregate Reuse Principle (CARP) is often referred to as Composite Reuse Principle (CRP ). the principle of merging aggregate reuse refers to the use of existing objects in a new object, which is part of the existing objects called new objects, new objects reuse existing functions by assigning corresponding actions or commands to these existing objects.

The merging Reuse Principle and concise expression are as follows: Use synthesis and aggregation as much as possible, and do not use inheritance as much as possible.

Aggregation (aggregation) is a type of association, used to represent a whole and a part of the ownership relationship. A reference to a part can be used to call some accessible methods and attributes. Of course, such access is usually to access interfaces and abstract classes. As a part, it can be referenced by multiple new objects at the same time and provide services for multiple new objects.

Composition is also a type of association, but synthesis is a much stronger association than aggregation. In the compositing relationship, the partial and overall lifecycle are the same. The new object as a whole has full control over the part, including the creation and destruction of the responsible and controlling part, that is, the part is responsible for memory allocation and memory release. It can also be seen from this that the member objects in a compositing relationship cannot be shared by another compositing relationship.

Why "we should try to use synthesis and aggregation instead of inheritance? This is because: first, inheritance reuse destroys packaging. It exposes the implementation details of the superclass to the subclass directly, which violates the principle of information hiding. Second, if the superclass changes, the subclass must also be changed, which directly leads to high coupling between classes, which is not conducive to class extension, reuse, maintenance, etc, it also brings about a rigid and fragile system design. Instead, when merging and aggregation is used, the interaction between new objects and existing objects is usually performed through interfaces or abstract classes, which can effectively avoid the above shortcomings, in addition, this allows each new class to focus on its own tasks and conform to the single responsibility principle.

The aggregation relationship is as follows:

The aggregation relationship is as follows:

Story analysis: 

"Han Xin takes troops and is more beneficial." Han Xin has a great influence. On the one hand, he has built great honors, and on the other hand, he has a heavy attach. Building great honors and even high honors directly leads Han Xin to win the hearts of the people in the army. After all, as soldiers, few do not want to follow an invincible leader on the battlefield; in terms of holding heavy soldiers, the principle of merging and aggregating is to reference the secret soldiers in charge of their own command, that is, aggregating. Han Xin's hand gathered a large number of people who could fight well and listened to Han Xin's dispatch at any time, which had to be defended by Liu Bang. But now, if I talk about how much we can do, how can I not make Liu Bang feel angry?

In general, Mo Fei Wang Tu; in the four seas, Mo Fei Wang Chen ", Han Xin owns soldiers, but Liu Bang owns the world, not to mention Han Xin does not have all soldiers. "Jun called Chen died, Chen had to die", Liu has the power to kill the world. Han believes it is clear that "Your Majesty is not good at offering troops, but is good at understanding ". Liu Bang has a stronger "ownership" relationship than Han Xin, which can dominate both life and death, and at the same time control Han Xin's life and death. This is equivalent to a compositing relationship. More importantly, Han Xin is also a member of the synthetic relationship and is the general of Liu Bang. So Liu Bang is more powerful than Han Xin!

As shown in:

 

JavaCodeImplementation: 

New minister's interface:

PackageCom. diermeng. designpattern. carp;

/*

* Minister's Interface

*/

Public InterfaceMinister {

/*

* Actions that the Minister can perform

*/

Public VoidDuty ();

}

Soldier Interface

PackageCom. diermeng. designpattern. carp;

/*

* Soldier Interface

*/

Public InterfaceSoldier {

/*

* Actions that soldiers can perform

*/

Public VoidDuty ();

}

 

 

Han Xin's implementation of Minister's Interface

PackageCom. diermeng. designpattern. Carp. impl;

 

ImportCom. diermeng. designpattern. Carp. Minister;

ImportCom. diermeng. designpattern. Carp. soldier;

/*

* Han Xin's implementation of the Minister's Interface

*/

Public ClassHanxinImplementsMinister {

// Aggregation relationship of soldiers

Soldier [] soldiers;

 

/*

* Construction method without Parameters

*/

PublicHanxin (){}

/*

* Constructor with soldier Parameters

*/

PublicHanxin (Soldier [] soldiers ){

Super();

This. Soldiers = soldiers;

}

 

/*

* Obtain a set of soldiers

*/

PublicSoldier [] getsoldiers (){

ReturnSoldiers;

}

 

/*

* Set a set of soldiers

*/

Public VoidSetsoldiers (Soldier [] soldiers ){

This. Soldiers = soldiers;

}

/*

* Han Xin's functions

* @ See COM. diermeng. designpattern. Carp. Minister # duty ()

*/

Public VoidDuty (){

System.Out. Println ("I am Liu Bang's minister, always loyal to Liu Bang ");

 

}

 

}

 

Implementation of soldier a's interface to soldiers

package COM. diermeng. designpattern. carp. impl;

Import COM. diermeng. designpattern. carp. soldier;

/*

* soldier a

*/

Public class soldiera implements soldier {

/*

* duties of soldier a

* @ see COM. diermeng. designpattern. carp. soldier # duty ()

*/

Public void duty () {

system. out . println ("I am Han Xin's soldier ");

}

}

Implementation of Soldier B's interface to soldiers

package COM. diermeng. designpattern. carp. impl;

Import COM. diermeng. designpattern. carp. soldier;

/*

* Soldier B

*/

Public class soldierb implements soldier {

/*

* duties of Soldier B

* @ see COM. diermeng. designpattern. carp. soldier # duty ()

*/

Public void duty () {

system. out . println ("I am Han Xin's Soldier B ");

}

}

Liu Bang

PackageCom. diermeng. designpattern. Carp. impl;

 

ImportCom. diermeng. designpattern. Carp. Minister;

 

/*

* Liu Bang

*/

Public ClassLiubang {

// Has a minister

Minister [] Minister;

 

/*

* Construction method without Parameters

*/

PublicLiubang (){}

/*

* Input the Minister's array as a parameter to the constructor

*/

PublicLiubang (Minister [] Minister ){

Super();

This. Minister = Minister;

}

 

/*

* Obtain a set of Ministers

*/

PublicMinister [] getminister (){

ReturnMinister;

}

 

/*

* Set a set of Ministers

*/

Public VoidSetminister (Minister [] Minister ){

This. Minister = Minister;

}

 

/*

* Functions of Liu Bang

*/

Public VoidDuty ()

{

System.Out. Println ("I am an emperor, under heaven, Mo Fei Wang Tu; within four seas, Mo Fei Wang Chen ");

}

}

Create a test class with the following code:

PackageCom. diermeng. designpattern. Carp. client;

 

ImportCom. diermeng. designpattern. Carp. Minister;

ImportCom. diermeng. designpattern. Carp. soldier;

ImportCom. diermeng. designpattern. Carp. impl. Hanxin;

ImportCom. diermeng. designpattern. Carp. impl. liubang;

ImportCom. diermeng. designpattern. Carp. impl. soldiera;

ImportCom. diermeng. designpattern. Carp. impl. soldierb;

 

/*

* Test Client

*/

Public ClassCarpclient {

Public Static VoidMain (string [] ARGs)

{

// Declare and instantiate soldier

Soldier soldiera =NewSoldiera ();

// Declare and instantiate Soldier B

Soldier soldierb =NewSoldierb ();

// Construct a soldier Array

Soldier [] soldiers = {soldiera, soldierb };

 

// Declare and instantiate Han Xin, and input the soldier Array

Minister Hanxin =NewHanxin (soldiers );

 

// Construct an array of Ministers

Minister [] Minister = {Hanxin };

// Declare and instantiate Liu Bang, and input the Minister Array

Liubang =NewLiubang (Minister );

 

Liubang. Duty ();

 

// Cyclically output Minister

For(Minister aminister: liubang. getminister ()){

Aminister. Duty ();

}

 

}

}

The program running result is as follows:

I am the emperor, and under heaven, I am not king earth; within four seas, I am not king Chen

I am Liu Bang's minister, always loyal to Liu Bang

 

Existing applications: 

For object-oriented software systems, improving Software maintainability and reusability is always a core issue. The rational and adequate use of the principle of merging aggregate reuse is very helpful for building software systems that are maintainable, reusable, scalable, and flexible. The principle of synthetic aggregation reuse can be applied to almost any environment as a means to construct high-quality systems. I will not repeat the existing applications here.

Tip: 

Although the principle of merging aggregate reuse can be applied to almost any environment, this principle also has its own shortcomings. This principle encourages the use of existing classes and objects to build new class objects, which leads to many classes and objects in the system to be managed and maintained, this increases the complexity of the system.

At the same time, it is not to say that the principle of synthetic aggregation reuse is the best in any environment, if there is a clear relationship between the two classes under the premise of being in line with the classification, in addition, the base class can abstract the common attributes and methods of the subclass. At this time, the subclass can expand the base class by adding the attributes and methods of the parent class, in this case, using inheritance is a better choice.

Note: This document references and uses free and open images and content on the Internet and is released in a free and open manner, hoping to contribute to the mobile Internet and the smart phone age! This document can be reproduced at will, but cannot be used.Benefit.

Related Article

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.