-
-
Save suzdalnitski/27cf9d7f7ebaa56d026b813e5befca71 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Fruit { | |
constructor(type, price) { | |
this.type = type; | |
this.price = price; | |
} | |
} | |
class AbstractFruitFactory { | |
make(type, price) { | |
return new Fruit(type, price); | |
} | |
} | |
class AppleFactory extends AbstractFruitFactory { | |
make(price) { | |
return super.make("apple", price); | |
} | |
} | |
class OrangeFactory extends AbstractFruitFactory { | |
make(price) { | |
return super.make("orange", price); | |
} | |
} | |
class GrapeFactory extends AbstractFruitFactory { | |
make(price) { | |
return super.make("grape", price); | |
} | |
} | |
class FruitRepository { | |
constructor() { | |
this.fruitList = []; | |
} | |
locate(strategy) { | |
return strategy.locate(this.fruitList); | |
} | |
put(fruit) { | |
this.fruitList.push(fruit); | |
} | |
} | |
class FruitLocationStrategy { | |
constructor(fruitType) { | |
this.fruitType = fruitType; | |
} | |
locate(list) { | |
return list.find(x => x.type === this.fruitType); | |
} | |
} | |
class FruitPriceLocator { | |
constructor(fruitRepository, locationStrategy) { | |
this.fruitRepository = fruitRepository; | |
this.locationStrategy = locationStrategy; | |
} | |
locatePrice() { | |
return this.fruitRepository.locate(this.locationStrategy).price; | |
} | |
} | |
const appleFactory = new AppleFactory(); | |
const orangeFactory = new OrangeFactory(); | |
const grapeFactory = new GrapeFactory(); | |
const fruitRepository = new FruitRepository(); | |
fruitRepository.put(appleFactory.make(1.99)); | |
fruitRepository.put(orangeFactory.make(2.99)); | |
fruitRepository.put(grapeFactory.make(44.95)); | |
const appleLocationStrategy = new FruitLocationStrategy("apple"); | |
const applePriceLocator = new FruitPriceLocator( | |
fruitRepository, | |
appleLocationStrategy | |
); | |
const applePrice = applePriceLocator.locatePrice(); | |
console.log("apple", applePrice); |
this is disgusting dear god
OOPfags really believe this is good, lmfao.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
😅