Java design pattern: combined mode

Source: Internet
Author: User
Tags stub

Composition (overall vs. partial relationship) mode: combines different but related objects into a tree structure to achieve a "partial-overall" hierarchy, which makes the user consistent with the use of individual objects and composite objects.
* Pattern Role Composition:
* 1.Component object: Is an object interface in a composition, which is a common interface owned by all classes. is used to uniformly define the parts of the population.
2.Leaf object: The part of the population, not the next level.
3.Composite object: Used to store child parts. The component interface is implemented in part-related operations.


Take the company composition as an example, the company has a head office. Head Office under the Branch, branch office has offices and so on. form a tree structure.

/** * Abstract class: Used to manage sub-classes * @description: * @date 2016-1-22 pm 1:16:25 */ Public Abstract  class Firm {    PrivateString Firmname;//Company name     Public Firm(String firmname) { This. firmname = Firmname; } PublicStringGetfirmname() {returnFirmname; } Public void Setfirmname(String name) { This. firmname = name; }protected Abstract void Append(Firm Firm);//Join a company such as: New branch    protected Abstract void Romove(Firm Firm);//Delete company: Close Branch if for any reason    protected Abstract void Show(intlevel);//Show the level of the company: Head Office, branch office, offices, etc.}
/** * Office Object * @description: * @date 2016-1-22 pm 1:25:43 * * Public  class branchfirm extends Firm {    PrivateList<firm> cList; Public branchfirm(String name) {Super(name); CList =NewArraylist<firm> (); }@Override    protected void Append(Firm Firm)    {Clist.add (firm); }@Override    protected void Show(intDepth) {StringBuilder SB =NewStringBuilder (""); for(inti =0; I < depth; i++) {Sb.append ("*"); } System.out.println (NewString (SB) + This. Getfirmname ()); for(Firm c:clist) {C.show (depth +2); }    }@Override    protected void Romove(Firm Firm) {//TODO auto-generated method stubClist.remove (firm); }}
/** * Leaf node Category: Company Marketing department * @description: * @date 2016-1-22 pm 1:46:02 * * Public  class marketdepartment extends Firm {     Public marketdepartment(String name) {Super(name); }@Override    protected void Append(Firm Company) {    }@Override    protected void Show(intDepth) {//TODO auto-generated method stubStringBuilder SB =NewStringBuilder (""); for(inti =0; I < depth; i++) {Sb.append ("*"); } System.out.println (NewString (SB) + This. Getfirmname ()); }@Override    protected void Romove(Firm Company) {    }}
/** * Leaf node Category: Company Technical Department * @description: * @date 2016-1-22 pm 1:21:41 * * Public  class mintechdepartment extends Firm {     Public mintechdepartment(String name) {Super(name); }@Override    protected void Append(Firm Firm) {    }@Override    protected void Show(intDepth) {StringBuilder SB =NewStringBuilder (""); for(inti =0; I < depth; i++) {Sb.append ("*"); } System.out.println (NewString (SB) + This. Getfirmname ()); }@Override    protected void Romove(Firm Company) {    }}
 Public classTest { Public Static void Main(string[] args) {//TODO auto-generated method stubFirm head =NewBranchfirm ("Shenzhen XX Group Co., Ltd.");//Company headquartersHead.append (NewMarketdepartment ("Shenzhen XX Group Co., Ltd.--marketing department")); Head.append (NewMintechdepartment ("Shenzhen XX Group Co., LTD.--Technical department")); Firm jsfirm =NewBranchfirm ("Shenzhen XX Group Co., Ltd.-Jiangsu branch"); Jsfirm.append (NewMarketdepartment ("Shenzhen XX Group Co., Ltd.-Jiangsu Branch marketing Department")); Jsfirm.append (NewMintechdepartment ("Shenzhen XX Group Co., Ltd.-Jiangsu Branch Technical Department")); Firm szfirm =NewBranchfirm ("Shenzhen XX Group Co., Ltd.-Jiangsu branch-Suzhou Office"); Szfirm.append (NewMintechdepartment ("Shenzhen XX Group Co., Ltd.-Jiangsu Branch-Suzhou Office Technical Department")); Szfirm.append (NewMarketdepartment ("Shenzhen XX Group Co., Ltd.-Jiangsu branch-Suzhou Office Marketing Department")); Jsfirm.append (szfirm);//Assuming additional branch or office is required. Just follow the 第19-25 line operation.Head.append (jsfirm); Head.show (0); }} usually: Use the combined pattern in a unified way, using the overall department in general, when you need to represent the overall and partial hierarchies of an object, or if you need to ignore a combination of the overall object and part of the object.

Java design pattern: combined mode

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.