In this Lecture you will Learn: Object Design. Information Sources for Object Design. Class Specification: Attributes

Similar documents
OO Analysis and Design with UML 2 and UP

In this Lecture you will Learn: Design Patterns. Patterns vs. Frameworks. Patterns vs. Frameworks

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

C++ (Non for C Programmer) (BT307) 40 Hours

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

REVIEW OF THE BASIC CHARACTERISTICS OF OBJECT ORIENTATION

Introduction to Unified Modelling Language (UML)

Software Engineering from a

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

Software Engineering

Object-Oriented Design

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

Object-Oriented Concepts and Design Principles

COURSE 2 DESIGN PATTERNS

Chapter 1: Principles of Programming and Software Engineering

CS246 Software Abstraction and Specification Final Examination

Pertemuan 8. Object Oriented Modeling and Design

Chapter 8: Class and Method Design

SOLID Principles. Equuleus Technologies. Optional Subheading October 19, 2016

Software Construction

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Object-Oriented Analysis, Design and Implementation. Case Study Part II

Object-Oriented Analysis and Design Using UML

Self-review Questions

Chapter 5 Object-Oriented Programming

Engineering Design w/embedded Systems

Unified Modeling Language

Object Relationships

Today's Agenda. References. Open Closed Principle. CS 247: Software Engineering Principles. Object-Oriented Design Principles

Lecturer: Sebastian Coope Ashton Building, Room G.18 COMP 201 web-page:

Lecture 20: Implementation () / 33

BCS THE CHARTERED INSTITUTE FOR IT. BCS HIGHER EDUCATION QUALIFICATIONS BCS Level 5 Diploma in IT. Object Oriented Programming

Object-Oriented Design

Summary of the course lectures

UML Modeling I. Instructor: Yongjie Zheng September 3, CS 490MT/5555 Software Methods and Tools

Recap : UML artefacts. Black Box Requirements. Functional Specification. System. System. Test. Design. System. System. Development.

Unified Modeling Language (UML)

UML & OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 3 08/30/2011

The static Keyword. Lecture 2 Java Intermediate. The static Keyword. The Plan. Class Attributes The static Keyword. Class Methods The static Keyword

UML REFERENCE SHEETS. 2013, 2014 Michael Marking; all rights reserved, including moral rights. Web site:

Assigning Responsibilities (Patterns of Responsibility Assignment Principles: GRASP)

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

Lecture Chapter 2 Software Development

Programming, numerics and optimization

Inheritance and Interfaces

Information Hiding and Aspect-Oriented Modeling

Feature/Class Modeling for Software Product Lines

Introduction to Inheritance

CGS 2405 Advanced Programming with C++ Course Justification

UML Fundamental. OutLine. NetFusion Tech. Co., Ltd. Jack Lee. Use-case diagram Class diagram Sequence diagram

Originality is Overrated: OO Design Principles

A Conceptual Model of the UML

Concepts of Programming Languages

Fundamental Concepts

LABORATORY 1 REVISION

09. Component-Level Design

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

CHAPTER 1. Topic: UML Overview. CHAPTER 1: Topic 1. Topic: UML Overview

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

Software Design Fundamentals. CSCE Lecture 11-09/27/2016

OO design. Classes, Responsibilities, Collaborations (CRC) 13/9/1999 COSC

Introduction to Software Engineering. 5. Modeling Objects and Classes

CHAPTER 4 HEURISTICS BASED ON OBJECT ORIENTED METRICS

Introduction... ix. Chapter 1: Exploring Fundamental Programming Concepts... 1

Patterns and Testing

OBJECT ORİENTATİON ENCAPSULATİON

Introduction to Objects. James Brucker

QUIZ. How could we disable the automatic creation of copyconstructors

Exercise Unit 2: Modeling Paradigms - RT-UML. UML: The Unified Modeling Language. Statecharts. RT-UML in AnyLogic

CPS122 Lecture: Defining a Class

Programmazione. Prof. Marco Bertini

In this Lecture you will Learn: System Design. System Architecture. System Architecture

Object Oriented Modeling

JAVA MOCK TEST JAVA MOCK TEST II

Software Service Engineering

QUIZ. How could we disable the automatic creation of copyconstructors

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

Chapter 2: The Object-Oriented Design Process

Aggregation. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

Chapter 10 Object-Oriented Design Principles

Refining the Requirements Model

Software Development

Inheritance and Polymorphism

Safety Case Composition Using Contracts - Refinements based on Feedback from an Industrial Case Study

SE 1: Software Requirements Specification and Analysis

CSE 70 Final Exam Fall 2009

4.1 Introduction Programming preliminaries Constructors Destructors An example... 3

Software Design Heuristics

SDC Design patterns GoF

Chapter No. 2 Class modeling CO:-Sketch Class,object models using fundamental relationships Contents 2.1 Object and Class Concepts (12M) Objects,

Keywords: Abstract Factory, Singleton, Factory Method, Prototype, Builder, Composite, Flyweight, Decorator.

UML & OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 3 09/04/2012

Static Modeling. SWE 321 Fall2014

Outline CSE 142. Specification vs Implementation - Review. CSE142 Wi03 F-1. Instance Variables

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

Inheritance: Definition

S T R U C T U R A L M O D E L I N G ( M O D E L I N G A S Y S T E M ' S L O G I C A L S T R U C T U R E U S I N G C L A S S E S A N D C L A S S D I A

STUDENT LESSON A5 Designing and Using Classes

Topic : Object Oriented Design Principles

Transcription:

In this Lecture you will Learn: Object Design Chapter 14 How to apply criteria for good design How to design associations The impact of integrity constraints on design How to design operations The role of normalization in object design M8748 Peter Lo 2007 1 M8748 Peter Lo 2007 2 Information Sources for Object Design Class Specification: Attributes Object Design is concerned with the detailed design of the objects and their interactions. Object Design is particularly concerned with the specification of the attribute type, how operations function and how objects are linked to other objects. An attribute s data type is declared in UML using the following syntax name : type-expression = initial-value { propertystring } Where name is the attribute name type-expression is its data type initial-value is the value the attribute is set to when the object is first created property-string describes a property of the attribute, such as constant or fixed M8748 Peter Lo 2007 3 M8748 Peter Lo 2007 4

Class Specification: Attributes Common primitive data types include Boolean (true or false), Character (any alphanumeric or special character), Integer (whole numbers) and Floating-Point (decimal numbers). In most object-oriented languages more complex data types, such as Money, String, Date, or Name can be constructed from the primitive data types or may be available in standard libraries. The attribute name is the only feature of its declaration that is compulsory. Attributes Example The attribute balance in a BankAccount class might be declared with an initial value of zero using the syntax balance: Money = 0.00 Attributes that may not be null are specified accountname: String {not null} Arrays are specified qualification[0..10]: String M8748 Peter Lo 2007 5 M8748 Peter Lo 2007 6 Class Diagram Notation for Attribute Class Specification: Operations Shows a derivable attribute BankAccount accountnumber : Integer accountname : String {not null} balance : Money = 0 /availablebalance : Money overdraftlimit : Money open(accountname : String) : Boolean close() : Boolean credit(amount:money) : Boolean debit(amount:money) : Boolean getbalance() : Money setbalance(newbalance : Money) getaccountname() : String setaccountname(newname : String) BankAccount class with the attribute data types included The syntax used for an operation is operation name ( parameter-list ) : returntype-expression An operation s Signature is determined by the operation s name, the number and type of its parameters and the type of the return value if any M8748 Peter Lo 2007 7 M8748 Peter Lo 2007 8

Operation Example Class Diagram Notation for Operation The BankAccount class might have a credit() operation that passes the amount being credited to the receiving object and has a Boolean return value credit(amount: money): Boolean A credit() message sent to a BankAccount object could have the format creditok = accobject.credit(500.00) BankAccount accountnumber : Integer accountname : String {not null} balance : Money = 0 /availablebalance : Money overdraftlimit : Money open(accountname : String) : Boolean close() : Boolean credit(amount:money) : Boolean debit(amount:money) : Boolean getbalance() : Money setbalance(newbalance : Money) getaccountname() : String setaccountname(newname : String) BankAccount class with operation signatures included. M8748 Peter Lo 2007 9 M8748 Peter Lo 2007 10 Which Operations will Show? Primary Operations are the create, destroy, get and set operation. Generally don t show primary operations Only show constructors where they have special significance Varying levels of detail at different stages in the development cycle M8748 Peter Lo 2007 11 Visibility An attribute or operation may be assigned one of three levels of visibility i.e. public (+), private (-), protected (#) or package (~). Visibility symbol Visibility Meaning + Public The feature (an operation or an attribute) is directly accessible by an instance of any class. - Private The feature may only be used by an instance the class that includes it. # Protected The feature may be used either by the class that includes it or by a subclass or descendant of that class. ~ Package The feature is directly accessible only by M8748 Peter Lo 2007 instances of a class in the same package. 12

Class Diagram Notation for Visibility BankAccount class with visibility specified Class Exercise Why should attributes be private? M8748 Peter Lo 2007 13 M8748 Peter Lo 2007 14 Interfaces UML supports two notations to show interfaces The small circle icon showing no detail A stereotyped class icon with a list of the operations supported Normally only one of these notations is used on any one diagram The realize relationship, represented by the dashed line with a triangular arrowhead, indicates that the client class (e.g. Advert) supports at least the operations listed in the interface Class Diagram Notation for Interfaces Interfaces for the Advert class M8748 Peter Lo 2007 15 M8748 Peter Lo 2007 16

Criteria for Good Design: Coupling Coupling describes the degree of interconnectedness between design components It is reflected by the number of links an object has and by the degree of interaction the object has with other objects Criteria for Good Design: Cohesion Cohesion is a measure of the degree to which an element contributes to a single purpose The concepts of coupling and cohesion are not mutually exclusive but actually support each other Coad and Yourdon (1991) suggested several ways in which coupling and cohesion can be applied within an Object-Oriented approach M8748 Peter Lo 2007 17 M8748 Peter Lo 2007 18 Inheritance Coupling Operation Cohesion Inheritance Coupling describes the degree to which a subclass actually needs the features it inherits from its base class Vehicle decription servicedate maximumaltitude takeoffspeed checkaltitude() takeoff() LandVehicle Poor inheritance coupling as unwanted attributes and operations are inherited Operation Cohesion measures the degree to which an operation focuses on a single functional requirement. Good design produces high cohesive operations, each of which deal with a single functional requirement. Good operation cohesion but poor class cohesion numberofaxles registrationdate M8748 Peter Lo 2007 register() 19 M8748 Peter Lo 2007 20

Specialization Cohesion Specialization Cohesion addresses the semantic cohesion of inheritance hierarchies Class Exercise How does the application of the concepts of coupling and cohesion help to produce object-oriented designs? Poor specification cohesion Improved structure using Address class M8748 Peter Lo 2007 21 M8748 Peter Lo 2007 22 Liskov Substitution Principle (LSP) Liskov Substitution Principle Example Liskov Substitution Principle (LSP) is another design criterion that is applicable to inheritance hierarchies. Essentially the principle states that, in object interactions, it should be possible to treat a derived object as if it were a base object without integrity problems If the principle is not applied then it may be possible to violate the integrity of the derived object M8748 Peter Lo 2007 23 Disinheritance of debit() means that the left-hand hierarchy is not Liskov compliant ChequeAccount accountname balance credit() debit() Restructuring to satisfy LSP Account accountname balance credit() MortgageAccount MortgageAccount ChequeAccount interestrate interestrate calculateinterest() M8748 - debit() Peter Lo 2007 calculateinterest() debit() 24

Advantages of Applying LSP Disadvantages of Applying LSP The production of inheritance structures that exhibit high levels of inheritance coupling. The resulting classes in the inheritance structure are cohesive. There is no anomalous behavior when base class operations are applied to derived class objects and as a result the program code is much safer. The resulting inheritance hierarchy may not map precisely onto problem domain objects. (It is a moot point whether this is a disadvantage or not though it does complicate traceability from the problem domain to the implementation to some extent.) If base classes are not chosen carefully extending an inheritance hierarchy may require its complete restructuring to ensure Liskov compliance and this may be result in significant changes to programme code. For this reason it may not always be cost effective to apply LSP (again a debatable point) but when LSP is not enforced the resulting structures are likely to be less safe. Many developers take a pragmatic view to applying LSP because of this contention between the perception that development and maintenance costs are higher and the development of systems that are safe because maintaining the integrity of derived classes is easier with LSP. M8748 Peter Lo 2007 25 M8748 Peter Lo 2007 26 Further Design Guidelines Design Clarity Coad and Yourdon (1991) and Yourdon (1994) suggest further design guidelines that are included in the list below: Design Clarity Don t Over-Design Control Inheritance Hierarchies Keep Messages and Operations Simple Design Volatility Evaluate by Scenario Design by Delegation Keep Classes Separate A design should be made as easy to understand as possible. This reinforces the need to use design standards or protocols that have been specified. M8748 Peter Lo 2007 27 M8748 Peter Lo 2007 28

Don t Over-Design Control Inheritance Hierarchies Developers are on occasions tempted to produce designs that may not only satisfy current requirements but may also be capable of supporting a wide range of future requirements. Designing flexibility (or any other non-functional requirement) into a system has a cost, the system may take longer to design and construct but this may be offset in the future by easier and less expensive modification. However, it is not feasible to design for every eventuality. Systems that are over-designed in first instance are more difficult to extend if the modifications are not sympathetic to the existing structure. Inheritance hierarchies should be neither too deep nor too shallow. If a hierarchy is too deep it is difficult for the developer to understand easily what features are inherited. There is a tendency for developers new to O-O to produce over-specialized hierarchies, thus adding complexity rather than reducing it. Yourdon (1994) suggests that hierarchies of up to nine levels are manageable, whereas Rumbaugh et al. (1991) suggest that more than about four or five levels is too many. M8748 Peter Lo 2007 29 M8748 Peter Lo 2007 30 Keep Messages and Operations Simple In general it is better to limit the number of parameters passed in a message to no more than three (of course, a single parameter might be a complete object). Ideally an operation should be capable of specification in no more than one page. Design Volatility A good design will be stable in response to changes in requirements. It is reasonable to expect some change in the design if the requirements are changed. However, any change in the design should be commensurate with the change in requirements. Enforcing encapsulation is a key factor in producing stable systems. M8748 Peter Lo 2007 31 M8748 Peter Lo 2007 32

Evaluate by Scenario An effective way of testing the suitability of a design is to role play it against the use cases using CRC cards if appropriate. Design by Delegation A complex object should be decomposed (if possible) into component objects forming a composition or aggregation. Behavior can then be delegated to the component objects producing a group of objects that are easier to construct and maintain. This approach also improves reusability. M8748 Peter Lo 2007 33 M8748 Peter Lo 2007 34 Keep Classes Separate In general, it is better not to place one class inside another. The internal class is encapsulated by the other class and cannot be accessed independently. This reduces the flexibility of the system. Designing Associations Determine direction of message passing i.e. the navigability of the association In general an association between two classes A and B should be considered with the questions Do objects of class A have to send messages to objects of class B? Does an A object have to provide some other object with B object identifiers? If yes then A needs Bs object identifier M8748 Peter Lo 2007 35 M8748 Peter Lo 2007 36

Designing Associations An association that has to support message passing in both directions is a two-way association A two-way association is indicated with arrowheads at both ends Minimizing the number of two-way associations keeps the coupling between objects as low as possible One-to-One Association Example for the One-way One-to-One association M8748 Peter Lo 2007 37 M8748 Peter Lo 2007 38 One-to-Many Association Example for the Fragment of Class Diagram for the Agate Case Study Many-to-Many Associations This is the design for the works On Campaign association M8748 Peter Lo 2007 39 M8748 Peter Lo 2007 40

Collection Classes Collection classes are used to hold the object identifiers when message passing is required from one to many along an association OO languages provide support for these requirements. Frequently the collection class may be implemented as part of the sending class (e.g. Campaign) as some form of list Collection Classes Class Diagram Example of the One-to-Many Association using a Collection Class M8748 Peter Lo 2007 41 M8748 Peter Lo 2007 42 Collection Classes Sequence Diagram This sequence diagram shows the interaction when using a collection class Integrity Constraints Referential Integrity that ensures that an object identifier in an object is actually referring to an object that exists. It is maintained by including appropriate checks in object constructors and destructors. These checks would ensure that object references were either null (if the multiplicity constraints permitted this) or that the object reference refers to an object that exists. Dependency Constraints that ensures that attribute dependencies, where one attribute may be calculated from other attributes, are maintained consistently Domain Integrity that ensures that attributes only hold permissible values M8748 Peter Lo 2007 43 M8748 Peter Lo 2007 44

Constraints between Associations Dependency constraints can also exist between or among association. Designing Operations Various factors constrain algorithm design: The cost of implementation Performance constraints Requirements for accuracy The capabilities of the implementation platform M8748 Peter Lo 2007 45 M8748 Peter Lo 2007 46 Designing Operations Factors that should be considered when choosing among alternative algorithm designs Computational complexity Ease of implementation and understandability Flexibility Fine-tuning the object model Normalisation Normalization may be useful in OO approaches When implementing the system with a relational database management As a guide to decomposing a large, complex (and probably not very cohesive) objects Objects need not be normalised but it is important to remove redundancy M8748 Peter Lo 2007 47 M8748 Peter Lo 2007 48