Object-Oriented Concepts and Design Principles

Similar documents
Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

Software Engineering

CSC207H: Software Design SOLID. CSC207 Winter 2018

Inheritance and object compatibility

Chapter 5 Object-Oriented Programming

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Appendix A - Glossary(of OO software term s)

Java Object Oriented Design. CSC207 Fall 2014

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques. Version 03.s 2-1

Design Patterns: Elements of Reusable Object Oriented Software. Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides

1 of 27 9/27/2001 5:34 PM

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

Object-Oriented Design

More on Inheritance. Interfaces & Abstract Classes

COURSE 2 DESIGN PATTERNS

Object Oriented Programming: Based on slides from Skrien Chapter 2

What are the characteristics of Object Oriented programming language?

Review -- an Exercise. Use the Elevator example as a context, develop a: Use case diagram Class diagram Sequence diagram Start Chart diagram

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Software Engineering CSC40232: SOFTWARE ENGINEERING. Guest Lecturer: Jin Guo SOLID Principles sarec.nd.edu/courses/se2017

Adapter Pattern Structural

Components Based Design and Development. Unit 6: Components Models

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

Chapter 6. Object- Oriented Programming Part II

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

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

ECE 3574: Dynamic Polymorphism using Inheritance

Object-Oriented Design

Introduction to Object-Oriented Programming

Abstraction. Design fundamentals in OO Systems. Fundamental Software Development Principles

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

CS342: Software Design. November 21, 2017

Design Patterns Reid Holmes

Design Pattern: Composite

Inheritance (Chapter 7)

Design Patterns Design patterns advantages:

Introduction to Object Oriented Programming. (Hebrew University, CS / Spring 2012 )

Object-Oriented Design

From Module To Objects

CHAPTER 5 GENERAL OOP CONCEPTS

Design Patterns IV Structural Design Patterns, 1

Self-review Questions

Programming II (CS300)

Intro to: Design Principles

The major elements of the object-oriented model

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Inheritance, Polymorphism, and Interfaces

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Inheritance and Substitution גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

8. Polymorphism and Inheritance

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Design Patterns V Structural Design Patterns, 2

OODP Session 4. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

System Analysis and Design. Introduction: Object Oriented Design

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

The object-oriented approach goes a step further by providing tools for the programmer to represent elements in the problem space.

Object-Oriented Software Engineering Practical Software Development using UML and Java. Chapter 2: Review of Object Orientation

OBJECT ORIENTED PROGRAMMING. Abstract Class And Interface

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Object Oriented Programming. Java-Lecture 11 Polymorphism

Programming II (CS300)

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

Design Patterns IV. Alexei Khorev. 1 Structural Patterns. Structural Patterns. 2 Adapter Design Patterns IV. Alexei Khorev. Structural Patterns

Object-Oriented Programming

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

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

CSC207 Week 3. Larry Zhang

Last Time: Object Design. Comp435 Object-Oriented Design. Last Time: Responsibilities. Last Time: Creator. Last Time: The 9 GRASP Patterns

Lecture 20: Design Patterns II

Lecture 15: Generalization, Polymorphism and States. Copyright W. Howden 1

Design Pattern- Creational pattern 2015

DESIGN PATTERNS SURESH BABU M ASST PROFESSOR VJIT

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

CSE 70 Final Exam Fall 2009

Data Structures (list, dictionary, tuples, sets, strings)

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

Object-oriented Programming. Object-oriented Programming

CS111: PROGRAMMING LANGUAGE II

Polymorphism. Arizona State University 1

Exam in TDDB84: Design Patterns,

C++ Important Questions with Answers

Object Relationships

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

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Topics in Object-Oriented Design Patterns

Lecture 5: Inheritance

Object- Oriented Design with UML and Java Part I: Fundamentals

Introduction to Design Patterns

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Chapter 8: Class and Method Design

Subtypes and Subclasses

Object-Oriented Design

Object-Oriented Software Engineering. Chapter 2: Review of Object Orientation

About This Lecture. Data Abstraction - Interfaces and Implementations. Outline. Object Concepts. Object Class, Protocol and State.

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

10/17/2011. Object Oriented Software Development. Types of interface. Interfaces example

Inheritance and Polymorphism

Programming Languages & Paradigms PROP HT Abstraction & Modularity. Inheritance vs. delegation, method vs. message. Modularity, cont d.

Transcription:

Object-Oriented Concepts and Design Principles Signature Specifying an object operation or method involves declaring its name, the objects it takes as parameters and its return value. Known as an operation signature Interface Set of all signatures defined by an objects operations is known as the objects interface Type A type denotes a particular interface, e.g. Window, Shape, Stack directly supported in Java & C# with the notion of an interface as opposed to a class A GUI object can be of types: JFrame and ActionListener, could easily be extended to MouseListener, Shape etc. A type is part or all of the interface Widely differing objects may share a type, e.g. both the traffic lights and shop example both inherit the Frame interface and implement the ActionListener interface Subtype A type is a subtype of another if it contains its interface contains that of its supertype 1

Dynamic Binding and Polymorphism dynamic binding doesn t commit an objects response to a message to a particular implementation until run-time can write programs that expect a particular interface and know that any object with that interface will respond to the message allows substitution at run time of objects which share the same interface known as polymorphism Polymorphism: simplifies definition of clients decouples from each other allows them to vary their relationships at run time Class defines an objects implementation, specifies internal data and representation and defines the operations an object can perform consequently an objects response (the method it invokes) to a message is fixed at compile time Abstract Class a class whose main purpose is to define a common interface for its subclasses defers some or all of its implementation to its subclasses cannot be instantiated only way C++ can come close to what an interface class does in Java 2

Class Inheritance and Interface Inheritance difference between an object s class and type: type only refers to set of messages to which object can respond while class defines object s internal state and operations since a class defines an object s operations, it also specifies its interface or type class inheritance defines an object s implementation in terms of another s, i.e. is a mechanism for code reuse and representation sharing. Interface inheritance or subtyping defines when an object can be substituted in another s place at run-time Java has another type of inheritance for specifying interface inheritance only, namely the interface class construct. In C++ and Eiffel inheritance means both interface and implementation inheritance. In Smalltalk, inheritance means implementation inheritance Pure interface inheritance can be achieved in C++ by inheriting publicly from pure abstract classes 3

Inheritance: Raison d Etrè Implementation reuse is only half the reason for inheritance. Its ability to define objects with identical interfaces is the basis for polymorphism (proper use!) Benefits of programming to an interface: 1. clients remain unaware of the specific types of objects they use as long as the objects adhere to the interface expected by the client 2. clients remain unaware of the classes that implement the objects greatly reduces implementation dependencies leads to principle Program to an interface and not an implementation don t declare variables to be instances of concrete classes if possible 4

Class Inheritance versus Composition 2 most common techniques for reusing functionality in object-oriented systems, inheritance allows object to extend its functionality by adding to and/or modifying the data and operations it inherits from its parent class - white box reuse composition - obtain new functionality by assembling or composing objects to get more complex structure - black-box reuse Inheritance Advantages defined statically at compile-time and easy to use. Directly supported by programming languages makes it easy to modify the implementation being reused operation overriding provides a natural or intuitive implementation of specialisation/generalisation relationships, e.g. BankAccount and CurrentAccount inter-object behaviour at run time may be easier to follow as more of the program structure is decided at compile time 5

Disadvantages can t change inherited implementation of operations at run-time as they are fixed at compile-time even worse, parent classes often define at lease part of their subclasses physical representation which leads to tight coupling between parent class and its subclasses implementation of a subclass is so tightly bound to that of its parent class that any modification of parent class will force change in subclass this breaks the spirit of encapsulation, violating a basic Object-Oriented principle one way around this is to inherit from abstract classes (or interface classes in Java) A subclass cannot rely on testing of parent class, it must be fully tested itself With inheritance, extra, optional features for an object would require extensive and unnecessary subclassing (an individual subclass for each option) which would complicate the code and class hierarchy unnecessarily 6

Composition object composition defined dynamically at run-time trough objects acquiring references to other objects, any object can be replaced at run-time by another once it has the same type requires that objects respect each others interfaces, thus not breaking encapsulation because object implementation is written in terms of object interfaces, there are much less implementation dependencies favouring composition over inheritance : helps keep classes encapsulated and focused on one task high cohesion leads to smaller and more manageable classes and class hierarchies on the other hand composition: will give rise to a greater number of objects system behaviour will depend on their interrelationships instead of on defined in class structures 7

Design Principle Favour object composition over class inheritance Ideally, should be able to get all functionality by assembling existing components. Not so in practice, usually set of existing components not rich enough. Reuse by inheritance makes it easy to create new components which can be composed with old ones. Thus inheritance and object composition work together 8

Delegation a way for making composition as powerful for reuse as inheritance For example, instead of making Window a subclass of Rectangle, Window reuses behaviour of Rectangle by keeping a rectangle instance variable and delegating rectangle specific behaviour to it delegation makes it easy to compose behaviours at run-time and to change the way they are composed. E.g. change a Window to oval at run-time by substituting the rectangle reference to an oval reference assuming they both share a same type such as Shape harder to understand than more static software run-time inefficiencies, not serious criterion use only when it simplifies more than it complicates 9