AlexLecture_CompositePattern

AlexLecture_CompositePattern - Exercise Composite Pattern...

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

View Full Document Right Arrow Icon
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.
Image of page 1

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

View Full Document Right Arrow Icon
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) {}
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern