Association, Aggregation and Composition. Software Engineering CITS1220

Similar documents
CS111: PROGRAMMING LANGUAGE II

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Inheritance Abstraction Polymorphism

Object-Oriented Software Engineering Practical Software Development using UML and Java

ITI Introduction to Computing II

ITI Introduction to Computing II

OBJECT ORİENTATİON ENCAPSULATİON

Lecture 34 SDLC Phases and UML Diagrams

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

COMP 250. inheritance (cont.) interfaces abstract classes

Throughout the exam, write concisely and underline key words or phrases. Have fun! Exam 2. Week 7 (Winter 2013). Dr. Yoder. Sec 031.

Software Engineering Prof. Rushikesh K.Joshi IIT Bombay Lecture-15 Design Patterns

Inheritance STL. Entity Component Systems. Scene Graphs. Event Systems

OO System Models Static Views

Class diagrams. Modeling with UML Chapter 2, part 2. Class Diagrams: details. Class diagram for a simple watch

Outline. Threads. Single and Multithreaded Processes. Benefits of Threads. Eike Ritter 1. Modified: October 16, 2012

Global Gomoku Lab 4 in D0010E

Introduction to UML and Class Diagrams

Introduction to UML and Class Diagrams

Modelling with Classes. CITS1220 Software Engineering

Inheritance (Deitel chapter 9)

Inheritance. OOP: Inheritance 1

Programming, numerics and optimization

MechEng SE3 Lecture 7 Domain Modelling

Inheritance and Polymorphism

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Event-driven Programming: GUIs

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

Module 10 Inheritance, Virtual Functions, and Polymorphism

Introducing the UML Eng. Mohammed T. Abo Alroos

What is a Pattern? Lecture 40: Design Patterns. Elements of Design Patterns. What are design patterns?

Inheritance and Interfaces

Programmazione. Prof. Marco Bertini

CS 11 java track: lecture 3

ECE 3574: Dynamic Polymorphism using Inheritance

SEEM4570 System Design and Implementation. Lecture 11 From Design to Implementation

More on Design. CSCI 5828: Foundations of Software Engineering Lecture 23 Kenneth M. Anderson

DHANALAKSHMI SRINIVASAN COLLEGE OF ENGINEERING AND TECHNOLOGY ACADEMIC YEAR (ODD SEM)

Last Lecture. Lecture 26: Design Patterns (part 2) State. Goals of Lecture. Design Patterns

Inheritance. Software Engineering with Inheritance. CSC330 Object Oriented Programming. Base Classes and Derived Classes. Class Relationships I

1B1b. Classes in Java Part III. Review. Static. Static (2) Example. Static (3) 1B1b Lecture Slides. Copyright 2004, Graham Roberts 1

Objectives. INHERITANCE - Part 1. Using inheritance to promote software reusability. OOP Major Capabilities. When using Inheritance?

INHERITANCE - Part 1. CSC 330 OO Software Design 1

Chapter 8 Objects and Classes

CSC330 Object Oriented Programming. Inheritance

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming for Engineers in Python

8. Polymorphism and Inheritance

CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

Inheritance. Chapter 7. Chapter 7 1

Enhanced Entity- Relationship Models (EER)

Topics in Object-Oriented Design Patterns

CSE 143. More ArrayIntList; pre/post; exceptions; debugging. Computer Programming II. CSE 143: Computer Programming II

Adam Blank Lecture 3 Autumn 2016 CSE 143. Computer Programming II

Object-Oriented Software Engineering Practical Software Development using UML and Java. Chapter 5: Modelling with Classes

CSE 331 Software Design & Implementation

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Goals of Lecture. Lecture 27: OO Design Patterns. Pattern Resources. Design Patterns. Cover OO Design Patterns. Pattern Languages of Programming

Chapter 12 Object-Oriented Programming. Starting Out with Games & Graphics in C++ Tony Gaddis

Object Oriented Programming COP3330 / CGS5409

PART A : MULTIPLE CHOICE Circle the letter of the best answer (1 mark each)

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Inheritance. Finally, the final keyword. A widely example using inheritance. OOP: Inheritance 1

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Class Diagram. Classes consist of. Note that class diagrams contain only classes, not objects.

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

CLASSES AND OBJECTS IN JAVA

8. Object-oriented Programming. June 15, 2010

CS111: PROGRAMMING LANGUAGE II

Overview. OOP: model, map, reuse, extend. Examples of objects. Introduction to Object Oriented Design

Design Engineering. Dr. Marouane Kessentini Department of Computer Science

ECE 122. Engineering Problem Solving with Java

BM214E Object Oriented Programming Lecture 8

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Class Diagram. Classes consist of. Note that class diagrams contain only classes, not objects.

Object-Oriented Design

Last Lecture. Lecture 17: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 4448/ Spring Semester, 2005

MIT AITI Swing Event Model Lecture 17

index.pdf January 21,

Derived Classes in C++

OO Techniques & UML Class Diagrams

Chapter 5: Structural Modeling

Software Development Cycle. Unified Modeling Language. Unified Modeling Language. Unified Modeling Language. Unified Modeling Language.

MSO Analysis & UML. Hans Philippi (based on the course slides of Wouter Swierstra) August 24, Analysis & UML 1 / 56

Unified Modeling Language (UML) Class Diagram

What is inheritance. 8: Inheritance. Recall the objectives of OOP. Defining a Subclass. A cylinder is like a circle, but with an extra length

Lecture 5: Inheritance

Design Patterns. it s about the Observer pattern, the Command pattern, MVC, and some GUI. some more

COURSE 2 DESIGN PATTERNS

Engineering Design w/embedded Systems

BBM 102 Introduction to Programming II Spring Inheritance

CSE 403: Software Engineering, Spring courses.cs.washington.edu/courses/cse403/15sp/ UML Class Diagrams. Emina Torlak

BCS THE CHARTERED INSTITUTE FOR IT. BCS HIGHER EDUCATION QUALIFICATIONS BCS Level 5 Diploma in IT OBJECT ORIENTED PROGRAMMING

Lecture 19 GUI Events

Midterm 2. 7] Explain in your own words the concept of a handle class and how it s implemented in C++: What s wrong with this answer?

ITI Introduction to Computing II

ITI Introduction to Computing II

VALLIAMMAI ENGINEERING COLLEGE

And Even More and More C++ Fundamentals of Computer Science

Transcription:

Association, Aggregation and Composition Software Engineering CITS1220

Lecture Outline 1. Associations in UML 2. Aggregation and Composition 3. Examples in Java 2

Reuse A major motivation for OOP is reusability Take advantage of the previous work of yourself and others Reuse tried and tested designs and debugged and code Build classes/objects from software components 3

Ways of reusing Cut and paste (sometimes called cloning) is dangerous as bugs are duplicated We look at associations today General associations Aggregation Composition Inheritance later in semester 4

Associations An association is a relationship between two classes Describes how instances of one class refer to instances of another class Indicated in UML by connecting two classes with a line (optionally labelled) Employee worksfor Company 5

Multiplicities Associations should indicate their multiplicities Symbol near a class B indicates this many instances of B involved in relationship Employee worksfor 1 Company 6

Multiplicities II The symbol * means any number including 0 Each employee works for exactly 1 company Each company has 0 or more employees Employee * worksfor 1 Company 7

Ranges The symbol.. is used to indicate ranges Managers may have 0, 1 or more PAs Each PA must work for at least one manager, but maybe more PA * 1..* Manager 8

In Java In Java, an association is reflected by one class having variables referring to other classes public class Company { private String companyname; private Employee[] staff; } 9

An Association A MobilePhoneGUI displays exactly one MobilePhone A MobilePhone may (or may not) be displayed by a MobilePhoneGUI MobilePhoneGUI * 1 MobilePhone (Uni)directional association 10

Aggregation & Composition Special forms of association reflecting the is composed of or is built from relationship Vehicle 1 * VehiclePart MobilePhoneGUI 1 * JButton 11

What s the difference? Book gives a very subtle difference between aggregation and composition In aggregation the parts have an independent existence and can be shared or reassigned between aggregates In composition, the parts BELONG to exactly one aggregate, are created when the aggregate is created and destroyed when the aggregate is destroyed 12

Terminology Association, aggregation and composition overlap and authors often blur the differences Many would use aggregation for the MobilePhoneGUI example It is common to simply use composition We will not make fine distinctions in this unit 13

Java Example Consider a class to represent a cylinder A circular base and a height Cylinder base:circle height: double volume(): double Circle radius: double area(): double 14

Existing Circle code public class Circle { private double radius; public Circle(double r) { radius = r; } } public double area() { return (Math.PI * radius * radius); } 15

Cylinder has a Circle.. public class Cylinder { private Circle base; private double height; // constructor with two arguments public Cylinder(double r, double h) { base = new Circle(r); height = h; } 16

.. and uses its methods // method for computing volume of cylinder public double volume() { // call the area() method return (base.area() * height); } } 17

Constructing aggregate objects If a class A has instance variables referring to another class B, then when and how do the objects of class B get constructed? Internally - The constructor for A constructs all required instances of B Externally - The client passes instances of B via constructor arguments 18

Internal Construction Usual model for the filled diamond type of composition Cylinder calls constructor for Circle In Java there is no need for the parts to be destroyed explicitly as this happens automatically In C++ the destructor for Cylinder would need to call the destructor for Circle 19

External Construction public class MobilePhoneGUI { private MobilePhone myphone; // Lots of GUI stuff } How is myphone set? 20

External Construction II // Constructor public MobilePhoneGUI (MobilePhone mp) { myphone = mp; } Recall This saves the value of the parameter variable mp into the instance variable myphone There must be an already-created instance of MobilePhone available when the MobilePhoneGUI is constructed 21

Standard Java cliché It is normal to just use the same name for the parameter and instance variable // Constructor public MobilePhoneGUI (MobilePhone myphone) { this.myphone = myphone; } Avoids proliferation of single-use variable names 22

Mutual knowledge How do we ensure that two objects each know about the other? This is a particular issue for GUI design The GUI object constructs UI components such as buttons Users click buttons GUI object needs to respond and so the button needs to know about the GUI object 23

Listeners The standard mechanism for this is the use of listeners - each UI component keeps a list of who is interested in me JButton close = new JButton( C ); closebutton.addmouselistener(this); 24

Listeners II GUI object JButton close GUI object knows about the JButton because it created the button, and then passes the button a reference to itself! Now the button knows who created it - more importantly it knows who is interested in mouse events! close 25

Events A MouseEvent occurs whenever the user does something mouse-related with a component, for example clicks the button When this happens: The button notifies all of its registered listeners - in this case it notifies the GUI object that it has been clicked 26