23 Design Patterns in Java 13-combined mode (composite pattern)

Source: Internet
Author: User

Import java.util.LinkedList;
Abstract class Company
{
private String name;
public void SetName (String name)
{
This.name=name;
}
Public String GetName ()
{
return name;
}
abstract void Add (company company);
abstract void Remove (company company);
abstract void disp (int depth);
}

Class Office extends Company
{
Public Office (String name)
{
SetName (name);
}
public void add
{
SYSTEM.OUT.PRINTLN ("Office can not add");
}
public void Remove
{
SYSTEM.OUT.PRINTLN ("Office can not remove");
}
public void disp (int depth)
{
StringBuilder sb = new StringBuilder ("");
for (int i = 0; i < depth; i++) {
Sb.append ("-");
}
System.out.println (New String (SB) + this.getname ());
SYSTEM.OUT.PRINTLN ("Office name is:" +name);
}
}

Class Concretecompany extends Company
{
Public Concretecompany (String name)
{
SetName (name);
}
Linkedlist<company> acompanylist=new linkedlist<company> ();
public void add
{
Acompanylist.add (company);
System.out.println ("Add Office:" +company.getname ());
}
public void Remove
{
Acompanylist.remove (company);
System.out.println ("Remove Office:" +company.getname ());
}
public void disp (int depth)
{
StringBuilder sb = new StringBuilder ("");
for (int i = 0; i < depth; i++) {
Sb.append ("-");
}
System.out.println (New String (SB) + this.getname ());
For (company Company:acompanylist)
{
Company.disp (depth + 2);
}
}
}


public class Compositepatterntest
{
public static void Main (string[] args)
{
Company Parentcompany=new Concretecompany ("Anhui head Office");

Company Suzhoubranchcompany=new Concretecompany ("Suzhou branch");
Company Bengbubranchcompany=new Concretecompany ("Bengbu branch");
Company Hefeioffice=new Office ("Hefei Office");
Parentcompany.add (Suzhoubranchcompany);
Parentcompany.add (Bengbubranchcompany);
Parentcompany.add (Hefeioffice);

Company Lingbibranchcompany=new Concretecompany ("Lingbi branch");
Company Sixianoffice=new Office ("Si County Office");
Company Huaiyuanoffice=new Office ("Huaiyuan Office");
Company Dangshanoffice=new Office ("Dangshan Office");
Suzhoubranchcompany.add (Lingbibranchcompany);
Suzhoubranchcompany.add (Sixianoffice);
Suzhoubranchcompany.add (Huaiyuanoffice);
Suzhoubranchcompany.add (Dangshanoffice);

Company Louzhuangoffice=new office ("Lou Zhuang Office");
Lingbibranchcompany.add (Louzhuangoffice);

Company Wuheoffice=new Office ("Wuhe Office");
Company Guzhenoffice=new office ("Gu Zhen Office");
Bengbubranchcompany.add (Wuheoffice);
Bengbubranchcompany.add (Guzhenoffice);

Parentcompany.disp (0);

}
}

23 Design Patterns in Java 13-combined mode (composite pattern)

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.