Object Oriented Practices

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Software Engineering and Design Principles Chapter 1.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Chapter 1 Principles of Programming and Software Engineering.
CS 2511 Fall Features of Object Oriented Technology  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance.
Computer Science 240 Principles of Software Design.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
SAMANVITHA RAMAYANAM 18 TH FEBRUARY 2010 CPE 691 LAYERED APPLICATION.
Computer Science 240 © Ken Rodham 2006 Principles of Software Design.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
User Interface Principles in API Design Elliotte Rusty Harold
Software Engineering and Object-Oriented Design Topics: Solutions Modules Key Programming Issues Development Methods Object-Oriented Principles.
Introduction to Object-Oriented Programming Lesson 2.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Beginning Software Craftsmanship Brendan Enrick Steve Smith
Principles of Programming & Software Engineering
Object Oriented Systems Design
Appendix 1 - Packages Jim Fawcett copyright (c)
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Chapter 10 Thinking in Objects
Data Abstraction: The Walls
Classes (Part 1) Lecture 3
Classes and Objects: Encapsulation
CSE687 – Object Oriented Design
Chapter 10 Thinking in Objects
Chapter 10 Thinking in Objects
Data Abstraction: The Walls
Modern Collections Classes
Principles of Programming and Software Engineering
Copyright © by Curt Hill
Review: Two Programming Paradigms
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Magento Technical Guidelines Eugene Shakhsuvarov, Software Magento
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
The Object-Oriented Thought Process Chapter 08
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
Creating and Using Classes
Object Oriented Practices
Object Oriented Practices
Classes and Objects: Encapsulation
Chapter 9 Thinking in Objects
Objects First with Java
Classes and Objects Encapsulation
Encapsulation & Visibility Modifiers
Object-Oriented Programming Using C++ Second Edition
Informatics 122 Software Design II
Object Oriented Practices
Object Oriented Practices
Need for the subject.
Programming Logic and Design Fourth Edition, Comprehensive
Modern Collections Classes
Object Oriented Practices
DEV-08: Exploring Object-oriented Programming
Software Design and Architecture
Chapter 9 Thinking in Objects
Object Oriented Practices
SAMANVITHA RAMAYANAM 18TH FEBRUARY 2010 CPE 691
COP 3330 Object-oriented Programming in C++
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
CMSC 202 Encapsulation Version 9/10.
Chapter 8 - Design Strategies
Classes and Objects Systems Programming.
C++ Object Oriented 1.
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Presentation transcript:

Object Oriented Practices This is the Encapsulation 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 Encapsulation is a way to minimize how much of your code and design is visible to others. In this module we’ll go over techniques to hide as much of the implementation details as is prudent from external code. We’ll explain how this leads to a more maintainable code base, and enables you to change the implementation with minimal concern on other areas of the code. It’s also one of the core concepts in Object Oriented Programming. It’s how we isolate different elements of our programs so that we can move forward faster and with more speed. Let’s explore the concepts, and how our programming languages enable us to achieve the isolation that we seek in different programming tasks.

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

Encapsulation Topics 01 | Definition 05 – Code Review 02 | Guideline: Limit Accessibility 06 – Homework Results 03 | Encapsulation Practices 04 | Encapsulation Results Encapsulation is a way to minimize how much of your code and design is visible to others. In this module we’ll go over techniques to hide as much of the implementation details as is prudent from external code. We’ll explain how this leads to a more maintainable code base, and enables you to change the implementation with minimal concern on other areas of the code. It’s also one of the core concepts in Object Oriented Programming. It’s how we isolate different elements of our programs so that we can move forward faster and with more speed. Let’s explore the concepts, and how our programming languages enable us to achieve the isolation that we seek in different programming tasks.

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

Defining Encapsulation Encapsulation is minimizing the visibility of every program element to the smallest possible scope. Read through the definition. Explain why it creates more maintainable programs. -hiding complexty Define “scope” for the views. Explain that “program element” can be a data field, method, property, or anything else.

Advantages of Encapsulation Fewer name collisions Refactoring code is easier Smaller API Footprint Elements with less visibility can be changed more safely Explain and discuss each of these advantages. Why is less visibility better? Discuss the continuum, public, protected, internal, private.

Language Support for Encapsulation Classes: Public Internal (default) Private (nested classes only) Members Internal Protected internal Private (default) The C# language (and VB.NET) provide a rich vocabulary to specify the accessibility of each element. Note that the default accessibility is limited. That’s to encourage you to create elements with less visibility. (but it’s a good idea to be explicit for readability) Good rule of thumb is to start limited and slowly expose as you need it.

Guideline: Limit Accessibillity Section header The rest of this presentation is a set of guidelines that can help limit accessibility and promote encapsulation Bill Wagner | Software Consultant James Sturtevant | Senior Technical Evangelist

Strive for ‘Small’ Classes ‘Small’ is a relative term Avoid Feature Creep Minimize the public API Avoid any Unnecessary Additions Consider More classes Avoid extra convenience methods Discussion points here: . Classes should be focused . API Surface area should be as small and focused as possible. . API should focus on what, and hide all the how for the algorithm.

Single Responsibility Principle A class should be responsible for exactly one item of functionality Related to Separation of Concerns The ‘S’ in the SOLID Principles Discussion items: . Classes with exactly one responsibility are easier to enforce encapsulation . The focus minimizes the need for increased visibility. SOLID (Single responsibility, Open-closed, Liskov substitution, Interface segregation and Dependency inversion)

Why is Smaller Easier? Classes with smaller scope almost always have smaller public APIs Easier to understand Explain the advantages of smaller classes. Grady Booch (UML creator) quote: When your design is too complicated, make more classes. It seems counter intuitive, but it actually helps a great deal.

Thought Exercise: Class with One Public API Consider what a class with a single public Method looks like How can items be encapsulated? How much is encapsulated? Discuss as an option. How does that speak to encapsulation and related design? It’s extreme, but it’s worth considering.

Encapsulation Practices Section header How can we accomplish better encapsulation? Bill Wagner | Software Consultant James Sturtevant | Senior Technical Evangelist

Practice: Public Methods are Minimized Fewer public methods means minimal surface area Minimal Surface Area means opportunities for encapsulation The key discussion item is that we want to minimize the “what”, so that we can keep the “how” private.

Practice: All Member Fields are Private Advantages: Gated access through properties Validation on any updates or modifications Storage model can change without affecting client code. Concerns: Can provide layers of latency without value. Advantages is obvious Concern: If the class is just a data container, it adds extra work with minimal gain.

Practice: Properties Provide Validation Advantages: An object can be guaranteed to be in a valid state Invalid transformations are blocked Private methods can assume a valid state Discussion item: If an object is always in a valid state, then private methods can simply do their work. They need less error checking, because private methods can assume that an object is valid.

Practice: Public Accessors Provide Safe Access Can return computed data Can return interface reference Instead of direct access Can return a transformation Discussion: . Hiding direct access means hiding ‘how’

Practice: Public Mutators Require Validation “Gated” API for any changes Ensures Validity Throw exception on invalid transformations Keep Object state consistent Discussion: This protects object invariants Objects are valid Private, encapsulated items are hidden.

Practice: Mutators Complete or Do Nothing Keeps Valid Objects Valid State is always known Either all changes are made -or- None of the changes are made This is a bigger discussion How best to keep all the object state coherent? Don’t want to make half a change.

Practice: Common Code in Internal Classes Encapsulation is not just about private members Internal Classes are encapsulated in assemblies Encapsulating Helper classes Discussion: There are layers of encapsulation It’s not just Private and Public

Practice: Use Explicit Accessibility Defaults are Good Practices But, explicit is more readable Forces you to make an explicit decision Discussion topics: Code is read often Be clear But understand that the default is a good idea.

Practice: Nested Private Classes Sometimes a class is only useful to one other class Consider a nested private class Very tightly coupled, and encapsulated This is not an edge case, but it’s not completely common either. Discuss when to use. Discuss how to use. Point out how there is a disadvantage in that we can end up with duplicated code.

Encapsulation Results Section header Why are these practices going to help? Bill Wagner | Software Consultant James Sturtevant | Senior Technical Evangelist

Good Outcome: Clear and Minimal Interface Public Interfaces define What Should be (close to) Invariants New Versions should mean additions, not changes By encapsulating as much as possible, we should create public interfaces that are more constant over time.

Good Outcome: Smallest Possible Visibility Less chance of Leaky Abstraction Less chance of future breaking changes Easier to Enhance Discussion points: . What is a Leaky Abstraction? details and limitations of the implementation leak through ORM as exam . Why will that lead to breaking changes? . Which makes everything easier to enhance.

Good Outcome: Implementation is Hidden Public is “What” Private is “How” Over time, implementations may change: . Better algorithm . Better performance . Updates to dependencies. If those are private, no worries.

What’s Next Code Review StringBuilder ArrayList Dictionary We’re going to review a few of the .NET core elements and discuss how these are applied.