Skip to content

Instantly share code, notes, and snippets.

@cdanyl
cdanyl / simple_strategy_pattern.js
Last active October 1, 2017 08:37
Strategy Design Pattern in JavaScript
/*
* Strategy Pattern in Javascript
* Can replace a naive switch case
* Essentially it is a way to evaluate an input at runtime and execute predefined code based off of that input.
*
* Encapsulates what varies
* Favor composition over inheritance
* Program to interfaces, not implementations
* Another benefit of the Strategy pattern is that it can hide complex logic or data that the client doesn't need to know about.
*/
@cdanyl
cdanyl / strategy_pattern.js
Created September 27, 2017 19:27
OOP version VS. FP version of Javascript implementation of Strategy Pattern
/*
* I have a USER model that comes from two different sources: Database and Web services.
* When receiving USER model from these sources, they both have different form,
* but basically contains almost the same data.
* I want to parse this external data forms into transitional data form that my application understands.
* This transitional data form is called Correspondence model (in MDD theory).
* When I have both external models in Correspondence model, I want to merge them and map the result to the API model.
* The API model is exposed from the application through the JSON REST API.
*/
// Procedural style
function validate(values) {
if (!values.first) {
return false;
}
return values.last;
}
@cdanyl
cdanyl / prototypes-delegation-composition.es6.js
Created May 22, 2017 19:44
Delegation using Object.create() VS. Composing Behavior through Mixins (the best)
/**
* Delegation using Object.create() (Prototype delegation)
* This pattern improves things by:
* - Delegation builds functionality by composing (via links) regular objects, not by using constructor functions.
* - Delegation uses more specific method names on the delegator objects that are reflective of the actions they perform.
* Inheritance keeps method names fairly generic, as sub-classes tend to re-implement specific behavior that override
* the base classes method.
* - State is typically maintained at the delegator level, not in the delegatee objects.
*/
@cdanyl
cdanyl / builderPattern.es6.js
Created May 17, 2017 18:32
General helper to create a builder pattern in Javascript
// General helper to create the builder pattern automatically
const make = target => ({
with(source) {
return Object.assign(target, source);
},
});
// Let's make a spaghetti
const pasta = { ingredients: [] };
/*
UI state with data
Software programs are just transformations of data.
Given input A, the program P produces output B.
It's easy to see this pattern in small Unix command-line tools,
but you could also apply it to larger systems, and why not a user-interface?
*/
const State = {};
@cdanyl
cdanyl / compositioninjavascript.js
Created March 16, 2017 11:46
* Demonstrates using object composition * instead of inheritance.
const show = function (output) {
if (typeof output !== 'undefined') {
console.log(output);
}
};
// composes a vehicle object
const makeVehicle = function (spec) {
const vehicle = {};
@cdanyl
cdanyl / 01-prototype-pattern.js
Created March 1, 2017 22:12 — forked from tomturton/01-prototype-pattern.js
Comparison of JavasScript object 'instantiation' methods
/*
'Prototype' pattern
-------------------
Traditional pattern to emulate classical inheritance in JavaScript
Pros:
- already used by majority of OO JS developers
- performs well
@cdanyl
cdanyl / command.js
Created March 1, 2017 22:03
JavaScript Patterns
// in Javascript, is completely pointless.
// Structure and example
var commandPattern = (function(){
var commandSet = {
doSomething: function(arg1, arg2) {
return "This is argument 1 "+ arg1 + "and this is arg 2 "+ arg2;
},
doSomethingElse: function(arg3) {
return "This is arg 3 "+arg3;
},