Object Oriented Practices

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

General OO Concepts and Principles CSE301 University of Sunderland Harry R. Erwin, PhD.
Lecture 9 Improving Software Design CSC301-Winter 2011 – University of Toronto – Department of Computer Science Hesam C. Esfahani
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.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Effective C#, Chapter 1: C# Language Elements Last Updated: Fall 2011.
Object Oriented Software Development
Incremental Design Why incremental design? Goal of incremental design Tools for incremental design  UML diagrams  Design principles  Design patterns.
9-Dec Dec-15  INTRODUCTION.  FEATURES OF OOP.  ORGANIZATION OF DATA & FUNCTION IN OOP.  OOP’S DESIGN.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
SOLID Design Principles
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Engineering, 7th edition. Chapter 8 Slide 1 System models.
Advanced Programming in Java
Design Patterns: MORE Examples
Object-Oriented Programming Concepts
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING
Inheritance ITI1121 Nour El Kadri.
Course Outcomes of Object Oriented Modeling Design (17630,C604)
Object-Oriented Programming: Inheritance
Chapter 5:Design Patterns
University of Central Florida COP 3330 Object Oriented Programming
Design (2).
Final and Abstract Classes
Inheritance and Polymorphism
Chapter 11 Object-Oriented Design
Behavioral Design Patterns
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
Copyright © by Curt Hill
Review: Two Programming Paradigms
Microsoft .NET 3. Language Innovations Pan Wuming 2017.
Advanced Programming in Java
Object-Orientated Programming
Week 4 Object-Oriented Programming (1): Inheritance
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Abstract descriptions of systems whose requirements are being analysed
Adaptive Code Umamaheswaran Senior Software Engineer
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Lecture 23 Polymorphism Richard Gesick.
Object Oriented Practices
Object Oriented Practices
Object-Oriented Programming
Object-Oriented and Classical Software Engineering Sixth Edition, WCB/McGraw-Hill, 2005 Stephen R. Schach
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Java Programming Language
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Week 6 Object-Oriented Programming (2): Polymorphism
Slides by Steve Armstrong LeTourneau University Longview, TX
Interfaces.
Advanced Programming Behnam Hatami Fall 2017.
Chapter 20 Object-Oriented Analysis and Design
Object Oriented Practices
Object Oriented Practices
Software Design Lecture : 12.
Object Oriented Practices
Object Oriented Practices
Overriding Methods & Class Hierarchies
CS 350 – Software Design Principles and Strategies – Chapter 14
Fundaments of Game Design
Adapter Design Pattern
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
Review of Previous Lesson
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Chapter 8 Inheritance Part 2.
Final and Abstract Classes
C++ Object Oriented 1.
Presentation transcript:

Object Oriented Practices This is the Interfaces module. I’ve tested this to be 30 minutes in length for the slide deck. The code review portion should be 45 minutes. 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 Interfaces enables developers to treat unrelated classes as though they are similar. It’s a common contract that can be implemented by classes that are unrelated through inheritance.

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

Module Topics Interfaces 01 | Interfaces Defined 02 | Guideline: Interfaces represent Contracts 03 | Interfaces Practices 04 | Interfaces Results and Risks Interfaces enables developers to treat unrelated classes as though they are similar. It’s a common contract that can be implemented by classes that are unrelated through inheritance.

Defining Interfaces Bill Wagner | Software Consultant James Sturtevant | Senior Technical Evangelist

Defining Interfaces An Interface defines a contract that can be implemented by many unrelated classes. Read through the definition. The interfaces provide a way to define behaviour that can be implemented by unrelated types. There are important differences between classes and interfaces: . Interfaces cannot contain any implementation. They only define the contract. . A class can implement multiple interfaces. It can only have one direct base class. These differences mean important implications for their use. Discuss.

Advantages of Interfaces Contract Definition Polymorphism among unrelated types Treat unrelated types similarly Explain and discuss each of these advantages. Common, 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 Interfaces Interfaces may be public, or internal Interfaces may extend other interfaces Classes can implement multiple interfaces Must implement all members Or be abstract (more later) May implement interfaces implicitly or explicitly. The C# language (and VB.NET) provide a rich vocabulary to support interfaces. Go over the implications of these rules.

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. This applies to interfaces as well 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

Guideline: Interfaces Represent Contracts Section header Let’s look at guidelines for finding and managing inheritance Bill Wagner | Software Consultant James Sturtevant | Senior Technical Evangelist

Keep Contracts Small SRP is critically important for interface definitions The smaller and more focused the better Each interface should represent a single “feature” or “task” When you have related interfaces, consider inheritance Carefully! Discussion points here: . Larger contracts can often result in contracts that are “almost” completely implemented. . Methods that are implemented with a MethodNotImplementedException are a bad code small. . Larger contracts make it harder to satisfy Liskov. . Larger contracts often involve leaky abstractions.

Avoid Marker Interfaces A Marker interface is an interface with no members. They can be used to ‘mark’ capabilities. Require Reflection to find instances Discussion items: It’s just an anti-pattern. What’s the point of a contract that has no particular implementation?

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

Practice: Common Abstraction Interfaces provide contracts for functionality Can be implemented by completely unrelated types Example: Enumerating a sequence Container Characters in a String Lines in a file Discussion: This is one key difference between using base classes and interfaces. Interfaces can represent the same pattern implemented with totally different code.

Practice: Interfaces for Client code You can specify required contracts for contract code Create interfaces clients must implement Algorithms can be open for extension You can create algorithms that *require* certain behaviour from the code that wants to use that algorithm. Example: LINQ methods (rely on System.Enumerable) Sort: relies on Icomparable<T>

Practice: Interfaces for Test Doubles Test Doubles (in static languages) must have a type That type must be a substitute for the production type That often implies an interface that can be mocked. Necessary, but often not part of the application design. The key discussion here is that because of normative typing, anything that is used for mocking must have an interface to implement (or base class to override)

Interfaces 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: Natural Implementation Implementing an interface should be “natural” Implementing an interface should be “obvious” Corollary: Implementations for interfaces should be complete. Developers that are implementing an interface should find that natural and obvious. If it is hard to figure out how to implement an interface, that might mean your contract is ambiguous and difficult.

Good Outcome: Complete Implementation Interface Implementers can create Complete Implementations Never see NotImplementedException Discussion points: . Classes shouldn’t “almost” implement an interface

Risk: Copied implementation This is often caused by: Contracts with too many requirements Convenience Methods in Interfaces “All in one” interface Discussion points: The larger and more complicated interfaces are, the fewer classes implement all of those members in an obvious manner.