Big talk Design Pattern _ combination pattern

Source: Internet
Author: User

Use a system as an example for branch offices under the company.

Package COM. WZS. design; import Java. util. arraylist; import Java. util. list;/*** big talk design mode -- page194 combination mode ** @ author administrator **/public class compositemode {public static void main (string [] ARGs) {concretecompany root = new concretecompany ("Beijing Head Office"); root. add (New hrdepartment ("Head Office Human Resources Department"); root. add (New financedepartment ("Head Office Finance Department"); concretecompany comp = new concretecompany ("Shanghai East China Branch"); comp. add (New hrdepartment ("Shanghai East China Branch Human Resources Department"); comp. add (New financedepartment ("Shanghai East China Branch Finance Department"); root. add (COMP); concretecompany comp1 = new concretecompany ("Nanjing Office"); comp1.add (New hrdepartment ("Nanjing Office Human Resources Department ")); comp1.add (New financedepartment ("Nanjing Office Finance Department"); comp. add (comp1); concretecompany comp2 = new concretecompany ("Hangzhou Office"); comp2.add (New hrdepartment ("Hangzhou Office Human Resources Department ")); comp2.add (New financedepartment ("Hangzhou Office Finance Department"); comp. add (comp2); system. out. println ("structure:"); root. display (1); system. out. println ("Responsibility:"); root. lineofduty () ;}}// COMPANY Abstract class company {public string name; Public Company (string name) {This. name = Name;} public abstract void add (Company); // Add public abstract void remove (Company); // remove public abstract void display (INT depth ); // display public abstract void lineofduty (); // perform duties} // The company class concretecompany extends company {private list <company> Children = new arraylist <company> (); public concretecompany (string name) {super (name) ;}@ overridepublic void add (Company) {children. add (company) ;}@ overridepublic void display (INT depth) {system. out. println (mystring. createstring ("-", depth) + "" + this. name); For (Company: Children) {company. display (depth + 2) ;}@overridepublic void lineofduty () {for (Company: Children) {company. lineofduty () ;}@overridepublic void remove (Company) {children. remove (company) ;}// HR department class hrdepartment extends company {public hrdepartment (string name) {super (name) ;}@ overridepublic void add (Company) {}@ overridepublic void display (INT depth) {system. out. println (mystring. createstring ("-", depth) + "" + this. name) ;}@ overridepublic void lineofduty () {system. out. println (this. name + "Employee Recruitment and Training Management. ") ;}@ overridepublic void remove (Company) {}} // Finance Department class financedepartment extends company {public financedepartment (string name) {super (name );} @ overridepublic void add (Company) {}@ overridepublic void display (INT depth) {system. out. println (mystring. createstring ("-", depth) + "" + this. name) ;}@ overridepublic void lineofduty () {system. out. println (this. name + "company financial revenue and expenditure management. ") ;}@ overridepublic void remove (Company) {}}/** create character prefix */class mystring {public static string createstring (string STR, int depth) {string strtemp = ""; for (INT I = 0; I <depth; I ++) {strtemp + = STR;} return strtemp ;}}

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.