Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add data patterns js #1280

Merged
merged 1 commit into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
var Participant = function (name) {
this.name = name;
this.chatroom = null;
};

Participant.prototype = {
send: function (message, to) {
this.chatroom.send(message, this, to);
},
receive: function (message, from) {
console.log(from.name + " to " + this.name + ": " + message);
}
};

var Chatroom = function () {
var participants = {};

return {

register: function (participant) {
participants[participant.name] = participant;
participant.chatroom = this;
},

send: function (message, from, to) {
if (to) { // single message
to.receive(message, from);
} else { // broadcast message
for (key in participants) {
if (participants[key] !== from) {
participants[key].receive(message, from);
}
}
}
}
};
};

function run() {

var yoko = new Participant("Yoko");
var john = new Participant("John");
var paul = new Participant("Paul");
var ringo = new Participant("Ringo");

var chatroom = new Chatroom();
chatroom.register(yoko);
chatroom.register(john);
chatroom.register(paul);
chatroom.register(ringo);

yoko.send("All you need is love.");
yoko.send("I love you John.");
john.send("Hey, no need to broadcast", yoko);
paul.send("Ha, I heard that!");
ringo.send("Paul, what do you think?", paul);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function Click() {
this.handlers = []; // observers
}

Click.prototype = {

subscribe: function (fn) {
this.handlers.push(fn);
},

unsubscribe: function (fn) {
this.handlers = this.handlers.filter(
function (item) {
if (item !== fn) {
return item;
}
}
);
},

fire: function (o, thisObj) {
var scope = thisObj || window;
this.handlers.forEach(function (item) {
item.call(scope, o);
});
}
}

function run() {

var clickHandler = function (item) {
console.log("fired: " + item);
};

var click = new Click();

click.subscribe(clickHandler);
click.fire('event #1');
click.unsubscribe(clickHandler);
click.fire('event #2');
click.subscribe(clickHandler);
click.fire('event #3');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
function Shop() {
this.construct = function (builder) {
builder.step1();
builder.step2();
return builder.get();
}
}

function CarBuilder() {
this.car = null;

this.step1 = function () {
this.car = new Car();
};

this.step2 = function () {
this.car.addParts();
};

this.get = function () {
return this.car;
};
}

function TruckBuilder() {
this.truck = null;

this.step1 = function () {
this.truck = new Truck();
};

this.step2 = function () {
this.truck.addParts();
};

this.get = function () {
return this.truck;
};
}

function Car() {
this.doors = 0;

this.addParts = function () {
this.doors = 4;
};

this.say = function () {
console.log("I am a " + this.doors + "-door car");
};
}

function Truck() {
this.doors = 0;

this.addParts = function () {
this.doors = 2;
};

this.say = function () {
console.log("I am a " + this.doors + "-door truck");
};
}

function run() {
var shop = new Shop();
var carBuilder = new CarBuilder();
var truckBuilder = new TruckBuilder();
var car = shop.construct(carBuilder);
var truck = shop.construct(truckBuilder);

car.say();
truck.say();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var Factory = function () {
this.createEmployee = function (type) {
var employee;

if (type === "fulltime") {
employee = new FullTime();
} else if (type === "parttime") {
employee = new PartTime();
} else if (type === "temporary") {
employee = new Temporary();
} else if (type === "contractor") {
employee = new Contractor();
}

employee.type = type;

employee.say = function () {
console.log(this.type + ": rate " + this.hourly + "/hour");
}

return employee;
}
}

var FullTime = function () {
this.hourly = "$12";
};

var PartTime = function () {
this.hourly = "$11";
};

var Temporary = function () {
this.hourly = "$10";
};

var Contractor = function () {
this.hourly = "$15";
};

function run() {

var employees = [];
var factory = new Factory();

employees.push(factory.createEmployee("fulltime"));
employees.push(factory.createEmployee("parttime"));
employees.push(factory.createEmployee("temporary"));
employees.push(factory.createEmployee("contractor"));

for (var i = 0, len = employees.length; i < len; i++) {
employees[i].say();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function CustomerPrototype(proto) {
this.proto = proto;

this.clone = function () {
var customer = new Customer();

customer.first = proto.first;
customer.last = proto.last;
customer.status = proto.status;

return customer;
};
}

function Customer(first, last, status) {

this.first = first;
this.last = last;
this.status = status;

this.say = function () {
console.log("name: " + this.first + " " + this.last +
", status: " + this.status);
};
}

function run() {

var proto = new Customer("n/a", "n/a", "pending");
var prototype = new CustomerPrototype(proto);

var customer = prototype.clone();
customer.say();
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var Singleton = (function () {
var instance;

function createInstance() {
var object = new Object("I am the instance");
return object;
}

return {
getInstance: function () {
if (!instance) {
instance = createInstance();
}
return instance;
}
};
})();

function run() {

var instance1 = Singleton.getInstance();
var instance2 = Singleton.getInstance();

console.log("Same instance? " + (instance1 === instance2));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

// old interface

function Shipping() {
this.request = function (zipStart, zipEnd, weight) {
// ...
return "$49.75";
}
}

// new interface

function AdvancedShipping() {
this.login = function (credentials) { /* ... */ };
this.setStart = function (start) { /* ... */ };
this.setDestination = function (destination) { /* ... */ };
this.calculate = function (weight) { return "$39.50"; };
}

// adapter interface

function ShippingAdapter(credentials) {
var shipping = new AdvancedShipping();

shipping.login(credentials);

return {
request: function (zipStart, zipEnd, weight) {
shipping.setStart(zipStart);
shipping.setDestination(zipEnd);
return shipping.calculate(weight);
}
};
}

function run() {

var shipping = new Shipping();
var credentials = { token: "30a8-6ee1" };
var adapter = new ShippingAdapter(credentials);

// original shipping object and interface

var cost = shipping.request("78701", "10010", "2 lbs");
console.log("Old cost: " + cost);

// new shipping object with adapted interface

cost = adapter.request("78701", "10010", "2 lbs");

console.log("New cost: " + cost);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var Mortgage = function (name) {
this.name = name;
}

Mortgage.prototype = {

applyFor: function (amount) {
// access multiple subsystems...
var result = "approved";
if (!new Bank().verify(this.name, amount)) {
result = "denied";
} else if (!new Credit().get(this.name)) {
result = "denied";
} else if (!new Background().check(this.name)) {
result = "denied";
}
return this.name + " has been " + result +
" for a " + amount + " mortgage";
}
}

var Bank = function () {
this.verify = function (name, amount) {
// complex logic ...
return true;
}
}

var Credit = function () {
this.get = function (name) {
// complex logic ...
return true;
}
}

var Background = function () {
this.check = function (name) {
// complex logic ...
return true;
}
}

function run() {
var mortgage = new Mortgage("Joan Templeton");
var result = mortgage.applyFor("$100,000");

console.log(result);
}
Loading