Top Banner
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern
27

Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Dec 26, 2015

Download

Documents

Ursula Mathews
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Linzhang Wang

Dept. of Computer Sci&Tech,

Nanjing University

The Composit Pattern

Page 2: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Intent

Compose objects into tree structures to represent part-whole hierarchies.

Let clients treat individual objects and compositions of objects uniformly.

Page 3: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Motivation

Some system let user build complex diagrams out of simple components.

A simple implementation could define classes for each concept of the components. a problem: Code that uses these classes must treat

primitive and container objects differently.

Page 4: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Motivation(2)

The key of composite pattern is an abstract class that represents both primitives and their containers.

A typical composite structures:

aPicture

aPicture

aLine aRect

aText aLine aRect

Page 5: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Applicability

Use this pattern when You want to represent part-whole hierarchies of

objects. You want clients to be able to ignore the difference

between compositions of objects and individual objects. Client will treat all objects in the composite structure uniformly

Page 6: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Structure

Page 164

ComponentOperation()Add(Component)Remove(Component)GetChild(int)

LeafOperations()

CompositeOperation()Add(Component)Remove(Component)GetChild(int)

children

Client

Page 7: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Participants

Component declare the interface for objects in the composition. implements default behavior. declares an interface for accessing and managing

its child components. (opt.) defines an interface for accessing a

component’s parent in the recursive structure, …

Page 8: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Participants(2)

Leaf represents leaf objects in the composition. define behavior for primitive objects.

Composite defines behavior for components having children. stores child components. implements child-related operations in the

component interface. Client

manipulates objects in the composition through the Component interface.

Page 9: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Collaborations

Client use the Component class interface to interact with objects in the composite structure. If the recipient is a Leaf, then the request is handled

directly. If not, the it usually forwards requests to its child

components, possibly performing additional operations before and/or after forwarding.

Page 10: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Consequences

defines class hierarchies consisting of primitive objects and composite objects.

makes the client simple. makes it easier to add new kinds of

components. (disadvantage) can make your design overly

general.

Page 11: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Implementation issues(1)

Explicit parent references: the reference from child to components to their parent. Simplify the traversal and management of a

composite structure: moving up, delete. The references is usually defined in the

component class. You must maintain the invariant. Relative pattern: Chain of Responsibility

Page 12: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Implementation issues(2)

Sharing components It’s useful to share components to reduce storage

requirements. Difficult if a component can have no more than one

parent. Relative pattern: Flyweight

Page 13: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Implementation issues(3)

Maximizing the Component interface The Component class should define as many

common operations for Composite and Leaf classes as possible.

But sometime it conflicts with the principle of class hierarchy design that says a class should only define operations that are meaning ful to its subclasses.

A little creativity: treat the Leaf class as a special kind of Composite class.

Page 14: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Implementation issues(4.1)

Declaring the child management operations Declare it in Component or in Composite? The

decision involves a trade-off between safety and transparency.

Define the operations in Component gives you transparency, but cost safty.

Define the operations in Composite gives you safty, but you lose transparency.

Page 15: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Implementation issues(4.2)

We emphasize transparency over safety in this pattern.

An approach for safty without a type-unsafe cast: declare an operation Composite *GetComposite() in the class Component. The default operation return null. Each composite class override it and return this. You can query wether a component instance is a

composite object using GetComposite()

Page 16: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Implementation issues(4.3)

class Compositeclass Component {

public: virtual Composite * GetComposite(){return null}

}class Composite: public Component {

public: virtual Composite * GetComposite(){return this}}

if(test = aComponent->GetComposite()){test->Add(new Leaf);

}

Page 17: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Implementation issues(5)

Should Component implement a list of Components? May define the set of children as an instance

variable in the Component class where the child access and management operations are declared.

But incurs a space penalty for every leaf.

Page 18: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Implementation issues(6)

Child ordering Many designs specify an ordering on the children of

Composite. When child ordering is an issue, you must design

child access and management interfaces carefully. Related pattern: Iterator.

Page 19: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Implementation issues(7)

Caching to improve performance The Composite class can cache traversal or

search information about its children. The information about its sub-tree can be cached

in the parent object. To avoid traverse the sub-tree again and again.

A component will require invalidating the caches of its parents. So you need an interface for telling composites that their caches are invalid.

Page 20: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Implementation issues(8)

Who should delete components? In language without garbage collection, it’s best to

make a Composite responsible for deleting its children when it’s destroyed.

An exception: when Leaf objects are immutable.

Page 21: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Implementation issues(9)

What’s the best data structure for storing components? a variety of data structure can be used: liked lists,

trees, arrays, hash tables. The choice depends on efficiency. It isn’t necessary to use a general-purpose data

structure: a subclass of Composite can implement its own management interface.

Related pattern: Intrepreter.

Page 22: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Sample Code Equipment: computer and stereo components.class Equipment {

virtual ~Equipment();const char* Name() { return _name;}virtual Watt Power();virtual Currency NetPrice();virtual Currency DiscountPrice();virtual void Add(Equipment *);virtual void Remove(Equipment *);virtual Iterator<Equipment *>* CreaterIterator();

Protected:Equipment(const char *);

private:const char* _name;

}

Page 23: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Sample Code

Leaf class:class FloppyDisk : public Equipment{public:

FloppyDisk(const char*);virtual ~FloppyDisk();virtual Watt Power();virtual Currency NetPrice();Virtual Currency DiscountPrice();

}

Page 24: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Sample Code The base class for equip. containing other

equip.class CompositeEquipment: public Equipment{public:

virtual ~CompositeEquipment();virtual Watt Power();virtual Currency NetPrice();virtual Currency DiscountPrice();virtual void Add(Equipment *);virtual void Remove(Equipment *);virtual Iterator<Equipment *>*CreatIterator();

protected:CompositeEquipment(const char*);

private:List <Equipment *> _equipment;

}

Page 25: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Sample Code

Default Impl. of NetPriceCurrency CompositeEquipment::NetPrice(){

Iterator<Equipment *> *i=CreateIterator();Currency total = 0;for (i->First(); !i->IsDone(); i->Next() {

total += i->CurrentItem()->NetPrice();}

}

Page 26: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Sample Code

class for computer chassisclass Chassis: public CompositeEquipment{public:

Chassis(const char*);virtual ~Chassis();virtual Watt Power();virtual Currency NetPrice();virtual Currency DiscountPrice();

};

Page 27: Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Sample Code

Use the classesCabinet * cabinet = new Cabinet(“PC Cabinet”);

Chassis* chassis = new Chassis(“PC Chassis”);

Cabinet->Add(chassis);

Bus * bus = new Bus(“NCA Bus”);

bus->Add(new Card(“16Mbs Token Ring”));

Chassis->Add(bus);

Chassis->Add(new FloppyDisk(“3.5in Floppy”));

cout << ‘The net price is” <<Chassis->NetPrice() <<endl.