Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Composite Pattern 
 

Individual objects as well as the composite objects can be represented with the Composite Design Pattern.

 

Composite Pattern

                         

Individual objects as well as the composite objects can be represented with the Composite Design Pattern. Composite pattern represents these objects with a tree structure. Suppose, Within a company, there is an employee hierarchy where a manager has its subordinates, Project Leader also have the subordinates, while the developer has no subordinates. 

Benefits : It is more flexible as compared to the static inheritance. It simplifies coding by implementing each feature in a class. It enhances the capability of an object as the new classes are created to add new features and make some changes. 

Usage: This design pattern is used when the responsibilities are needed to be added dynamically to the individual objects without affecting other objects. Where an object's responsibilities may vary from time to time. 

Now, lets try to solve the above problem technically. 

Develop a class "Employee" having the getters and setters for the attributes empname, empsal and emp subordinates.

Employee.java

class Employee {
String Empname;
double Empsalary;
Employee(String n, double s){
Empname = n;
Empsalary = s;
}
String getName() {
return Empname;
}
double getSalary() {
return Empsalary;
}
public String toString() {
return "Employee" + Empname;
}
}

For example, General Manager may have several employee and some of them are Managers, further these managers have several employees. To illustrate all these things, lets design a simple Manager class. 

Manager.java:

class Manager {
Manager manager;
Employee[] emply;
String dept;
Manager(Manager mgr,Employee[] e, String d ) {
this(e, d);
this.manager = mgr;
}

Manager(Employee[] e, String d) {
emply = e;
dept =d;
}
String getDept() {
return dept;
}
Manager getManager() {
return manager;
}
Employee[] getEmployee() {
return emply;
}
public String toString() {
return dept + " manager";
}
}

Here is the "Test" class that shows the information about the "Manager" class.

Test.java :

class Test {
public static void main(String[] args) {
Employee[] e1 = {new Employee("Zulfiqar"90),
new Employee("Amit"80)};
Manager m1 = new Manager(e1, "Accounting");

Employee[] e2 = {new Employee("Aquil"70),
new Employee("Ravi"60),
new Employee("Vinod"40)};

Manager m2 = new Manager(m1, e2, "Production");

System.out.println(m2);
Employee[] emp = m2.getEmployee();
if (emp != null)
for (int k = 0; k < emp.length; k++)
System.out.println(" "+emp[k]+" Salary: $"
emp[k].getSalary()); 

Manager m = m2.getManager();
System.out.println(" " + m);
if (m!= null) {
Employee[] emps = m.getEmployee();
if (emps != null)
for (int k = 0; k < emps.length; k++)
System.out.println(" " + emps[k]+" Salary: $"
emps[k].getSalary());


}
}
}

Here is the output of the program :

C:\> java Test
Production manager
Employee Zulfiqar Salary: $90.0
Employee Amit Salary: $90.0
Employee Aquil Salary: $80.0
Accounting manager
Employee Ravi Salary: $70.0
Employee Vinod Salary: $50.0

The above example concludes that the composite pattern allows us to create a tree like structure for both simple and complex objects.

                         

» View all related tutorials
Related Tags: c class conversion interface io interfaces client get version ip pattern cli int this tab ie cte work expect to

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.