function Flipkart() {
this.getPrice = function (productName) {
return `Flipkart:: Price for item ${productName}: is $10`;
};
}
function Amazon() {
this.getPrice = function (productName) {
return `Amazon:: Price for item ${productName}: is $11`;
};
}
function Price() {
this.setWebsiteName = function (websiteName) {
this.websiteName = websiteName;
};
this.getPrice = function (productName) {
return this.websiteName.getPrice(productName);
};
}
const priceFromFlipkart = new Price();
priceFromFlipkart.setWebsiteName(new Flipkart());
console.log(priceFromFlipkart.getPrice("S24"));
const priceFromAmazon = new Price();
priceFromAmazon.setWebsiteName(new Amazon());
console.log(priceFromAmazon.getPrice("S24"));
Strategy Pattern
Using ES5 syntax
class Flipkart {
constructor() {}
getPrice(productName) {
return `Flipkart:: Price for item ${productName}: is $10`;
}
}
class Amazon {
constructor() {}
getPrice(productName) {
return `Amazon:: Price for item ${productName}: is $11`;
}
}
class Price {
constructor(websiteName) {
this.websiteName = websiteName;
}
getPrice(productName) {
return this.websiteName.getPrice(productName);
}
}
const priceFromFlipkart = new Price(new Flipkart());
console.log(priceFromFlipkart.getPrice("S24"));
const priceFromAmazon = new Price(new Amazon());
console.log(priceFromAmazon.getPrice("S24"));