Slide 1

Slide 1 text

Principles of Object Orientation Frank Neff

Slide 2

Slide 2 text

Don’t be STUPID… • Singleton • Tight Coupling • Untestable • Premature Optimization • Indescriptive Naming • Duplication

Slide 3

Slide 3 text

…GRASP SOLID

Slide 4

Slide 4 text

S.O.L.I.D. • Set of principles defining “good code” • Coined by Robert C. Martin (aka. “Uncle Bob”) • Applicable in every object oriented language • Every character represents a different principle

Slide 5

Slide 5 text

Single Responsibility Principle

Slide 6

Slide 6 text

SRP • “A class should have only one single responsibility” • If a class does two or more different things, split it • Only one reason to change a class

Slide 7

Slide 7 text

SRP

Slide 8

Slide 8 text

SRP

Slide 9

Slide 9 text

Open / Closed Principle

Slide 10

Slide 10 text

OCP • “A class should be open for extension but closed for modification” • Define interfaces or abstract classed which should not change (modification) • Inherit them in concrete classes (extension) • When changes occur, inherit again with different concrete class (extension)

Slide 11

Slide 11 text

OCP

Slide 12

Slide 12 text

OCP

Slide 13

Slide 13 text

OCP

Slide 14

Slide 14 text

Liskov Substitution Principle

Slide 15

Slide 15 text

LSP • “Supertypes can be replaced with Subtypes of them without changing any properties or methods” • If S extends/implements T, all instances of T can be replaced with S • No further changes to S or T should be needed to do so • If X extends/implements S, all instances of T can be replaces with X

Slide 16

Slide 16 text

LSP

Slide 17

Slide 17 text

Interface Segregation Principle

Slide 18

Slide 18 text

ISP • “No client should be forced to depend on methods it does not use” • Split large interfaces into smaller / more specific ones • Think of interfaces as roles or maybe behaviours of an object • Classes can implement multiple interfaces, but should not implement unnecessary methods

Slide 19

Slide 19 text

Dependency Inversion Principle

Slide 20

Slide 20 text

DIP • “High-level modules should not depend on low-level modules. Both should depend on abstractions.” • “Abstractions should not depend on details. Details should depend on abstractions.” • One concrete class should not depend on other classes • Concrete classes should depend on abstracts or interfaces

Slide 21

Slide 21 text

DIP

Slide 22

Slide 22 text

G.R.A.S.P. • Abbrev.: General Responsibility Assignment Software Patterns • Guidelines for assigning responsibility to objects in o.o. design • Applicable in every object oriented language

Slide 23

Slide 23 text

High Cohesion • “High Cohesion is an evaluative pattern that attempts to keep objects appropriately focused, manageable and understandable.” • Defines that a class should only have one responsibility and methods / properties of a class should work together closely • The Single Responsibility Principle ensures high cohesion • Supports low coupling of components

Slide 24

Slide 24 text

Low Coupling • Low Coupling is an evaluative pattern, which dictates how to assign responsibilities to support: • Lower dependency between classes • Lower impact on other classes • Higher reuse potential • Depends on high cohesion

Slide 25

Slide 25 text

Polymorphism • “According to Polymorphism, responsibility of defining the variation of behaviors based on type is assigned to the type for which this variation happens.” • Provision of a single interface to entities of different types • Create an object from subtype, use interface of supertype

Slide 26

Slide 26 text

Controller • “The Controller pattern assigns the responsibility of dealing with system events to a non-UI class that represents the overall system or a use case scenario.” • Responsible for handling a system event • Connection between UI and domain logic

Slide 27

Slide 27 text

Creator • A class responsible for creating objects • Holds and creates instanced of another class • Deals with the interface of that class • Contains initialisation information for the created objects • Also called “Factory Pattern”

Slide 28

Slide 28 text

Indirection • “The Indirection pattern supports low coupling (and reuse potential) between two elements by assigning the responsibility of mediation between them to an intermediate object.” • Object which “mediates” between two others • E.g.: Controller in MVC • Often referred as “Delegation Pattern"

Slide 29

Slide 29 text

Information Expert • “Information Expert is a principle used to determine where to delegate responsibilities.” • A class must contain all information it needs for calculation • See also: “Information Hiding”

Slide 30

Slide 30 text

Protected Variations • “This pattern protects elements from the variations on others by wrapping the focus of instability with an interface and using polymorphism to create various implementations of this interface.” • See also: Open/Closed Principle

Slide 31

Slide 31 text

Pure Fabrication • “A Pure Fabrication is a class that does not represent a concept in the problem domain, specially made up to achieve low coupling, high cohesion, and the reuse potential thereof derived.” • Called “Service” in Domain Driven Design • E.g. an object which is connecting an internal library to a module (so there is no dependency between library and module) • Not to be confused with the Factory Pattern

Slide 32

Slide 32 text

– Robert C. Martin (aka. Uncle Bob) “Of course bad code can be cleaned up. But it’s very expensive.”

Slide 33

Slide 33 text

Thanks Frank Neff Twitter: @frank_neff Blog: frne.github.io Slides: speakerdeck.com/frne