CSCI 1260 – Lecture 2: Instantiation, Aggregation, and Composition

Slides:



Advertisements
Similar presentations
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
Lecturer: Sebastian Coope Ashton Building, Room G.18 COMP 201 web-page: Lecture.
CS-2135 Object Oriented Programming
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,
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
1 SWE Introduction to Software Engineering Lecture 23 – Architectural Design (Chapter 13)
What is UML? A modeling language standardized by the OMG (Object Management Group), and widely used in OO analysis and design A modeling language is a.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Object-Oriented Analysis and Design
Object Oriented Design and UML
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
Programming in Java Unit 3. Learning outcome:  LO2:Be able to design Java solutions  LO3:Be able to implement Java solutions Assessment criteria: 
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance Interfaces Reading for this Lecture: L&L 6.4 – 6.5.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
PROG Object Oriented Programming II With Java PROG Object Oriented Programming II With Java Class Relationships.
Object Oriented Analysis and Design Class and Object Diagrams.
CSCI-383 Object-Oriented Programming & Design Lecture 10.
Chapter 3 Class Diagrams. 2 Outline Class Basics Class Basics Classes Classes Association Association Multiplicity Multiplicity Inheritance Inheritance.
UML Class Diagram notation Indicating relationships between classes SE-2030 Dr. Mark L. Hornick 1.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
CSCE 240 – Intro to Software Engineering Lecture 3.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
2-1 © Prentice Hall, 2004 Chapter 2: Introduction to Object Orientation Object-Oriented Systems Analysis and Design Joey F. George, Dinesh Batra, Joseph.
Chapter 9 A Second Look at Classes and Objects - 3 Aggregation.
Auburn University COMP 2710 Software Construction Use Case Analysis – Examples and Exercises Dr. Xiao Qin Auburn University.
Copyright © Jim Fawcett Spring 2017
More About Objects and Methods
Object-Orientated Analysis, Design and Programming
Chapter 5: Structural Modeling
Object-Oriented Programming: Inheritance
Object-Oriented Analysis and Design
Java Programming: Guided Learning with Early Objects
The Object-Oriented Thought Process Chapter 1
Chapter 11 Object-Oriented Design
Class diagram Description
Object Oriented Programming using Java - Class Instance Variables
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
Object Oriented Concepts -I
Week 4 Object-Oriented Programming (1): Inheritance
CompSci 230 Software Construction
UML Diagramming with Astah*
Inheritance B.Ramamurthy 11/7/2018 B.Ramamurthy.
Inheritance November 10, 2006 ComS 207: Programming I (in Java)
Chapter 3 Introduction to Classes, Objects Methods and Strings
Defining Your Own Classes Part 1
Corresponds with Chapter 7
Chapter 10 Thinking in Objects
Software Engineering Lecture #11.
Lecture 22 Inheritance Richard Gesick.
Object-Oriented Programming Using C++ Second Edition
Defining Classes and Methods
Basics of OOP A class is the blueprint of an object.
Object Oriented Programming Review
Object Oriented Programming in java
Defining Classes and Methods
Defining Classes and Methods
Chapter 9 Introduction To Classes
Information System Design
UML  UML stands for Unified Modeling Language. It is a standard which is mainly used for creating object- oriented, meaningful documentation models for.
Creating and Using Classes
SPL – PS3 C++ Classes.
Presentation transcript:

CSCI 1260 – Lecture 2: Instantiation, Aggregation, and Composition Class Relationships November 23, 2018 Class Relationships

Learning Objectives Lecture Objectives: At the end of the lecture you should be able to: Draw/Produce UML diagrams (instantiation relationship) Implement and use the instantiation relationship Produce UML diagrams (aggregate relationship) Produce UML diagrams (composition relationship) Implement and use the aggregation relationship Implement and use the composition relationship November 23, 2018 Class Relationships

Instantiation Relationship The USES relationship November 23, 2018 Class Relationships

Instantiation Relationship In the instantiation relationship, one class uses the services provided by another class. Its objects instantiate and use objects of another class or classes. An Employee object may use an object of the Name class to represent the person’s name and it may use an object of the Address class to manage the person’s address. An Automobile object may use objects of the Engine, Transmission, Steering, SoundSystem, and other classes. The user needs to know about the objects it is using, but the objects being used do not need to know about their user. A Carpenter needs to know her Hammer but the Hammer doesn’t care who using it. A Car needs to know about its Engine, but the Engine doesn’t care whether the Car is blue or white. This promotes enhances separation of concerns, encapsulation, information hiding, and other object-oriented goals. November 23, 2018 Class Relationships

Showing Instantiation in UML In the diagram, objects of the class Person use the services of objects of the class Hammer One shows an Instantiation relationship in UML with a dotted arrow pointing from the user to the object in use November 23, 2018 Class Relationships

Implementing the Instantiation Relationship Typically, this relationship is implemented in Java code with a client method in the user class, say class A, instantiating an object of the other class, say class B, and then calling one or more methods of B on that object. November 23, 2018 Class Relationships

Sample Code Skeleton A method in class Carpenter instantiates an object of class Hammer and then uses it. Instantiation/Uses can also be implemented by passing an object of class Hammer to a method of class Carpenter and using it. November 23, 2018 Class Relationships

The “Has-a” Relationship Composition and Aggregation: the whole-part relationships November 23, 2018 Class Relationships

Aggregation and Composition The relationship between two classes may be a part-whole relationship The whole “has-a” part Consider: A room has a chair A house has a roof Both cases describe a part-whole relationship, where the room or the house is the whole and the chair or the roof is the part In Java, the whole is a class (or instance thereof) and the part is an attribute of the class November 23, 2018 Class Relationships

Aggregation and Composition The two examples on the previous slide depict situations in which there is a very subtle difference in the relationships The room has a chair but the chair is not an essential part of the room The chair and the room may exist without the other The chair may be removed from a room and placed in another – and both will continue to exist and function without the other This relationship is called the aggregate relationship (the chair and the room) On the other hand: The house has a roof The roof is an essential part of the house; the house is incomplete without the roof Neither the house nor the roof can exist without the other This relationship is called the composition relationship (the roof is an essential component of the house) November 23, 2018 Class Relationships

Composition and Aggregation In both relationships, the class representing the whole has an attribute representing the part In the composition relationship, the part and whole are created together In Java, the part is instantiated in the constructor of the whole The part and whole come into existence together and exist together until the whole ceases to exist (the house and its roof have the same lifetime) In an aggregation relationship, the part is not created with the whole (i.e., in its constructor) Some other method is provided to instantiate the part This method may or may not ever be invoked by the driver or some other class November 23, 2018 Class Relationships

Aggregation November 23, 2018 Class Relationships

Aggregation In the aggregate relationship, one class aggregates objects of the other class In the diagram, class Room has (or contains) zero or one objects of type Chair The class Room, acting as the whole, maintains a reference (or references) to an object(s) of the class Chair that act as the part There are usually one or more methods (that may or may not ever be invoked) that instantiate one or more instances of the part Reference name This type arrow indicates the aggregation relationship Between 0 and 1 instances of a Chair November 23, 2018 Class Relationships

Aggregation in UML In Jude/Astah*, the aggregation relationship is indicated as shown here: Use this dropdown … Select the Aggregation Symbol Tooltip helps one select the proper symbol … to get this list November 23, 2018 Class Relationships

Arrow is the result of dragging FROM the Room class TO the Chair class Aggregation in Jude Drag the selected arrow FROM the “whole” (Room) TO the “part” (Chair) The PART The WHOLE Arrow is the result of dragging FROM the Room class TO the Chair class November 23, 2018 Class Relationships

Aggregation: code skeleton The Room has an attribute reference to a Chair object Room instantiates a reference to an object of type Chair – initially null Some method in Room associates the Room object with an actual Chair with the reference named chair Note that the Room object exists (and is initialized by the Room constructor – not shown) and the Chair object that is passed to the setChair method exists separately. The Chair becomes a real part of this Room only when this method is invoked November 23, 2018 Class Relationships

Composition November 23, 2018 Class Relationships

Indicates a House object has exactly one Roof object, named roof Composition One class is partially or fully composed of objects of other classes In the diagram, class House has (or contains) one object roof of type Roof The class acting as the whole (House) maintains a reference (or references) to an object(s) of the class that is acting as the part (Roof) The difference between this and aggregation is that the part is instantiated when the whole is instantiated. The house and the roof come into existence together (in the constructor for the House) Indicates a House object has exactly one Roof object, named roof This type of arrow indicates that the roof is an integral component of the house November 23, 2018 Class Relationships

Drag it FROM House TO Roof … Composition in UML Select the Composition Arrow in Jude/Astah* and Drag FROM the whole (House) TO the part (Roof) Drag it FROM House TO Roof … Select this symbol … to get this November 23, 2018 Class Relationships

Composition In aggregation, the part is created in a method that may or may not ever be invoked, while here, the part is always created. Because the Roof/House cannot exist without the other, the House constructor creates its own Roof – and the Roof object exists as long as the House object does November 23, 2018 Class Relationships

Security Issues in Part-Whole Relationships Can something outside a class access a private member of the class without the class knowing it? November 23, 2018 Class Relationships

Security Issues? The textbook mentions a number of potential security issues with the aggregation/composition of classes In the aggregation/composition relationship, you should restrict access to the aggregated/composed object reference by only allowing changes through the object that represents the whole For example, we should not allow the size of the roof to change unless the entire house is changed (by adding, removing, or changing parts of it) The House may change its Roof, but we shouldn’t allow the Roof to change itself without the House’s knowledge or its control The easiest way to do this, is to make sure the House owns the only copy of the Roof (so it can’t be changed elsewhere) There is a security problem with the aggregation example code given earlier. What is it? November 23, 2018 Class Relationships

Changing the attributes of a reference changes all 3 Security Issues, cont. Remember that an Object of a Class is a Reference to the actual object in memory Multiple object references may refer to the same actual object in memory Changing “one object” through its reference changes the actual object in memory so the others are changed, too Object 1 Reference 1 Object 2 Reference 2 Object 3 Reference 3 Object in memory 3 instances refer to it Changing the attributes of a reference changes all 3 November 23, 2018 Class Relationships

Example This technique is called a Shallow Copy because the reference is duplicated, but not the object to which it refers November 23, 2018 Class Relationships

Example, continued This technique is called a Deep Copy because the object itself is duplicated – not just another reference to the same object November 23, 2018 Class Relationships

Security Problems - resolved The security problem noted several slides back can be resolved by ALWAYS using Deep Copy operations to assure that the only methods that can change a component object in a Composite class are the members of the Composite class itself See the example on pages 358-359 (that starts several pages earlier) of the textbook for the right and wrong ways to handle this issue November 23, 2018 Class Relationships

Correction to Aggregation Example The correct code for the aggregation example to avoid the security issue is One should almost always do this to prevent this type of security issue Room owns the clone of the parameter and only Room can change it. If the object that owns chairIn changes it, there will be no changes to the Room’s chair. November 23, 2018 Class Relationships

Copy Constructor In a class, it may be desirable to initialize a new object as a duplicate of an existing object (as shown in a previous example) This is done with a copy constructor A copy constructor is a constructor that takes one parameter – an existing object of the same class The copy constructor initializes the attributes of the new object by copying the values of the corresponding attributes of the parameter into them Example: Card myCard = new Card (yourCard); Initializes the new Card name myCard by copying all the attribute values from yourCard into the corresponding attributes in myCard November 23, 2018 Class Relationships

Example of Copy Constructor Deep copy : copies the entire object – not just copy a reference to it into another reference November 23, 2018 Class Relationships