Slide 1

Slide 1 text

Introduction to Design Patterns June 7th, 2019 Kenju Wagatsuma Cookpad Inc.

Slide 2

Slide 2 text

TOC ● Introduction to Design patterns ● Design patterns examples ● Pattern Language ● Resources for learning

Slide 3

Slide 3 text

Introduction to Design patterns

Slide 4

Slide 4 text

What is Design patterns? ● reusable solutions to specific problems in software development ○ … is not a silver bullet to all problems ○ … is framework agnostic ○ … was firstly introduced by so-called “Gang of Four” in 1994 ■ Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides

Slide 5

Slide 5 text

How Design patterns used? patterns Definition Example Creational patterns create objects Factory method / Builder / Singleton / Prototype / Lazy initialization Structural patterns relationships among objects Adapter / Bridge / Composite / Decorator / Facade / Proxy Behavioral patterns how each objects behave Command / Iterator / Observer / Strategy / Visitor

Slide 6

Slide 6 text

Why to use Design patterns? ● lingua franca of software development ○ “This class can be refactored with Decorator pattern...” ● proven ways for re-architecturing for specific cases ○ many programmers have used Design patterns for few decades...

Slide 7

Slide 7 text

Design patterns are just tools ● 🙅 It is all or nothing ○ “Singleton pattern is good (or bad)” ○ “You should use decorator patterns never” ● 🙆 It solves specific problems ○ “Singleton pattern is good (or bad) when …” ○ “You should use decorator patterns to solve this problem because...”

Slide 8

Slide 8 text

Design patterns Examples

Slide 9

Slide 9 text

Factory pattern function TextCreative(props) { this.id = props.id; this.text = props.text; } function ImageCreative(props) { this.id = props.id; this.image = props.image; } function CreativeFactory() {} CreativeFactory.prototype.create = function(props) { if (props.type === ‘text’) { return new TextCreative(props); } else if (props.type === ‘image’) { return new ImageCreative(props); } else { throw new Error(`${props.type} is invalid`); } }

Slide 10

Slide 10 text

Decorator pattern function Creative(props) { this.id = props.id; } /* define decorators */ function addPopup(creative) { creative.onLoad(function() { popUp.show(‘creative loaded’); }); } function addImage(creative, imageUrl) { creative.loadImage(imageUrl); } const creative = new Creative({ id: ‘foo-bar’, }); const decorators = [ addPopup, addImage, ]; decorators.forEach((decorator) => { decorator(creative); });

Slide 11

Slide 11 text

Command pattern const creatives = ['cr1', 'cr2', 'cr3']; function SingleSelectionCommand() { const idx = Math.floor( Math.random() * creatives.length); return creatives[idx]; } function MultipleSelectionCommand(props) { const selected = new Set(); for (let i = 0; i < props.count; i++) { selected.add(SingleSelectionCommand()); } return Array.from(selected); } /** define command executor */ function CreativeSelector(props) {} CreativeSelector.prototype.execute = function( command, props, ) { return command(props); } /** execute commands */ const selector = new CreativeSelector(); const single = selector.execute(SingleSelectionCommand, {}); const multi = selector.execute(MultipleSelectionCommand, { count: 2, });

Slide 12

Slide 12 text

Pattern Language

Slide 13

Slide 13 text

👈 Please have a look at this great answer to the Quora’s question

Slide 14

Slide 14 text

Pattern Language Each “patterns” consists of... Pattern Name Context Problem Solution

Slide 15

Slide 15 text

Resources for your learning

Slide 16

Slide 16 text

“GoF” ● Creational ○ Abstract Factory/Builder/Factory Method/Prototype/Singleton ● Structural ○ Adapter/Bridge/Composite/Decorator/Facad e/Flyweight/Proxy ● Behavioural ○ Chain of Responsibility/Command/Interpreter/Iterato r/Mediator/Memento/Observer/State/Strate gy/Template Method/Visitor ※ some patterns can be outdated or too common nowadays

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

FREE (online version) for example... ● Command ● Observer ● Prototype ● Singleton ● State ● Component ● Event Queue ● Object Pool ● Dirty Flag ● etc.

Slide 19

Slide 19 text

FREE (online version) for example... ● Constructor ● Module ● Revealing Module ● Singleton ● Observer ● Mediator ● Prototype ● Command ● Facade ● etc.

Slide 20

Slide 20 text

Thank you!