Object Oriented Practices

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

General OO Concepts and Principles CSE301 University of Sunderland Harry R. Erwin, PhD.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
1 CS1001 Lecture Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.
Object-oriented Programming Concepts
Computer Science 240 Principles of Software Design.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
DAAD project “Joint Course on OOP using Java” Design Patterns in the course ‘OOP in Java’ - first experiences Ana Madevska Bogdanova Institute of informatics.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Computer Science 240 © Ken Rodham 2006 Principles of Software Design.
CSC 395 – Software Engineering Lecture 12: Reusability –or– Programming was Bjarne Again.
Introduction to SOLID Principles. Background Dependency Inversion Principle Single Responsibility Principle Open/Closed Principle Liskov Substitution.
Instructor: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Object Oriented.
DAAD project “Joint Course on OOP using Java” On Object Oriented modeling in Java (Why & How) Ana Madevska Bogdanova Institute of informatics Faculty of.
Chapter 8 Object Design Reuse and Patterns. Object Design Object design is the process of adding details to the requirements analysis and making implementation.
Object Oriented Software Development
Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
CS2110: SW Development Methods Inheritance in OO and in Java Part 2: Topics: Forms of inheritance Interfaces in Java.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
CS 350 – Software Design Expanding Our Horizons – Chapter 8 The traditional view of objects is that they are data with methods. Sometimes objects could.
OO in Context Lecture 13: Dolores Zage. Confused about OO Not alone, there is much confusion about OO many programs are claimed to be OO but are not really.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
SOLID Design Principles
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CSCE 240 – Intro to Software Engineering Lecture 3.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
1 More About Derived Classes and Inheritance Chapter 9.
Object-Oriented Design
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING
CS 350 – Software Design The Strategy Pattern – Chapter 9
CSE687 - Object Oriented Design class notes Survey of the C++ Programming Language Jim Fawcett Spring 2004.
Behavioral Design Patterns
Copyright © by Curt Hill
Object Oriented Concepts -II
Week 4 Object-Oriented Programming (1): Inheritance
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
The Object-Oriented Thought Process Chapter 08
Strategy Design Pattern
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Inheritance B.Ramamurthy 11/7/2018 B.Ramamurthy.
Object Oriented Practices
Understanding Inheritance
CSE 332 Overview and Structure
OOP and ADTs Chapter 14 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved.
The Object-Oriented Thought Process Chapter 05
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Week 6 Object-Oriented Programming (2): Polymorphism
Subtype Polymorphism, Subtyping vs
Object Oriented Practices
Object Oriented Practices
Object Oriented Practices
Object Oriented Practices
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Object Oriented Practices
Fundaments of Game Design
Adapter Design Pattern
Workshop for Programming And Systems Management Teachers
Overview of C++ Polymorphism
Object Oriented Analysis and Design
Review of Previous Lesson
Object-Oriented PHP (1)
Object Oriented Design & Analysis
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
C++ Object Oriented 1.
Liskov Substitution Principle (LSP)
Presentation transcript:

Object Oriented Practices This is the Encapsulation module. I’ve tested this to be 40 minutes in length for the slide deck. The code review portion should be an hour. The speaker notes on the slides will detail the key concepts we want to cover in each of these slid Bill Wagner | Software Consultant James Sturtevant | Senior Technical Evangelist, Microsoft

Course Topics Object Oriented Practices 01 | Encapsulation 05 | Generics 02 | Inheritance 06 | Delegates Events and Lambdas 03 | Interfaces 07 | Functional Programming 04 | Abstract Classes 08 | Review Exercises Inheritance is one of the most common OO idioms to achieve reuse in your designs. It’s a very powerful idiom. It’s also one that if you overuse, it can cause lots of problems.

Inheritance Bill Wagner | Software Consultant James Sturtevant | Senior Technical Evangelist

Module Topics Inheritance 01 | Definition 05 | Inheritance Results and Risks 02 | Inheritance 03 | Inheritance Guidelines 04 | Inheritance Practices Inheritance is one of the most common OO idioms to achieve reuse in your designs. It’s a very powerful idiom. It’s also one that if you overuse, it can cause lots of problems.

Inheritance Defined Bill Wagner | Software Consultant James Sturtevant | Senior Technical Evangelist

Defining Inheritance Inheritance is the technique of reusing code by creating base classes for behaviour that is shared by multiple derived classes that implement specializations of the base class Read through the definition. Explain why it creates more maintainable programs. Explain how this can provide for code reuse by creating a common implementation for shared behaviour. Discuss the “Is a” catch phrase. The idea that a derived class “is a” base class. Example - Cars

Advantages of Inheritance Implementation Reuse Shared base class implementation for all derived classes Polymorphism Support Explain and discuss each of these advantages. Implementation Reuse means a smaller codebase, Polymorphism means that we can write functionality using the base class references. This means algorithms can be shared by writing the methods using only the base class methods and properties.

Language Support for Inheritance Classes can declare a single base class Derived Classes inherit all base class functionality Can call protected members in the base class Can access protected fields in the base class Virtual Members Can be re-implemented by derived classes Can be abstract (which we will discuss later) The C# language (and VB.NET) provide a rich vocabulary to support inheritance. We can specify base classes, access all of those members of the base class.

Guideline: “Is a” to model Inheritance Section header Let’s look at guidelines for finding and managing inheritance Bill Wagner | Software Consultant James Sturtevant | Senior Technical Evangelist

Start with Leaf Classes Begin by Defining Classes For your Application Don’t Focus on Inheritance too early Look for common behaviour as you add classes Remember the “is a” catchphrase Discussion points here: . It’s hard to design base classes until you see some of the derived classes It’s much easier to see inheritance opportunities when you see some leaf classes.

Search for Common Behavior Common Behaviour represents base class implementation Common Storage may not be important Discussion items: . The most important benefits from inheritance come from sharing implementation of behaviour, not common storage . Common storage doesn’t really save much in terms of code or space. Also, common storage may not represent the kind of behaviour that we want to get from implementation reuse. It may be just a coincidence.

Liskov Substitutability Principle Let Φ(x) be a property provable about objects x of type T. Then Φ(y) should be true for objects y of type S where S is a subtype of T. Explain this means any derived class object can be used in place of a base class object without noticing any difference. Classic violation: . Square and a Rectangle with SetWIdth and SetHeight . Circle and Ellipse when setting the radii

Inheritance Practices Section header How can we accomplish smart inheritance? Bill Wagner | Software Consultant James Sturtevant | Senior Technical Evangelist

Practice: Consider Inheritance over Copy/Paste I need something like <Class> Separate common parts into base Create 2 derived classes The key discussion here is that if you want to copy, past, modify, you may be seeing an opportunity for inheritance.

Practice: Leverage the .NET Base Class Library There’s a lot of functionality you can reuse Classes (usually) designed for use as base classes Tested implementations for common patterns There are a lot of classes in the .NET BCL that are designed to be used as base classes. Use them. You’ll learn how to create derived classes, how to use and extend base classes. Examples? -Controllers -Pages -ActionResults

Practice: Seek Common Behavior Can seemingly unrelated classes share implementation? Sometimes, if the behaviour is shared. Example: Memory Stream File Stream String Stream It’s important to make sure that you look for behaviour, and not just similar storage. Patterns. From the example, different concepts can actually be related by inheritance, and may share implementation.

Inheritance Results and Risks Section header Why are these practices going to help? How might they cause concern? Bill Wagner | Software Consultant James Sturtevant | Senior Technical Evangelist

Good Outcome: Generalities in Base classes Behaviour that can be shared is shared Codebase is smaller New Derived classes can be developed quickly It’s all about the reuse

Good Outcome: Polymorphism that works New derived classes can be used in existing scenarios Follow Liskov Substitutability Principle Discussion points: . Derived class are immediately useful. They can be used in all places where the base class can already be used Polymorphism makes it easier to incorporate new derived classes very quickly.

Risk: “Almost is-a” This is an example of a leaky abstraction Look for Violations of the Liskov Substitutability Principle Discussion points: . “If it looks like a duck, and quacks like a duck, but needs batteries, you may have a leaky abstraction.” . This almost always means violating the Liskov Substitutability Principle. . The

Risk: Too Deep Hierarchy Very Deep hierarchies make it hard to discover functionality Increases the chances of LSP violations Increasing limiting to extensions Derived classes inherit too much functionality Discussion points: When hierarchies are too deep, its easy to get confused with all the inherited capabilities Difficult to continue extending Difficult to maintain

Risks: Downcasting needed Downcasting is converting a base class to a derived class Another example of an LSP violation You need to get at the base class functionality Example of a leaky abstraction Last slide, so point out how bad hierarchies show up in any of these possible poor outcomes. When used properly, it’s very powerful. When mis-used, it’s very painful.