Design Patterns (JS) Flashcards

1
Q

Pattern to solve the problem of having service layers

A

module pattern

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

pattern for overly complicated object interfaces

A

facade pattern

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

pattern for better visibility into state changes

A

observer pattern

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

types of patterns

A

creational
structural
behavioural

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

creational patterns

A

Constructor (js specific)
Module (js specific)
Factory
Singleton

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

structural patterns

A

Decorator
Facade
Flyweight

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

behavioural patterns

A

Command
Mediator
Observer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Ways to create an Object

A
var newObj = {};
var newObj = Object.create(Object.prototype); // will come up around inheritance
var newObj = new Object(); // a little out of favor, use class instead
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Ways of assigning value to objects

A

. notation
obj.param = ‘new value’

[] notation
obj[‘param’] = ‘new value’ //one big benefit is being able to use variables inside []

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

defineProperty on object allows to…

A

add some additional settings to the objects properties, for example if it can be enumerated, configured or writeable

Object.defineProperty (object, ‘param’,{
value: whatevervalue,
writable: false, //can’t overwrite this later with object.param assignment
enumerable: false //won’t be listed among the params of the object
configurable: false //won’t allow later defineProperty amendments
})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Inherit an object from another object

A

var obj1 = {whatever};

var obj2 = Object.create(obj1) //copies obj1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Constructor pattern is used to

A

create new objects with their own object scope

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What happens when you put “new” in front of the function

A

Creates a constructor function: a new object, links to an object prototype, binds “this” to the object scope, returns “this”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Create prototype of the object

A

ObjectName.prototype.methodName = function…

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Why use prototype?

A

To abstract large functions and avoid repeating them every time we create a new instance of the object. Prototype allows to reference these chunky pieces of code in one place without creating a copy of them with each new instance of the object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Module pattern in a nutshell

A

Simple way to encapsulate Methods

Creates a “Toolbox” of functions to use

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

A module pattern at its core is just ____

A

an object literal

18
Q

Unlike a Constructor, Module is rarely ____

A

copied or needed more than 1 instance

19
Q

With Module, we can abstract out ___ work

A

db

20
Q

Best practice with defining methods in Module

A

Declare at the top, then reference at the bottom for readability

21
Q

Using Constructor and Module together

A

Create a Module which accesses data in db, and pass that to an instance of constructor that takes the DB data and creates new object

22
Q

Factory pattern is used for…

A

Simplifying object creation
Creating different objects based on need
Repository creation

23
Q

Singleton pattern is used for…

A

restricting an object to one instance of that object across the app

24
Q

How does the Singleton work…

A

Remembers the last time you used it

Hands the same instance back

25
Q

Structural patterns deal with…

A

relationships of objects - either extend or simplify the functionality

26
Q

Decorator pattern is used for ….

A

adding new functionality to existing object, without being obtrusive

27
Q

Decorator pattern allows for (4)…

A

more complete inheritance
wrap an object
protect existing object
adding extended functionality

28
Q

Facade patterns is used to…

A

provide a simple interface to a complicated system. jQuery is the best example of Facade implementation

29
Q

Flyweight pattern is used to…

A

conserve memory by sharing portions of an object between objects. only useful if you have large number of objects, or if the objects are large (but few)

30
Q

Behavioural patterns are concerned with

A

the communication between objects and the assignment of responsibilities.

Help objects cooperate.
Assign clear hierarchy.
Encapsulate requests.

31
Q

Observer pattern allows to…

A

allows for a collection of objects to watch an object and be notified of changes

32
Q

Why Observer?

A

Allows for a loosely coupled system

One object is the focal point (a little coupled)

33
Q

How does Observer work (example of Task)

A

Task is the Subject

Logging, Notification & Auditing are observer services
task has ObserverList{} and Notify() function that calls functions within observers

34
Q

example structure of files for Task (Observer)

A

task. js that contains task code
main. js that start with the following:

var Task = require(‘./task’);

then create Observers within main.js

then create the ObserverList

35
Q

Mediator pattern…

A

controls communication between objects so neither obj has to be coupled with others

36
Q

Mediator allows for

A

loosely coupled system
one object manages all comms
many to many relationship

37
Q

Command pattern is …

A

encapsulating the calling of a method as an object

38
Q

use Command pattern to…

A

fully decouple the execution from implementation
much less fragile implementations
supports undo operations
supports auditing and logging of operations

39
Q

.call vs .apply

A

apply allows to pass in an array of args

call requires to state each arg

40
Q

make a list of arguments that come after the first argument

A
var args = Array.prototype.slice.call(arguments, 1)
[].slice.call(arguments,1) //simplified version