Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8, Design Patterns Composite

Similar presentations


Presentation on theme: "Chapter 8, Design Patterns Composite"— Presentation transcript:

1 Chapter 8, Design Patterns Composite
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 8, Design Patterns Composite

2 A Pattern Taxonomy Pattern Behavioral Creational Structural Pattern
Composite Decorator Adapter Bridge Façade Proxy Iterator Command Observer Template Strategy Singleton Abstract Factory Builder Factory Prototype

3 Introducing the Composite Pattern
Models tree structures that represent part-whole hierarchies with arbitrary depth and width. The Composite Pattern lets client treat individual objects and compositions of these objects uniformly * Client Component Leaf Operation() Composite Operation() AddComponent RemoveComponent() GetChild() Children

4 What is common between these definitions?
Software System: Definition: A software system consists of subsystems which are either other subsystems or collection of classes Composite: Subsystem (A software system consists of subsystems which consists of subsystems , which consists of subsystems, which...) Leaf node: Class Software Lifecycle: Definition: The software lifecycle consists of a set of development activities which are either other actitivies or collection of tasks Composite: Activity (The software lifecycle consists of activities which consist of activities, which consist of activities, which....) Leaf node: Task

5 Modeling a Software System with a Composite Pattern
User Software System * Class Subsystem Children

6 Modeling the Software Lifecycle with a Composite Pattern
Manager Software Lifecycle * Task Activity Children

7 The Composite Patterns models dynamic aggregates
Fixed Structure: Car * * Battery Engine Doors Wheels Organization Chart (variable aggregate): * * * School * What you are seeing on this slide are what I would like to call patterns for analysis. A pattern is a recurring theme, something once you get used to see something this way, allows you to very fast understand a situation. It is well known, that if you show a set of chess positions of middle games tochess masters and non chess players, that chess masters are able to reconstruct these games without any effort. However, if you give them random chess configurations, chess masters are about as bad as non-chess players in reconstructing the boards. This tells you about the value of patterns. I would like you to learn about these aggregation patterns. In fact, I challenge you to see for your team and subsystem, if any of the analysis problems you face, can be cast in terms of one of the 3 patterns above. University Department Dynamic tree (recursive aggregate): Composite Pattern Dynamic tree (recursive aggregate): Program * * * Block Compound Simple Statement Statement

8 Graphic Applications also use Composite Patterns
The Graphic Class represents both primitives (Line, Circle) and their containers (Picture) Client Graphic Circle Draw() Picture Add(Graphic g) RemoveGraphic) GetChild(int) Children Line *

9 The Composite Design Pattern in Java
The AWT/Swing widgets form a tree-like structure The Composite Design Pattern is well suited for such a structure

10

11 A Hierarchy in a Company
This is clearly a tree structure where the “bosses” are internal nodes and the leaves are the workers An interesting problem to solve is how to structure the classes such that you can find the total salaries for an employee and all subordinate employees (if any)

12 The Abstract Employee public abstract class AbstractEmployee {
protected String name; protected long salary; protected Employee parent = null; protected boolean leaf = true; public abstract long getSalary(); public abstract String getName(); public abstract boolean add(Employee e) throws NoSuchElementException; public abstract void remove(Employee e) public abstract Enumeration subordinates(); public abstract Employee getChild(String s); public abstract long getSalaries(); public boolean isLeaf() { return leaf; } }

13 public class Employee extends AbstractEmployee {
public Employee(String _name, long _salary) { name = _name; salary = _salary; leaf = true; } // public Employee(Employee _parent, String _name, long _salary) { parent = _parent; public long getSalary() { return salary; public String getName() { return name; The Employee Class - 1

14 public boolean add(Employee e) throws NoSuchElementException {
throw new NoSuchElementException("No subordinates"); } // public void remove(Employee e) throws NoSuchElementException { public Enumeration subordinates () { Vector v = new Vector(); return v.elements (); public Employee getChild(String s) throws NoSuchElementException { throw new NoSuchElementException("No children"); public long getSalaries() { return salary; public Employee getParent() { return parent; The Employee Class - 2

15 The Boss Class - 1 public class Boss extends Employee {
Vector employees; public Boss(String _name, long _salary) { super(_name, _salary); leaf = false; employees = new Vector(); } // public Boss(Employee _parent, String _name, long _salary) { super(_parent, _name, _salary); public Boss(Employee emp) { //promotes an employee position to a Boss //and thus allows it to have employees super(emp.getName (), emp.getSalary());

16 The Boss Class - 2 employees.add(e); return true; }
public boolean add(Employee e) throws NoSuchElementException { employees.add(e); return true; } // public void remove(Employee e) throws NoSuchElementException { employees.removeElement(e); public Enumeration subordinates () { return employees.elements (); } public long getSalaries() { long sum = salary; for (int i = 0; i < employees.size(); i++) { sum += ((Employee)employees.elementAt(i)).getSalaries(); return sum;

17 public Employee getChild(String s) throws NoSuchElementException {
Employee newEmp = null; if (getName().equals(s)) return this; else { boolean found = false; Enumeration e = subordinates(); while (e.hasMoreElements() && (! found)) { newEmp = (Employee)e.nextElement(); found = newEmp.getName().equals(s); if (! found) { if (! newEmp.isLeaf ()) { newEmp = newEmp.getChild(s); } else newEmp = null; found =(newEmp != null); } if (found) return newEmp; else return null; The Boss Class - 3

18 The Relationships between Classes
What is the component? What is the composite? Which items are leaves?

19 Employee Tree - 1 import java.awt.*; import java.awt.event.*;
import java.util.*; import javax.swing.text.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; import javax.swing.tree.*; public class empTree extends JxFrame implements TreeSelectionListener { Employee prez, marketVP, prodVP; Employee salesMgr, advMgr; Employee prodMgr, shipMgr; JScrollPane sp; JPanel treePanel; JTree tree; DefaultMutableTreeNode troot; JLabel cost; public empTree() { super("Employee tree"); makeEmployees(); setGUI(); }

20 Employee Tree - 2 treePanel = new JPanel();
private void setGUI() { treePanel = new JPanel(); getContentPane().add(treePanel); treePanel.setLayout(new BorderLayout()); sp = new JScrollPane(); treePanel.add("Center", sp); treePanel.add("South", cost = new JLabel(" ")); treePanel.setBorder(new BevelBorder(BevelBorder.RAISED)); troot = new DefaultMutableTreeNode(prez.getName()); tree= new JTree(troot); tree.setBackground(Color.lightGray); loadTree(prez); sp.getViewport().add(tree); setSize(new Dimension(200, 300)); setVisible(true); } public void loadTree(Employee topDog) { DefaultMutableTreeNode troot; troot = new DefaultMutableTreeNode(topDog.getName()); treePanel.remove(tree); tree= new JTree(troot); tree.addTreeSelectionListener(this); sp.getViewport().add(tree); addNodes(troot, topDog); tree.expandRow(0); repaint();

21 private void addNodes(DefaultMutableTreeNode pnode, Employee emp) {
DefaultMutableTreeNode node; Enumeration e = emp.subordinates(); if (e != null) { while (e.hasMoreElements()) { Employee newEmp = (Employee)e.nextElement(); node = new DefaultMutableTreeNode(newEmp.getName()); pnode.add(node); addNodes(node, newEmp); } } } private void makeEmployees() { prez = new Boss("CEO", ); prez.add(marketVP=new Boss("Marketing VP", )); prez.add(prodVP=new Boss("Production VP", )); marketVP.add(salesMgr=new Boss("Sales Mgr", 50000)); marketVP.add(advMgr=new Boss("Advt Mgr", 50000)); //add salesmen reporting to sales manager for (int i=0; i<5; i++) salesMgr .add(new Employee("Sales "+ i, rand_sal(30000))); advMgr.add(new Employee("Secy", 20000)); prodVP.add(prodMgr = new Boss("Prod Mgr", 40000)); prodVP.add(shipMgr = new Boss("Ship Mgr", 35000)); Employee Tree - 3

22 Employee Tree - 4 for (int i = 0; i < 4; i++)
//add manufacturing staff for (int i = 0; i < 4; i++) prodMgr.add( new Employee("Manuf "+i, rand_sal(25000))); //add shipping clerks for (int i = 0; i < 3; i++) shipMgr.add( new Employee("ShipClrk "+i, rand_sal(20000))); } private long rand_sal(long salary) { return salary(long)(Math.random()-0.5)*salary/5; public void valueChanged(TreeSelectionEvent evt) { TreePath path = evt.getPath(); String selectedTerm = path.getLastPathComponent().toString(); Employee emp = prez.getChild(selectedTerm); if (emp != null) cost.setText(new Float(emp.getSalaries()).toString()); static public void main(String argv[]) { new empTree();


Download ppt "Chapter 8, Design Patterns Composite"

Similar presentations


Ads by Google