Object Oriented Programming Principles, Practices, and Patterns Brendan Enrick NimblePros...

Preview:

Citation preview

Object Oriented Programming Principles, Practices, and Patterns

Brendan Enrick

NimblePros

benrick@nimblepros.com | @brendoneus

http://brendan.enrick.com/

Thank You!Organizers

Prize Sponsors

Principles Practices Patterns

Object Oriented Principles

Principles

Abstraction Encapsulation

Inheritance Polymorphism

Composition

Abstraction

“obscure unimportant details”

Data Abstraction & Control Abstraction

Control Abstraction

“defined set of operations”

“grouping steps together”

Data Abstraction

“defined set of data”

“grouping data together”

Data Abstraction Example

Car

Engine

Wheels

Windshield

Doors

Abstraction Demo

Encapsulation

“keeping secrets”

“black box”

What is my balance?

Encapsulation Example

Customer Banker

Where did the money come from? How did he know the balance?Did the transaction get recorded?

I would like to withdraw $25.

Two hundred dollars.

Here. (Gives $25)

Encapsulation Demo

Inheritance

“is a”

Inheritance Example

Vehicle

LandVehicle

Car Bicycle

WaterVehicle

Boat

Inheritance Example

WindowLookThrough()

SlidingWindowLookThrough()

Open()Close()

SkylightLookThrough()

Inheritance Demo

Polymorphism

“works like a”

Composition

“has a”

Composition Example

• Employee has a manager.• Order has a customer.

Polymorphism

AB

C

Polymorphism Example – Play Catch

CatcherThrower

Baseball

Football

Bowlingball

Polymorphism Demo

Object Oriented Practices

SOLID

• Steve Smith will be discussing these later.

Favor Composition Over Inheritance

• Encourages less coupling.

No circular dependencies

• Dependency structures must always be a DAG.

Stable Dependencies Principle

• Depend only on that which is more stable than yourself.

• Stability == Less changing

Abstract Classes are Stable

• A package consisting of only abstract classes is very stable.

• Implementation details change far more than interfaces.

Use Patterns

• Use proven patterns to solve design problems.

• Most problems are solved using variations of different problems.

Paradigm Segregation

• When connecting two different programming paradigms, use an interface layer to segregate them.

Object Oriented Patterns

Encapsulating Algorithms

Strategy Pattern

• Encapsulates algorithms and makes them interchangeable.

• Flexible• Composition-based

Template Method Pattern

• Outlines an algorithm and allows subclasses to define specifics.

• Great code reuse.• Inheritance-based

Strategy Pattern

• Through the use of encapsulation, abstraction, polymorphism, and composition, defines a set of interchangeable algorithms.

Strategy Pattern

• Use polymorphism to define each algorithm and use them interchangeably.

Strategy Pattern Example

Basketball Player Selection

Height Speed Shooting

Strategy Pattern Demo

Template Method Pattern

• Through encapsulation, abstraction, polymorphism, and inheritance, allows a subclass to redefine certain steps of an algorithm without changing the structure of the algorithm.

Template Method Pattern

Pizza Making

• Knead Dough• Add Toppings• Bake• Cut Slices

Pie Making

• Form Crust• Add Filling• Bake• Cut Slices

Template Method Pattern

Crusted Baking

• Prepare Crust• Add Ingredients• Bake• Cut Slices

Template Method Pattern Demo

Recommended