AlexLecture_CompositePattern - 1 Composite Pattern 2...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Composite Pattern 2 Exercise ¡ How to design the classes for line, rectangle, text, picture? 3 Composite Pattern ¡ Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. This is called recursive composition . ¡ UML: ¡ In UML, ─ abstract class name is italic; ─ abstract operations are italic. 4 Where to apply composite pattern ¡ The objects have part-of relationships. ¡ There are common operations. You want clients to be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly. ¡ Example problems: ─ Graphics: line, rectangle, text, picture ● Part-of relationship: a picture may contain lines, rectangles, texts, and pictures. ● Common operations: draw() ─ Equipments: floppydisk, network card, chassis, cabinet ● Part-of relationship : a chassis may contain floppydisks, network cards, etc; and a cabinet may contain floppydisks, network cards, chassis, and cabinets. ● Common operations: power(), price() ─ Expressions: variables, literals, negate, binary expressions ● Note: a binary expression is composed by two expressions (add, substract, multiply, divide). ● Common operations: value() ─ Recursive composition. 2 5 Abstract operations ¡ A base class might declare an operation for which there is no reasonable default method. ─ Example: Class Shape Defines an area() operation But there is no general method for computing area of a shape ¡ Such an operation should be declared abstract ─ In UML, abstract operations are italic. ─ In C++, abstract operations are implemented as pure virtual functions. 6 Pure virtual function ¡ Definition: Mechanism by which a class can declare an operation without providing a method. ¡ Purpose: provide a uniform interface. ¡ Syntax: class BaseClass { public: virtual void pvf() =0; }; class DerivedClass : public BaseClass { public: void pvf() { … } }; 7 Example: Class Shape class Shape { public: virtual unsigned area() =0; }; Class Rectangle : public Shape { public: Rectangle( unsigned l, unsigned h) : length(l), height(h) {}...
View Full Document

This note was uploaded on 12/14/2009 for the course CSE 335 taught by Professor Kraemer during the Fall '08 term at Michigan State University.

Page1 / 6

AlexLecture_CompositePattern - 1 Composite Pattern 2...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online