OOP + Design Patterns

=

Happy Developers

Jason Adams

@jasontheadams

Big times in

WordPress Land

1. PHP 5.6+

2. Gutenberg

3. No more IE 10

4. REST API

Time to use the features we've only stared longingly at!

Let's talk about some common problems

"It took me 4 hours to find this stupid bug."

"I'm... not even sure how this function works anymore."

"How did it even get that value!? That shouldn't be possible!"

"So where should I make this thing? Is here fine?"

Design Patterns

Yes,

Someone else already had that problem and, yes, they already solved it.

What Design Patterns are and are not

Are Not:

Are:

  • A framework such as WordPress
  • A plug-n-play library such as React
  • Something you can copy and paste

Design
Pattern
Categories

1. Creational

2. Structural

3. Behavioral

Let's dive right in!

First up:

Value Objects

A scalar value is more than a value

Percentages

  • Between -1 and 1

Temperature

  • Has a unit (F/C/K)

Post Status

  • Limited to fixed list: private, published, draft...

Key takeaways for
Value Objects

  • Each Value Object instance should represent a single (scalar) value.

  • Keep the value property private.
  • Store everything you need to make your value meaningful.

  • Protect the value’s integrity.

Next up:

Singletons

Singletons

A common pattern in WordPress, especially in plugins, where the class only allows one instance of itself to exist. It looks so nifty...

BUT!

Singletons are nifty, but they aren't SOLID

Single Responsibility

Open/Close 

Liskov Substitution

Interface Segregation

Dependency Inversion

Key takeaways for
Singletons

  • Don't use them.

  • Use a Factory to manage your instance or Dependency Injection

Alain Schlesser has a great article on this with many well laid out alternatives:

https://alainschlesser.com/singletons-shared-instances

Next up:

Abstract Factory

Case Study
The wild west of chair sales

Key takeaways for
Abstract Factory

  • Let the factory manage the conditions of the object creation.

  • Use the same interface for the classes that may be returned.
  • Don’t mix creation with implementation.

Conclusion:
Design Patterns are your friend and mentor

Thanks!

Jason Adams

@jasontheadams

Get the slides

OOP + Design Patterns = Happy Developer

By Jason Adams

OOP + Design Patterns = Happy Developer

  • 745