Women Techmakers Berlin
JavaScript Crash Course
Advanced lectures pt. 1
Armagan Amcalar
March 15th, 2018
Armagan Amcalar
Head of Software Engineering @ unu GmbH
Founder @ Lonca Works
dashersw dashersw
This is a human story.
We write code for humans to understand.
That’s why it’s so complicated — it has direct interaction with human psychology.
dashersw
Software development has a very high cognitive load
The job of a software developer is to solve complexity
The software we write grows daily
We hate the code we wrote 6 months ago
But we have to live with it for years to come
dashersw
Code complexity should remain constant even if the code grows
Identification at first sight
What’s the secret?
Only if they knew…
dashersw
It’s about familiarity
Like signature stamps
Like paper money / bills
Do you think machines are intelligent and they can learn?
Do you think they are better than humans at this?
* They are taught to identify patterns
dashersw
pattern ˈpat(ə)n noun
dashersw
dashersw
Keep it simple, stupid
dashersw
Do not repeat yourself
dashersw
dashersw
UML
Class diagrams
Sequence diagrams
dashersw
dashersw
dashersw
dashersw
dashersw
Creational
Structural
Behavioral
class Person {
constructor() {
if (typeof Person.instance === 'object') {
return Person.instance;
}
Person.instance = this;
return this;
}
}
export default Person;
const john = new Person();
const john2 = new Person();
john === john2; // true
class BmwFactory {
create(type) {
if (type === 'X5')
return new Bmw(type, 108000, 300);
if (type === 'X6')
return new Bmw(type, 111000, 320);
}
}
class Bmw {
constructor(model, price, maxSpeed) {
this.model = model;
this.price = price;
this.maxSpeed = maxSpeed;
}
}
export default BmwFactory;
const bmwFactory = new BmwFactory();
const x5 = bmwFactory.create('X5');
const x6 = bmwFactory.create('X6');
//Equipment
class Equipment {
getPrice() {
return this.price || 0;
}
getName() {
return this.name;
}
setName(name) {
this.name = name;
}
}
// --- composite ---
class Composite extends Equipment {
constructor() {
super();
this.equipments = [];
}
add(equipment) {
this.equipments.push(equipment);
}
getPrice() {
return this.equipments.map(equipment => {
return equipment.getPrice();
}).reduce((a, b) => {
return a + b;
});
}
}
class Cabinet extends Composite {
constructor() {
super();
this.setName('cabinet');
}
}
// --- leafs ---
class FloppyDisk extends Equipment {
constructor() {
super();
this.setName('Floppy Disk');
this.price = 70;
}
}
class HardDrive extends Equipment {
constructor() {
super();
this.setName('Hard Drive');
this.price = 250;
}
}
class Memory extends Equipment {
constructor() {
super();
this.setName('Memory');
this.price = 280;
}
}
const cabinet = new Cabinet();
cabinet.add(new FloppyDisk());
cabinet.add(new HardDrive());
cabinet.add(new Memory());
cabinet.price; // 600
class Pasta {
constructor() {
this.price = 0;
}
getPrice() {
return this.price;
}
}
class Penne extends Pasta {
constructor() {
super();
this.price = 8;
}
}
class PastaDecorator extends Pasta {
constructor(pasta) {
super();
this.pasta = pasta;
}
getPrice() {
return this.pasta.getPrice();
}
}
class SauceDecorator extends PastaDecorator {
constructor(pasta) {
super(pasta);
}
getPrice() {
return super.getPrice() + 5;
}
}
class CheeseDecorator extends PastaDecorator {
constructor(pasta) {
super(pasta);
}
getPrice() {
return super.getPrice() + 3;
}
}
const penne = new Penne();
const penneWithSauce = new SauceDecorator(penne);
const penneWithSauceAndCheese = new CheeseDecorator(penneWithSauce);
penne.getPrice(); // 8
penneWithSauce.getPrice(); // 13
penneWithSauceAndCheese.getPrice(); // 16
class ShoppingCart {
constructor(discount) {
this.discount = discount;
this.amount = 0;
}
checkout() {
return this.discount(this.amount);
}
setAmount(amount) {
this.amount = amount;
}
}
function guestStrategy(amount) {
return amount;
}
function regularStrategy(amount) {
return amount * 0.9;
}
function premiumStrategy(amount) {
return amount * 0.8;
}
export { ShoppingCart, guestStrategy, regularStrategy, premiumStrategy };
const guestCart = new ShoppingCart(guestStrategy);
guestCart.setAmount(100);
guestCart.checkout(); // 100
const regularCart = new ShoppingCart(regularStrategy);
regularCart.setAmount(100);
regularCart.checkout(); // 90
const premiumCart = new ShoppingCart(premiumStrategy);
premiumCart.setAmount(100);
premiumCart.checkout(); // 80
dashersw
Armagan Amcalar
examples from https://github.com/fbeline/Design-Patterns-JS