Object Oriented Practices

Slides:



Advertisements
Similar presentations
George Blank University Lecturer.
Advertisements

ITEC200 – Week03 Inheritance and Class Hierarchies.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
From C++ to C#. Web programming The course is on web programming using ASP.Net and C# The course is on web programming using ASP.Net and C# ASP.Net is.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
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.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
An Object-Oriented Approach to Programming Logic and Design
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.
CS200 Algorithms and Data StructuresColorado State University Part 4. Advanced Java Topics Instructor: Sangmi Pallickara
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
Object Oriented Software Development
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Object-Oriented Programming. Objectives Distinguish between object-oriented and procedure-oriented design. Define the terms class and object. Distinguish.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
ITF11006.NET Generics. Generics - Sample Recent Generics - Characteristics Performance Type Safety Binary Code Reuse Code Bloat Naming Guidelines.
Introduction to Object-Oriented Programming Lesson 2.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
Object-Oriented Design Concepts University of Sunderland.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Object Oriented Programming Some Interesting Genes.
Motivation for Generic Programming in C++
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Copyright © Jim Fawcett Spring 2017
Data Abstraction: The Walls
Classes C++ representation of an object
Static data members Constructors and Destructors
Object-Oriented Programming: Classes and Objects
2.7 Inheritance Types of inheritance
Module 5: Common Type System
Review: Two Programming Paradigms
Table of Contents Class Objects.
About the Presentations
Microsoft .NET 3. Language Innovations Pan Wuming 2017.
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Week 4 Object-Oriented Programming (1): Inheritance
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Object-Oriented Programming: Classes and Objects
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Lecture 23 Polymorphism Richard Gesick.
Object Oriented Practices
Object Oriented Practices
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Object Based Programming
AVG 24th 2015 ADVANCED c# - part 1.
Java Programming Language
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Object Oriented Practices
Lesson 7. Events, Delegates, Generics.
Object Oriented Practices
Object Oriented Practices
Object Oriented Practices
COP 3330 Object-oriented Programming in C++
Fundaments of Game Design
Inheritance and Polymorphism
CIS 199 Final Review.
5. 3 Coding with Denotations
Classes C++ representation of an object
C++ Object Oriented 1.
Creating and Using Classes
Presentation transcript:

Object Oriented Practices This is the Generics 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 Generics enable a form of meta programming. When we can separate the specific portions of an algorithm from the generic portions of an algorithm.

Generics: Methods and Classes This is a bit longer, but it does have quite a bit of information, and lots of conceptual content. Slide discussion: 45 minutes. Code review: 1 hour. Bill Wagner | Software Consultant James Sturtevant | Senior Technical Evangelist

Course Topics Generics 01 | Generics Definition 02 | Generics Guidelines 03 | Generics Practices 04 | Generics Good Outcomes Generics enable a form of meta programming. When we can separate the specific portions of an algorithm from the generic portions of an algorithm.

Generics Defined Bill Wagner | Software Consultant This is a bit longer, but it does have quite a bit of information, and lots of conceptual content. Slide discussion: 45 minutes. Code review: 1 hour. Bill Wagner | Software Consultant James Sturtevant | Senior Technical Evangelist

Defining Generics Generics allow a class or method to specify the types of arguments or return values at the time of instantiation. Read through the definition. Generics allow us to create and algorithms that can be used with multiple (possibly unrelated) types. Generics refers to a technique of writing the code for a class, without specifying the data type that the class works with. You specify the data type when you declare an instance of a generic class. This allows a generic class to be specialized for many different data types without having to rewrite the class. But still get type safe without incurring the cost or risk of runtime casts or boxing operations Code reuse

Language Support for Generics Generic Methods Generic Interfaces Generic Classes Constraints There are four keys we’ll go over in language support.

Generic Methods in Practice Type Parameter for at least one argument, or return value. Sometimes both Example we’ll use: LINQ, System.Linq.Enumerable Other examples: Task.FromResult<T> Explain and discuss each of these advantages. Some methods can be implemented, well, generically. These can be written once, and reused with many different types in a type Safe manner. Remember that

Generic Interfaces in Practice Type Parameter for at least one argument, or return value. Sometimes both Common Contract for many unrelated types Examples: IComparable<T> IEquatable<T> Explain and discuss each of these advantages. Some methods can be implemented, well, generically. These can be written once, and reused with many different types in a type Safe manner. Consider two classic examples: Sort comparisons, and equality comparisons. When you specify the type you then get access to the properties of the time while you are implementing.

Generic Classes in Practice Type Parameter used for some field, or property in the class. Note: If not a data member, consider generic methods Examples: List<T> Task<T> Contrast these with earlier collection examples. Compile time checking on types.

Constraints Constraints define requirements for a type parameter. Specify Interface Support Specify Base class Specify parameterless constructor Specify class Specify struct If we don’t specify anything, type parameters are Where keyword where T: struct The type argument must be a value type. Any value type except Nullable can be specified. SeeUsing Nullable Types (C# Programming Guide) for more information. where T : class The type argument must be a reference type; this applies also to any class, interface, delegate, or array type. where T : new() The type argument must have a public parameterless constructor. When used together with other constraints, the new() constraint must be specified last. where T : <base class name> The type argument must be or derive from the specified base class. where T : <interface name> The type argument must be or implement the specified interface. Multiple interface constraints can be specified. The constraining interface can also be generic. where T : U The type argument supplied for T must be or derive from the argument supplied for U.

Guidelines: Generics Bill Wagner | Software Consultant Section header Let’s look at guidelines for finding and managing generic metaprogramming Bill Wagner | Software Consultant James Sturtevant | Senior Technical Evangelist

Be as generic as possible Start with Specific examples Create Generic versions Learn to separate specific from general algorithms. Discussion points here: . This really takes practice. . We’ll look at some examples from the BCL. Caching interface. Repository – ASP.NET Identity library

Guideline: Delegates for Specific Implementation Consider Defining generic delegates for specific needs We’ll concentrate on this in the next section For now: Delegates can define the specific capabilities. Discussion: The abstract base class combines abstract base classes with interfaces

Generics Practices Bill Wagner | Software Consultant Section header How can we accomplish smart use of abstract classes? Bill Wagner | Software Consultant James Sturtevant | Senior Technical Evangelist

Prefer Generics to Polymorphism Generics are often called for instead of System.Object Create a Type Safe version off a general algorithm With generics, you avoid downcasting. Discussion points here: . Generics provide type safety for collections, and other types. . More often than not, we’d be better off having different instantiations of a collection than using the ultimate base class. . Consider the usage of the type parameter. Did you many any object, or an object of a particular type?

Practice: Specific to Generic Learn from specific algorithms Can you make generic? If so, how? When the opportunity presents itself, do so It’s very hard to find a generic algorithm without specific examples Once you see one or two, consider how it could be generic.

Practice: Leverage Constraints What requirements do you have on Type Parameters Keep them minimal But use them Constraints let you specify your expectations and requirements for type parameters. Good news: you can make sure constraints are satisfied, or it won’t compile Bad news: more constraints limits the types usefulness. Employee – Teacher, TA, Adjunct teacher - all share the same properties and so you can reference them

Generics: Good Outcomes 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: Reuse via Metaprogramming Generics provide type safe reuse. Avoid System.Object When not intended. Avoid Copy / Paste / Modify reuse Reuse via metaprogramming means that the compiler creates the specific versions of eah generic when it is instantiated.

Good Outcome: Minimal Downcasting Generics means that you don’t have to cast System.Object => Specific type Discussion points: . Classes shouldn’t “almost” implement an interface . Classes shouldn’t model “Almost is a”