Top Banner
Principles of Object Oriented Programming David Giard MCTS, MCSE, MCDBA, MCSD DavidGiard.com
58
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Intro to object oriented programming

Principles of Object Oriented ProgrammingDavid GiardMCTS, MCSE, MCDBA, MCSD

DavidGiard.com

Page 2: Intro to object oriented programming

nPlus1.org

nPlus1.org is a site dedicated to helping Architects, aspiring Architects and Lead Developers learn, connect and contribute.

http://nplus1.org

Page 3: Intro to object oriented programming

Agenda

The ChallengeThe BasicsBringing Order To ChaosGuiding Principles

Page 4: Intro to object oriented programming

The Challenge

Page 5: Intro to object oriented programming

The Complexity of software derives from 4 elements:

Complexity of the problem domainDifficulty to manage the development processFlexibility possible through softwareSoftware problems often model discrete systems

Page 6: Intro to object oriented programming

How do you write great software every time??

Page 7: Intro to object oriented programming

Good question… lots of answers!

The customer–friendly programmer says:

“Great software always does what the customer wants it to. So even if the customer thinks of new ways to use the software, it doesn’t break or give them unexpected results.”

Page 8: Intro to object oriented programming

Good question… lots of answers!

The design–guru programmer says:

“Great software is when you use tried-and-true design patterns and principles. You’ve kept your objects loosely coupled, and your code opens for extension but closed for modification. That also helps keep the code more usable, so you don’t have to rework everything to use parts of your application over and over again.”

Page 9: Intro to object oriented programming

Good question… lots of answers!

The object-oriented programmer says:

“Great software is code that is object-oriented. So there's not a bunch of duplicate code, and each object pretty much controls its own behavior. It’s also easy to extend because your design is really solid and flexible.”

Page 10: Intro to object oriented programming

Make sure your software does what the customer wants

it to do.

Apply basic OO principles to add

flexibility

Strive for a maintainable,

reusable design

Answer: Simplify ComplexityAnswer: Eschew Obfuscation

Page 11: Intro to object oriented programming

The Basics

Page 12: Intro to object oriented programming

The BasicsObjectsClasses

MembersPropertiesMethodsEventsConstructors

InstancesMessage Passing

Page 13: Intro to object oriented programming

Defining Classesclass Name:BaseType{ // Members}

class MyType{ public String SomeString; public Int32 x; public Int32 y; public void DoSomething()

{…}

}

Page 14: Intro to object oriented programming

Class MembersFields

The state of an objectProperties

Also maintain stateMethods

ConstructorsFunctionsProperties (smart fields)

Members come in two basic formsInstanceStatic

Page 15: Intro to object oriented programming

FieldsMaintain state of an object

Accessing a field

class Point{ public Int32 X; public Int32 Y; private In32 _x;}

Point p = new Point();p.X = 100;p.Y = 200;Console.WriteLine(p.X);

Page 16: Intro to object oriented programming

PropertiesMethods that look like fields (smart fields)

Can have read-only or write-only properties

class Point{ Int32 _x; Int32 _y; public Int32 X { get{return _x;} set{_x = value;} } public Int32 Y { get{return _y;} set{_y = value;} }}

Page 17: Intro to object oriented programming

Methodsclass MyType{ public Int32 SomeMethod() { Int32 z = 0; // Code omitted… return z; }

public static void StaticMethod() { // Do something }}

MyType t = new MyType();t.SomeMethod();

Page 18: Intro to object oriented programming

Method Parametersclass MyType{ public Int32 AddNumbers(Int32 x, Int32 y) { Int32 z = x + y; return z; }

}

MyType t = new MyType();t.AddNumbers(1,2);

Page 19: Intro to object oriented programming

Types and Instances

Page 20: Intro to object oriented programming

Instantiating an Objectpublic class MyType{ public string MyStringField; public void InstanceMethod() { // Do something }}

MyType o = new MyType();o.MyStringField = “Hello”;String x = o.MyStringField;o.InstanceMethod();

Page 21: Intro to object oriented programming

Static Typespublic static class MyType{

public static void StaticMethod(){

// Do something}

}

MyType.StaticMethod();

Page 22: Intro to object oriented programming

ConstructorsConstructors are used to initialize fields

class Point{ private Int32 _x; private Int32 _y;

public Point() {

// Setup code }

public Point(Int32 xCoordinate, Int32 yCoordinate) { _x = xCoordinate; _y = yCoordinate; }}

Page 23: Intro to object oriented programming

Referencing an object

MyType obj1 = new MyType();obj2 = obj1;obj2.Color = “Red”;Console.WriteLine(obj1.Color)

Objects are reference typesPointer to memory

Page 24: Intro to object oriented programming

Event Handling

C# and VB have built in support for eventsGreat for dealing with objects in an event-driven operating systemMore than one type can register interest in a single eventA single type can register interest in any number of events

Page 25: Intro to object oriented programming

Event Handlingclass MyForm:Form{ MyForm(){ Button button = new Button(); button.Text = "Button"; button.Click += new EventHandler(HandleClick); Controls.Add(button); } void HandleClick(Object sender, EventArgs e){ MessageBox.Show("The Click event fired!"); } public static void Main(){ Application.Run(new MyForm()); } }

Page 26: Intro to object oriented programming

Designing Types: Accessibilities

public – Accessible to allprivate – Accessible to containing classprotected – Accessible to containing or derived classesinternal – Accessible to code in same assembly

Page 27: Intro to object oriented programming

Demo

Page 28: Intro to object oriented programming

Bringing Order to the ChaosObject Oriented Programming

Page 29: Intro to object oriented programming

Bringing Order to the Chaos Concepts of Object Orientation

EncapsulationAbstractionInheritancePolymorphismDecoupling

Page 30: Intro to object oriented programming

Bringing Order to the Chaos

EncapsulationandAbstraction

Page 31: Intro to object oriented programming

Bringing Order to the ChaosThe Role of Encapsulation

Car

EngineTransmission

Axel

Wheel

Wheel

Wheel

Wheel

Axel

Stop()

Drive()

Start()

Page 32: Intro to object oriented programming

Bringing Order to the ChaosThe Role of Abstraction

Layer 1

Module

Module

Layer 2

Module

Module

Layer 3Module Module

Page 33: Intro to object oriented programming

Demo

Page 34: Intro to object oriented programming

Bringing Order to the ChaosThe Role of Inheritance

Vehicle

Automobile

Car Truck

Plane

Jet Bi-Plane

Page 35: Intro to object oriented programming

InheritanceVehicle+Start()+Stop()+Steer()

Plane

Train

Automobile

+Start()+Stop()+Steer()+Fly()

+Start()+Stop()+Steer()+BlowHorn()

+Start()+Stop()+Steer()+Honk()

Page 36: Intro to object oriented programming

PolymorphismSportPlayerPlay()

BaseballPlayerPlay()Catch()

Catcherplay()Catch()

Outfielderplay()Catch()

FootballPlayerPlay()

ReceiverPlay()

Page 37: Intro to object oriented programming

Designing Types: PolymorphismUse the virtual keyword to make a method virtualIn derived class, override method is marked with the override keywordExample

ToString() method in Object class

Example derived class overriding ToString()

public virtual string ToString();

class SomeClass:Object{ public override String ToString(){ return “Some String Representing State”; }}

Page 38: Intro to object oriented programming

Demo

Page 39: Intro to object oriented programming

InterfacesDefine public membersNo ImplementationYour types can implement interfacesMust implement all methods in the interface

interface IName{ // Members}

Class ClassName : IName{}

Page 40: Intro to object oriented programming

Bringing Order to the ChaosThe Role of Decoupling

Page 41: Intro to object oriented programming

Demo

Page 42: Intro to object oriented programming

UML and Class Diagrams

Airplane

speed: int

getSpeed(): int

setSpeed(int)

Class Diagram

Member Variables

name:type

Name

Methods

name(parameters the method uses): return type

Page 43: Intro to object oriented programming

Visual Studio Class Designer

Page 44: Intro to object oriented programming

Demo

Page 45: Intro to object oriented programming

Guiding Principles

Page 46: Intro to object oriented programming

SOLID Principles

Single responsibilityOpen CloseLiskov Substitution.Interface Segregation Dependency Injection

Page 47: Intro to object oriented programming
Page 48: Intro to object oriented programming
Page 49: Intro to object oriented programming
Page 50: Intro to object oriented programming

Liskov substitution

public class Rectangle{ public virtual int Height { get; set; } public virtual int Width { get; set; }

public int Area() { return Height * Width; }}

Page 51: Intro to object oriented programming

Liskov substitution

rectangle.Height = 4;rectangle.Width = 5;int expectedArea = 20;int actualArea = rectangle.Area();Debug.Assert(expectedArea == actualArea);

Page 52: Intro to object oriented programming

Liskov substitutionpublic class Square : Rectangle { public override int Height { get { return base.Height; } set { base.Height = value; base.Width = value; } } public override int Width { get { return base.Width; } set { base.Width = value; base.Height = value; } }}

Page 53: Intro to object oriented programming

Liskov substitution

Rectangle square = new Square();square.Width = 4;square.Height = 5;int expectedArea = 20;int actualArea = rectangle.Area();Debug.Assert(expectedArea == actualArea);

Page 54: Intro to object oriented programming
Page 55: Intro to object oriented programming

Interface segregation principle

interface IWorker {public void work();public void eat();

}

Page 56: Intro to object oriented programming
Page 57: Intro to object oriented programming

Interface segregation principle

public class Customer { private IFoo _foo; public Customer(IFoo foo) { this._foo = foo; }}

Page 58: Intro to object oriented programming

David GiardDavidGiard.comTechnologyAndFriends.comDavidGiard@DavidGiard.comTwitter.com/DavidGiard

Special thanks to Chris Woodruff