From 7058b949f8f433210267e4fd67d0d21b8f7b05f2 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 29 Oct 2021 23:46:19 +0500 Subject: [PATCH] add data patterns js --- .../Data Patterns/behaviourl/mediator.js | 57 ++++++++++++++ .../Data Patterns/behaviourl/observer.js | 43 +++++++++++ .../Data Patterns/creational/builder.js | 74 +++++++++++++++++++ .../Data Patterns/creational/factory.js | 54 ++++++++++++++ .../Data Patterns/creational/prototype.js | 35 +++++++++ .../Data Patterns/creational/singelton.js | 25 +++++++ .../Data Patterns/structural/adapter.js | 52 +++++++++++++ .../Data Patterns/structural/facade.js | 48 ++++++++++++ .../Data Patterns/structural/flyweight.js | 73 ++++++++++++++++++ .../Data Patterns/structural/proxy.js | 59 +++++++++++++++ 10 files changed, 520 insertions(+) create mode 100644 Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/behaviourl/mediator.js create mode 100644 Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/behaviourl/observer.js create mode 100644 Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/creational/builder.js create mode 100644 Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/creational/factory.js create mode 100644 Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/creational/prototype.js create mode 100644 Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/creational/singelton.js create mode 100644 Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/structural/adapter.js create mode 100644 Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/structural/facade.js create mode 100644 Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/structural/flyweight.js create mode 100644 Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/structural/proxy.js diff --git a/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/behaviourl/mediator.js b/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/behaviourl/mediator.js new file mode 100644 index 0000000000..861ffd1632 --- /dev/null +++ b/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/behaviourl/mediator.js @@ -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); +} diff --git a/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/behaviourl/observer.js b/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/behaviourl/observer.js new file mode 100644 index 0000000000..6e69db835b --- /dev/null +++ b/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/behaviourl/observer.js @@ -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'); +} diff --git a/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/creational/builder.js b/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/creational/builder.js new file mode 100644 index 0000000000..79a6cd2fd7 --- /dev/null +++ b/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/creational/builder.js @@ -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(); +} diff --git a/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/creational/factory.js b/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/creational/factory.js new file mode 100644 index 0000000000..e2ca26794a --- /dev/null +++ b/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/creational/factory.js @@ -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(); + } +} diff --git a/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/creational/prototype.js b/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/creational/prototype.js new file mode 100644 index 0000000000..b788910ca6 --- /dev/null +++ b/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/creational/prototype.js @@ -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(); +} + diff --git a/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/creational/singelton.js b/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/creational/singelton.js new file mode 100644 index 0000000000..88dfb6f862 --- /dev/null +++ b/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/creational/singelton.js @@ -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)); +} diff --git a/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/structural/adapter.js b/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/structural/adapter.js new file mode 100644 index 0000000000..7e536e0c85 --- /dev/null +++ b/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/structural/adapter.js @@ -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); +} diff --git a/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/structural/facade.js b/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/structural/facade.js new file mode 100644 index 0000000000..fc98ef94b7 --- /dev/null +++ b/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/structural/facade.js @@ -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); +} diff --git a/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/structural/flyweight.js b/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/structural/flyweight.js new file mode 100644 index 0000000000..eee3c791dc --- /dev/null +++ b/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/structural/flyweight.js @@ -0,0 +1,73 @@ + +function Flyweight(make, model, processor) { + this.make = make; + this.model = model; + this.processor = processor; +}; + +var FlyWeightFactory = (function () { + var flyweights = {}; + + return { + + get: function (make, model, processor) { + if (!flyweights[make + model]) { + flyweights[make + model] = + new Flyweight(make, model, processor); + } + return flyweights[make + model]; + }, + + getCount: function () { + var count = 0; + for (var f in flyweights) count++; + return count; + } + } +})(); + +function ComputerCollection() { + var computers = {}; + var count = 0; + + return { + add: function (make, model, processor, memory, tag) { + computers[tag] = + new Computer(make, model, processor, memory, tag); + count++; + }, + + get: function (tag) { + return computers[tag]; + }, + + getCount: function () { + return count; + } + }; +} + +var Computer = function (make, model, processor, memory, tag) { + this.flyweight = FlyWeightFactory.get(make, model, processor); + this.memory = memory; + this.tag = tag; + this.getMake = function () { + return this.flyweight.make; + } + // ... +} + +function run() { + var computers = new ComputerCollection(); + + computers.add("Dell", "Studio XPS", "Intel", "5G", "Y755P"); + computers.add("Dell", "Studio XPS", "Intel", "6G", "X997T"); + computers.add("Dell", "Studio XPS", "Intel", "2G", "U8U80"); + computers.add("Dell", "Studio XPS", "Intel", "2G", "NT777"); + computers.add("Dell", "Studio XPS", "Intel", "2G", "0J88A"); + computers.add("HP", "Envy", "Intel", "4G", "CNU883701"); + computers.add("HP", "Envy", "Intel", "2G", "TXU003283"); + + console.log("Computers: " + computers.getCount()); + console.log("Flyweights: " + FlyWeightFactory.getCount()); +} diff --git a/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/structural/proxy.js b/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/structural/proxy.js new file mode 100644 index 0000000000..91efbcd461 --- /dev/null +++ b/Program's_Contributed_By_Contributors/JavaScript_Programs/Data Patterns/structural/proxy.js @@ -0,0 +1,59 @@ +function GeoCoder() { + + this.getLatLng = function (address) { + + if (address === "Amsterdam") { + return "52.3700° N, 4.8900° E"; + } else if (address === "London") { + return "51.5171° N, 0.1062° W"; + } else if (address === "Paris") { + return "48.8742° N, 2.3470° E"; + } else if (address === "Berlin") { + return "52.5233° N, 13.4127° E"; + } else { + return ""; + } + }; +} + +function GeoProxy() { + var geocoder = new GeoCoder(); + var geocache = {}; + + return { + getLatLng: function (address) { + if (!geocache[address]) { + geocache[address] = geocoder.getLatLng(address); + } + console.log(address + ": " + geocache[address]); + return geocache[address]; + }, + getCount: function () { + var count = 0; + for (var code in geocache) { count++; } + return count; + } + }; +}; + +function run() { + + var geo = new GeoProxy(); + + // geolocation requests + + geo.getLatLng("Paris"); + geo.getLatLng("London"); + geo.getLatLng("London"); + geo.getLatLng("London"); + geo.getLatLng("London"); + geo.getLatLng("Amsterdam"); + geo.getLatLng("Amsterdam"); + geo.getLatLng("Amsterdam"); + geo.getLatLng("Amsterdam"); + geo.getLatLng("London"); + geo.getLatLng("London"); + + console.log("\nCache size: " + geo.getCount()); + +}