front end architecture patterns

84

Upload: oleksandr-tryshchenko

Post on 21-Jan-2018

1.328 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Front end architecture patterns
Page 2: Front end architecture patterns

INTRODUCTION TOFRONT-END ARCHITECTURE PATTERNS

Oleksandr TryshchenkoSenior Front-end Developer, DataArttryshchenko.com

Page 3: Front end architecture patterns

Agenda

• Classic Back-End architecture patterns• Contemporary Front-End architecture patterns

18 May 2017 3

Page 4: Front end architecture patterns

History

18 May 2017 4

Page 5: Front end architecture patterns

Web 1.0

18 May 2017 5

• Server-side rendering• Static pages• ActiveX, java applets

Page 6: Front end architecture patterns

JavaScript Usage In 2004

Usage, %

Snow Effect Blinking Effect Something Useful

18 May 2017 6

Page 7: Front end architecture patterns

JavaScript Usage In 2004

18 May 2017 7

Page 8: Front end architecture patterns

Why didn’t JS Evolve Earlier?

• Limited browser API’s• Lack of protocols for client-server communication• Slow and expensive internet connection

18 May 2017 8

Page 9: Front end architecture patterns

Server-side Rendering

Browser Rendering Layer

LogicLayer Data Layer

18 May 2017 9

Client Server

New HTML document – Page needs to be reloaded.

Page 10: Front end architecture patterns

User gets page

User performs action (clicks link, submits

form, etc)

Browser sends request to server

Server handles request and

generates new page

Browser loads new page

18 May 2017 10

SERVER-SIDE RENDERING

Page 11: Front end architecture patterns

User gets page

User performs action (clicks link, submits

form, etc)

Browser sends request to server

Server handles request and

generates new page

Browser loads new page

18 May 2017 11

SERVER-SIDE RENDERING

Page 12: Front end architecture patterns

What Changed?

18 May 2017 12

Page 13: Front end architecture patterns

Game Changer: AJAX

• Microsoft Outlook Web Access• Gmail• Google Maps

18 May 2017 13

Page 14: Front end architecture patterns

Game Changer: AJAX

18 May 2017 14

Page 15: Front end architecture patterns

Game Changer: 3G, Smartphones

• iOS• Android• Windows Phone

18 May 2017 15

Page 16: Front end architecture patterns

Client-side Rendering

• One common way to interact with a server for different platforms.

• Performance expenses moved to client’s device.• Separated delivery process.

18 May 2017 16

Page 17: Front end architecture patterns

Client-side Rendering

Browser Front-EndApplication

Data Exchange

LayerLogic Layer Data Layer

18 May 2017 17

Client Server

Returns XML / JSON with data

Updates DOM Tree

Page 18: Front end architecture patterns

Front-End Frameworks

• Huge amount of code which developers must support• Spaghetti out of callbacks and events• High coupling• Awful encapsulation

18 May 2017 18

Page 19: Front end architecture patterns

Model View Controller

18 May 2017 19

Page 20: Front end architecture patterns

MVC (Model View Controller)

18 May 2017 20

Model

ControllerView

Page 21: Front end architecture patterns

MVC?

18 May 2017 21

Page 22: Front end architecture patterns

JavaScript is Asynchronous

18 May 2017 22

Page 23: Front end architecture patterns

Sync / Async

18 May 2017 23

Page 24: Front end architecture patterns

Synchronous

getData() modifyData() displayData()

18 May 2017 24

Page 25: Front end architecture patterns

Asynchronous

getData() WHATEVERNEXT …

18 May 2017 25

modifyData() displayData() …

Waiting For Server Response

Page 26: Front end architecture patterns

18 May 2017 26

Page 27: Front end architecture patterns

18 May 2017 27

Page 28: Front end architecture patterns

Workarounds

• Callbacks• Promises• Generators• Async / Await• Streams (ReactiveX)*

18 May 2017 28

Page 29: Front end architecture patterns

18 May 2017 29

Callbacks

Page 30: Front end architecture patterns

18 May 2017 30

Promises

Page 31: Front end architecture patterns

18 May 2017 31

ES6 Generators

Page 32: Front end architecture patterns

18 May 2017 32

Async / Await

Page 33: Front end architecture patterns

Data Exchange

• “Vertical” (through components’ composition)• “Horizontal” (through singleton)

18 May 2017 33

Page 34: Front end architecture patterns

Data Exchange Using Components Composition

Main component (Declares variable)

Widget 1(Uses variable)

Widget 2(Uses variable)

Widget 3(Uses variable)

Header Component

(Uses variable)

18 May 2017 34

Page 35: Front end architecture patterns

Data Exchange Using Components Composition

Main component (Declares variable)

Widget 1(Uses variable)

Widget 2(Uses variable)

Widget 3(Uses variable)

Header Component

(Uses variable)

18 May 2017 35

Emits Change Event + Value (any of the boxes)

Emits Change Event + Value

Page 36: Front end architecture patterns

Data Exchange Using Components’ Composition

Main component (Declares variable)

Widget 1(Uses variable)

Widget 2(Uses variable)

Widget 3(Uses variable)

Header Component

(Uses variable)

18 May 2017 36

Checks changes and updates components

Checks changes and updates components (each one)

Page 37: Front end architecture patterns

Data Exchange Using Singleton

Maincomponent

(Uses variable)

Widget 1(Uses variable)

Widget 2(Uses variable)

Widget 3(Uses variable)

Header Component

(Uses variable)

18 May 2017 37

Data Service (Declares variable)

Page 38: Front end architecture patterns

Difference?

18 May 2017 38

Page 39: Front end architecture patterns

Data Exchange Using Singleton

18 May 2017 39

App User Data Service

Transactions Service

Products Service

Contacts Service

etc

Page 40: Front end architecture patterns

Smart / Dumb Components

ContactsComponent(Gets Data)

DumbRepresentation

Layer

TransactionsComponent(Gets Data)

DumbRepresentation

Layer

18 May 2017 40

Page 41: Front end architecture patterns

Event-driven Architecture

• Many types of user input• Messages from different system components• Timeouts / intervals

18 May 2017 41

Page 42: Front end architecture patterns

Model View ViewModel

18 May 2017 42

Page 43: Front end architecture patterns

MVVM (Model View ViewModel)

View ViewModel Model

18 May 2017 43

Data BindingWrite

Read

Page 44: Front end architecture patterns

Why MVVM?

• Fast interaction with representation layer• Is suitable for events-oriented environment• Close enough to classical MVC

18 May 2017 44

Page 45: Front end architecture patterns

MVVM (Model View ViewModel)

18 May 2017 45

Page 46: Front end architecture patterns

18 May 2017 46

Component / Template Communication (Angular 2)

Page 47: Front end architecture patterns

18 May 2017 47

Component / Template Communication (Angular 2)

Page 48: Front end architecture patterns

18 May 2017 48

Component / Template Communication (Angular 2)

Page 49: Front end architecture patterns

18 May 2017 49

Component / Template Communication (Angular 2)

Page 50: Front end architecture patterns

What’s Wrong With it?

18 May 2017 50

Page 51: Front end architecture patterns

What’s Wrong With it?

18 May 2017 51

• State management• Data mutations

Page 52: Front end architecture patterns

Flux

18 May 2017 52

Page 53: Front end architecture patterns

Flux

Action Dispatcher Store View

18 May 2017 53

User input or event

Page 54: Front end architecture patterns

Flux isn’t only about Front-End

18 May 2017 54

Page 55: Front end architecture patterns

Shortly

• Multiple stores• Store has logic• State can be mutable

18 May 2017 55

Page 56: Front end architecture patterns

Flux Application Demo

18 May 2017 56

HTTPS://GITHUB.COM/SCOTCH-IO/REACT-FLUX-CART

Page 57: Front end architecture patterns

Flux

18 May 2017 57

Page 58: Front end architecture patterns

Redux

18 May 2017 58

Page 59: Front end architecture patterns

Redux is not a pattern itself!

18 May 2017 59

Page 60: Front end architecture patterns

Redux is a library. A library which partly implements Flux.

18 May 2017 60

Page 61: Front end architecture patterns

Redux

View

Actions

ReducerStore

State

18 May 2017 61

Page 62: Front end architecture patterns

What is State?

18 May 2017 62

Page 63: Front end architecture patterns

What is Reducer?

18 May 2017 63

• It’s a pure function

Page 64: Front end architecture patterns

Shortly

• State tree instead of multiple states• Pure function as reducers• Time Travel• Redux has no dispatcher entity, its store exposes API to

dispatch actions• The most logical are in reducers

18 May 2017 64

Page 65: Front end architecture patterns

What Does React Components Look Like

18 May 2017 65

HTTPS://GITHUB.COM/QUANGBUULE/REDUX-EXAMPLE

Page 66: Front end architecture patterns

Functional Programming

• No side-effects.• Immutable data structures.

18 May 2017 66

Page 67: Front end architecture patterns

Immutability

• No function should change anything outside it.• Every function should return a new value.• Function shouldn’t modify it’s arguments.

18 May 2017 67

Page 68: Front end architecture patterns

18 May 2017 68

Mutable

Page 69: Front end architecture patterns

18 May 2017 69

Mutable

Page 70: Front end architecture patterns

18 May 2017 70

Mutable

Page 71: Front end architecture patterns

18 May 2017 71

Mutable

Page 72: Front end architecture patterns

18 May 2017 72

Immutable

Page 73: Front end architecture patterns

18 May 2017 73

Immutable

Page 74: Front end architecture patterns

What Are Pure and Impure Functions

18 May 2017 74

Page 75: Front end architecture patterns

Redux

18 May 2017 75

Page 76: Front end architecture patterns

18 May 2017 H T T P : / / W W W . J C H A P R O N . C O M / 2 0 1 5 / 0 8 / 1 4 / G E T T I N G - S T A R T E D - W I T H - R E D U X / 76

Page 77: Front end architecture patterns

Different Patterns – Different Workarounds

• MVC• MVVM• FLUX• REDUX

18 May 2017 77

Object Oriented Programming

Functional Programming

Page 78: Front end architecture patterns

But Technically You Can…

• MVC• MVVM• FLUX• REDUX

18 May 2017 78

Object Oriented Programming

Functional Programming

Page 79: Front end architecture patterns

And What Should We Use?

18 May 2017 79

Page 80: Front end architecture patterns

Components

• Modular Architecture• Scalable

18 May 2017 80

Page 81: Front end architecture patterns

18 May 2017 F O O T E R H E R E 81

Page 82: Front end architecture patterns

18 May 2017 F O O T E R H E R E 82

Page 83: Front end architecture patterns

What’s the Idea?

18 May 2017 83

Page 84: Front end architecture patterns

Questions?

18 May 2017 84