K. Stirewalt CSE 335: Software Design Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures Topics: –Elaboration of.

Slides:



Advertisements
Similar presentations
K. Stirewalt CSE 335: Software Design Synthetic OOD concepts and reuse Lecture 4: Separation of concerns Topics: –Complex concern: Memory management –
Advertisements

Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance Inheritance Reserved word protected Reserved word super
Object-Oriented PHP (1)
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Software Testing and Quality Assurance
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
K. Stirewalt CSE 335: Software Design Synthetic OO Design Concepts & Reuse Lecture 1: Program fragments and abstract classes Topics: –Economics of software.
7M701 1 Class Diagram advanced concepts. 7M701 2 Characteristics of Object Oriented Design (OOD) objectData and operations (functions) are combined 
K. Stirewalt CSE 335: Software Design Foundations: Language Mechanisms and Primitive OO Concepts Lecture 2: Polymorphism Topics: – Polymorphism and virtual.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
K. Stirewalt CSE 335: Software Design Outline of course topics Foundational OO concepts Synthetic concepts: –Program families, program fragments, and abstract.
K. Stirewalt CSE 335: Software Design Synthetic OO Design Concepts & Reuse Lecture 5: Mixins and reusable strategies Topics: – More on the use of mixins.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
PRJ566: PROJECT PLANNING AND MANAGEMENT Class Diagrams.
UML Class Diagrams: Basic Concepts. Objects –The purpose of class modeling is to describe objects. –An object is a concept, abstraction or thing that.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
1 Dept. of Computer Science & Engineering, York University, Toronto Software Development CSE3311 Composite Pattern.
Association & Aggregation Lecture-9. Vehicle Car Tyre Engine Bus Passenger.
Object Oriented Software Development
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.
Programming Languages and Paradigms Object-Oriented Programming.
Design Patterns OOD. Course topics Design Principles UML –Class Diagrams –Sequence Diagrams Design Patterns C#,.NET (all the course examples) Design Principles.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
1 Chapter 2 (Cont.) The BA’s Perspective on Object Orientation.
SE2811 Week 7, Class 1 Composite Pattern Applications Conceptual form Class structure Coding Example Lab Thursday: Quiz SE-2811 Slide design: Dr. Mark.
C++ Review Classes and Object Oriented Programming Parasol Lab, Texas A&M University.
Programming in Java Unit 3. Learning outcome:  LO2:Be able to design Java solutions  LO3:Be able to implement Java solutions Assessment criteria: 
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
K. Stirewalt CSE 335: Software Design Synthetic OO Design Concepts & Reuse Lecture 1: Program fragments and abstract classes Topics: –Economics of software.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
K. Stirewalt CSE 335: Software Design Synthetic OO Design Concepts & Reuse Lecture 8: Modeling & documenting collaborations Topics: –Synthesis of multiple.
Object Oriented Programming
Foundations: Language mechanisms and primitive OO concepts Lecture 1: Classification and Inheritance Michigan State University Spring 2008 E. Kraemer Notes.
Object-Oriented Programming © 2013 Goodrich, Tamassia, Goldwasser1Object-Oriented Programming.
K. Stirewalt CSE 335: Software Design Synthetic OO Design Concepts & Reuse Lecture 5: Mixins and reusable strategies Topics: – More on the use of mixins.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Coming up: Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Chapter 16 UML Class Diagrams 1CS6359 Fall 2012 John Cole.
1 CSC241: Object Oriented Programming Lecture No 17.
OBJECT ORIENTED PROGRAMMING. Design principles for organizing code into user-defined types Principles include: Encapsulation Inheritance Polymorphism.
Object-Oriented Software Engineering Practical Software Development using UML and Java Modelling with Classes.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
Testing in OO Environment The reasons for testing is not any different for any of the design and implementation methodologies, including OO methodology.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
K. Stirewalt CSE 335: Software Design Synthetic OO Design Concepts & Reuse Lecture 1: Program fragments and abstract classes Topics: –Economics of software.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
Computer Science 313 – Advanced Programming Topics.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Composite Pattern Himanshu Gupta Shashank Hegde CSE776 – Design Patterns Fall 2011 Good composition is like a suspension bridge - each line adds strength.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Programming Concepts
More Design Patterns 1.
More Design Patterns 1.
UML Class Diagrams: Basic Concepts
Software Engineering Lecture #11.
Lecture 22 Inheritance Richard Gesick.
Object-Oriented Programming
Object-Oriented Programming
CISC/CMPE320 - Prof. McLeod
Object Oriented System Design Class Diagrams
Software Design Lecture : 36.
Chapter 11: Class Diagram
Computer Science II for Majors
Presentation transcript:

K. Stirewalt CSE 335: Software Design Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures Topics: –Elaboration of the expression class hierarchy –New modeling concepts: Aggregation and composition associations [Blaha and Rumbaugh § 4.4] –Composite object structures –The Composite pattern [Gamma]

K. Stirewalt CSE 335: Software Design Recall: Economics of software development Problem: Financing development of software systems –Big software is notoriously expensive over long term –Many software projects run out of money before they ever yield a product What the manager can do: –Outsource development to cheaper programmers –“Buy vs. build” –Amortize costs over long term: Design for change –Create assets that have intrinsic value and that pay dividends: Reusable libraries

K. Stirewalt CSE 335: Software Design Uses of abstract classes Defining an abstract “placeholder” that can hold objects of various types –E.g., Shape –Useful for building composite object structures Factoring common code into an abstract concept Serving as a program fragment in the design of a family of programs Definition of role-classes for use in collaboration- based designs

K. Stirewalt CSE 335: Software Design Uses of abstract classes Defining an abstract “placeholder” that can hold objects of various types –E.g., Shape –Useful for building composite object structures Factoring common code into an abstract concept Serving as a program fragment in the design of a family of programs Definition of role-classes for use in collaboration- based designs

K. Stirewalt CSE 335: Software Design Recall: Collaborative Exercise Design classes for arithmetic expression trees. Each arithmetic operator class should provide operations for retrieving operand expressions. Define at least the following classes: –Variable –Literal –Negate –Add, Subtract, Multiply, Divide Hint: You will need to invent some abstract classes

K. Stirewalt CSE 335: Software Design Classes Expr and Literal class Expr { public: virtual ~Expr() {} protected: Expr() {} }; Note: Constructor is not public! class Literal : public Expr { public: Literal( double d ) : _val(d) {} double value() const { return _val; } protected: double_val; };

K. Stirewalt CSE 335: Software Design Class BinaryExpr class BinaryExpr : public Expr { public: const Expr* getLeftOperand() const { return leftOperand; } const Expr* getRightOperand()const { return rightOperand; } protected: const Expr* leftOperand; const Expr* rightOperand; BinaryExpr( const Expr* l, const Expr* r ) : leftOperand( l ), rightOperand( r ) {} }; Note: Constructor is not public!

K. Stirewalt CSE 335: Software Design Example: Classes Add and Multiply class Add : public BinaryExpr { public: Add( Expr* l, Expr* r ) : BinaryExpr(l,r) {} }; class Multiply : public BinaryExpr { public: Multiply( Expr* l, Expr* r ) : BinaryExpr(l,r) {} };

K. Stirewalt CSE 335: Software Design Exercise int main(void) { Variable* x = new Variable( “ x ” ); Variable* y = new Variable( “ y ” ); Literal* l1 = new Literal(5.0); Literal* l2 = new Literal(3.9); Expr* e = new Add(l1, new Multiply(x,l2)); … Expr* e2 = new Divide(e, new Subtract(y,x)); … } Draw a UML object diagram depicting expressions e and e2

K. Stirewalt CSE 335: Software Design Exercise Draw a UML class diagram that illustrates the composite nature of the Expr class hierarchy.

K. Stirewalt CSE 335: Software Design First attempt at model of the Expr class hierarchy Expr BinaryExpr AddDivideMultiply Subtract Variable name : string Literal val : double UnaryExpr Negate child left right * * *

K. Stirewalt CSE 335: Software Design First attempt at model of the Expr class hierarchy Expr BinaryExpr AddDivideMultiply Subtract Variable name : string Literal val : double UnaryExpr Negate child left right * * * Question: Can you spot any problems with this model?

K. Stirewalt CSE 335: Software Design Consider the following object models : Add : Literal val = 5.0 : Add : Subtract left right left

K. Stirewalt CSE 335: Software Design DocumentParagraphSentence 1..* New concept: Aggregation Special kind of association with additional semantics –Minimally: Transitive and anti-symmetric A.k.a., the is-part-of or part-whole association –A sentence is part of a paragraph (a paragraph comprises sentences) –A paragraph is part of a document (a document comprises paragraphs) Two forms: –strong aggregation (a.k.a. composition) –weak aggregation

K. Stirewalt CSE 335: Software Design Terminology: Parts and assemblies Instances of classes on the “diamond end” of an aggregation are called assemblies; instances of classes on the non-diamond end are called the parts of an assembly. E.g.: –aggAB: instances of A are the assemblies, instances of B are the parts –aggBC: instances of B are the assemblies, instances of C are the parts –Parts that are also assemblies give rise to deep aggregation hierarchies A B C aggAB aggBC

K. Stirewalt CSE 335: Software Design Car Engine Deep hierarchies: Part-explosion diagrams GearboxBodyWheel DoorHoodTrunk 1..* 4

K. Stirewalt CSE 335: Software Design Aggregation Versus Association Aggregation is a special kind of an association: Use aggregation when: –association might naturally be called “is-part-of” or “is- made-of” –some operations on the assembly object automatically apply to the part objects E.g., deletion, moving, recomputing, etc –some values propagate from the assembly to all or some of the parts –there is an intrinsic asymmetry to the association i.e., one class is subordinate to the other

K. Stirewalt CSE 335: Software Design Better model of the Expr hierarchy Expr BinaryExpr AddDivideMultiply Subtract Variable name : string Literal val : double UnaryExpr Negate child left right * * *

K. Stirewalt CSE 335: Software Design Aggregation vs. composition Composition is a strong form of aggregation with additional semantic constraints: –A part can belong to at most one assembly –Once a part has been assigned to an assembly, its lifetime is coincident with that of the assembly Composition implies ownership of parts by assembly –Deletion of assembly object triggers deletion of component objects

K. Stirewalt CSE 335: Software Design Examples of Aggregations Vehicle VehiclePart 1..* Polygon LineSegment Vehicle comprises the parts; Parts may be “parts of” vehicles Where assembly appears, its parts also appear. Destroying the assembly means deleting its parts 3..* 0..1 *

K. Stirewalt CSE 335: Software Design Example with both compositions and associations CompanyDivisionDepartment Person employs * * **

K. Stirewalt CSE 335: Software Design Composite object structures Expression trees are examples of composite object structures –General notion of an expression, which may be a: simple object (e.g., an instance of a Variable or Literal) or composite object (e.g., instance of an Add, Subtract, Multiply, or Divide) Composite structures show up frequently in real object-oriented designs Composite designs especially useful when the abstract class declares polymorphic operations

K. Stirewalt CSE 335: Software Design Exercise Extend Expr hierarchy with polymorphic operation: void print( ostream& ) It should be possible to execute code such as: Expr* l = new Literal(5.0); Expr* v = new Variable( “ x ” ); Expr* e = new Add( l, v ); e->print(cout); l->print(cout); v->print(cout);

K. Stirewalt CSE 335: Software Design Composite pattern (idealized) ClientComponent Operation() Composite Operation() Add(Component) Remove(Component) GetChild(int) : Component Leaf Operation() children * *

K. Stirewalt CSE 335: Software Design Composite pattern (most general) ClientComponent Operation() Add(Component) Remove(Component) GetChild(int) : Component Composite Operation() Add(Component) Remove(Component) GetChild(int) : Component Leaf Operation() children * *

K. Stirewalt CSE 335: Software Design Design virtues of composites Makes client code simple –Clients treat composite structures and individual objects uniformly –Design principle: Abstraction through use of the abstract class Component and the identification of abstract operations that apply to many types of objects Makes it easy to add new kinds of components without modifying any client code –Design principle: Incrementality –Design principle: Anticipation of change

K. Stirewalt CSE 335: Software Design Question Can you come up with another example use of the Composite pattern?