38
OOP

Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

OOP

Page 2: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Object oriented programming

OOP

Inheritance

Polymorphism

Encapsulation

Page 3: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Class concepts

• Classes can contain:

• Constants

• Delegates

• Events

• Fields

• Constructors

• Destructors

• Properties

• Methods

• Nested classes

Page 4: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Encapsulation

• Public The type or member can be accessed by any other code in the same assembly or another assembly that references it.

• Private The type or member can be accessed only by code in the same class or struct.

• Protected The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.

• Internal The type or member can be accessed by any code in the same assembly, but not from another assembly.

Page 5: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Access modifiers

Page 6: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Access modifiers. Combined.

Page 7: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Access modifiers. Protected internal.

Page 8: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Access modifiers. Protected internal.

Page 9: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Access modifiers. Protected internal.

Page 10: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Access modifiers. Auto-property.

Page 11: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Access modifiers. Best practice

• Reasons to prefer public property over public field

• Serialization

• Reflection

• Testing

• Debugging

• Logging

• Used for data binding

Page 12: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Useful keywords

Readonly

declaration or in a constructor

Value is set when instance is created

Const

initialized at the declaration

Value is set once before compiling

Page 13: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Inheritance & Polymorphism

• No multiple inheritance in C#

• Classes can be inherited

• Interfaces can be implemented

Page 14: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Interfaces

• “Can – do” logics

• Interfaces can contain:

– Properties

– Events

– Methods

– Indexers

• Always public

• Cannot be initiated directly

• Classes can implement one or more interfaces

Page 15: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Abstract classes

• “Is a” – logics

• Can contain whatever a usual class can

• Cannot be initialized directly

• Class can inherit only one

• abstract class

Page 16: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Abstract classes vs interfaces

Feature Interface Abstract class

Multiple inheritance A class may inherit several interfaces.

A class may inherit only one abstract class.

Default implementation An interface cannot provide any code, just the signature.

An abstract class can provide complete, default code and/or just the details that have to be overridden.

Access Modfiers An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public

An abstract class can contain access modifiers for the subs, functions, properties

Page 17: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Abstract classes vs interfaces

Feature Interface Abstract class

Core VS Peripheral Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface

An abstract class defines the core identity of a class and there it is used for objects of the same type.

Homogeneity If various implementations only share method signatures then it is better to use Interfaces.

If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.

Speed Requires more time to find the actual method in the corresponding classes

Fast

Page 18: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Abstract classes vs interfaces

Feature Interface Abstract class

Adding functionality (Versioning)

If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.

If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.

Fields and Constants No fields can be defined in interfaces

An abstract class can have fields and constrants defined

Page 19: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Overriding

Page 20: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Overriding

Page 21: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Inheritance

Page 22: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Composition

Page 23: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Composition over inheritance

• Prefer composition over inheritance

• do not use a compose-always approach

• it's easy to change behavior on the fly with Dependency Injection / Setters with composition

• Inheritance is more rigid

Page 24: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Composition over inheritance

• Inheritance acid rule:

Does TypeB want to expose the complete interface (all public methods no less) of TypeA such that TypeB can be used where TypeA is expected?

• Prefer composition

Does TypeB only want only some/part of the behavior exposed by TypeA?

Page 25: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Multiple interfaces implementation

Page 26: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Implicit implementation

Page 27: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Explicit implementation

Page 28: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Explicit vs implicit implementation

• Implicit always public

• Explicit always private

• No virtual for explicit

• No abstract for explicit

Page 29: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Design patterns

• In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern isn't a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations.

Page 30: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Singleton

Page 31: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Lazy initialization

Page 32: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Abstract factory

Page 33: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Other interesting patterns

These patterns you will definitely need:

- Strategy

- Decorator

- Prototype

- Builder

- Facade

- Dependency injection

- Mediator

Page 34: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Generics

Powerful tool since c# 2.0

Point classes:

• Should contain values with floating points

• Should contain integer values

• Should log values every time the coordinates are changed

Page 35: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Generics

Page 36: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Generics

Page 37: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

Generics with where

Page 38: Object oriented programming in .net o.starosotskaya.pdf · Abstract classes vs interfaces Feature Interface Abstract class Core VS Peripheral Interfaces are used to define the peripheral

business, evolved.