Slide 1

Slide 1 text

Why OOP? What's wrong with procedural code? -- "structured programming" Why Object Oriented Programming?

Slide 2

Slide 2 text

Why OOP? OOP benefits Developers We naturally think/talk in terms of objects, attributes, behaviors, interactions, events - Should present a useful Model of the real world - Expressive: exposes how things work in plain language - Helps reason about your problem domain - Handles increasing complexity Samples: github / Cart

Slide 3

Slide 3 text

What is OOP? Using Classes: Encapsulation, Inheritance, Polymorphism (blah blah…) OOP Principles 1) Single Responsibility 2) Encapsulation 3) Semantic Modeling 4) Loose Coupling 5) Types of Objects

Slide 4

Slide 4 text

OOP Principles 1) Single Responsibility: ● Small, single task Classes + Small single-task Methods ● A class should have only one reason to change ● Separate Queries (->get*) & Commands (->update*) ● Use Factory classes ( CartFactory::Create() )

Slide 5

Slide 5 text

OOP Principles 2) Encapsulation: ● Expose "Public API" for class, Hide implementation -- default to ‘protected’ not public ● Self-validating objects

Slide 6

Slide 6 text

OOP Principles 3) Semantic Modeling: ● Semantic Naming (avoid names like "xxxManager") ● Model Relationships ("Is-A", "Has-A" etc) Favor composition over inheritance -- unless you are building a framework

Slide 7

Slide 7 text

OOP Principles 4) Loose Coupling: ● Modules -- high cohesion, internal dependencies ● Use interfaces // Type Hinting ● Dependency Inversion: Constructor/Setter injection ● Use Events // Commands // Services

Slide 8

Slide 8 text

OOP Principles 5) Types of Objects: ● Entities -- have an identity and a lifecycle ● Value Objects -- descriptive, immutable data objects ● Aggregates -- Composite Objects w/Root Entity ● Collections & Repositories