-
-
Save igor-tomov/eb279b7579a26adb4f5f59854d6664df 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
| enum Ingredients { | |
| SUGAR, | |
| COFFE, | |
| CREAM, | |
| SALT, | |
| } | |
| enum Features { | |
| STIR, | |
| BREW, | |
| FRY, | |
| BOIL, | |
| } | |
| interface Plugin { | |
| name: string | |
| } | |
| interface Storage { | |
| // Specific storage should define own data structure | |
| // in most cases it might be `Array<Plugin>` | |
| private storage: any | |
| add(item: Plugin): Storage | |
| remove(name: string): Storage | |
| get(name: string): Plugin | |
| } | |
| class Ingredient implements Plugin { | |
| public count: number = 0; | |
| constructor(name: string, count: number) { | |
| this.name = name; | |
| this.count = count; | |
| } | |
| } | |
| class IngredientStore implements Storage, Plugin { | |
| static storage: Array<Ingredient>; | |
| constructor() { | |
| super(); | |
| this.name = 'Ingredient Store' | |
| this.storage = []; | |
| } | |
| add(ingredient) { | |
| this.storage.push(ingredient); | |
| } | |
| remove(name) { | |
| this.storage = this.storage.filter(ingredient => ingredient.name !== name); | |
| return this; | |
| } | |
| get(name) { | |
| const ingredient = this.storage.find(ingredient => ingredient.name === name); | |
| if (! ingredient) { | |
| throw new Error(`Ingredient ${name} isn't registered`); | |
| } | |
| return ingredient; | |
| } | |
| } | |
| // -------------- below should be updated ------------- | |
| class FeatureStore implements Storage, Plugin { | |
| static storage: Array<Feature>; | |
| constructor() { | |
| super(); | |
| this.name = 'Feature Store'; | |
| this.storage = []; | |
| } | |
| add(feature) { | |
| storage.push(feature) | |
| return storage | |
| } | |
| remove(name) { | |
| storage.remove(name) | |
| return storage | |
| } | |
| get(name) { | |
| return storage.get(name) | |
| } | |
| } | |
| class RecipeStore implements Storage, Plugin { | |
| constructor() { | |
| this.name = 'Recipe Store' | |
| this.storage = [] | |
| super() | |
| } | |
| add(recipe) { | |
| storage.push(recipe) | |
| return storage | |
| } | |
| remove(name) { | |
| storage.remove(name) | |
| return storage | |
| } | |
| get(name) { | |
| return storage.get(name) | |
| } | |
| } | |
| class USM() { | |
| constructor() { | |
| this.featureRegistry = new FeatureRegistry | |
| this.ingredientStore = new IngredientStore | |
| this.recipeStorage = new RecipeStorage | |
| } | |
| unplug(name) | |
| plug(item) | |
| addIngredient(item) | |
| addRecipe(item) | |
| cook(Recipe) | |
| printPossibleDishes() | |
| on() | |
| of() | |
| } | |
| class Feature implements Plugin() { | |
| constructor() { | |
| name: '' | |
| } | |
| perform() | |
| } | |
| class Recipe implements Plugin() { | |
| static INGREDIENTS_LIST: Array<string>; | |
| static REQUIRED_FEATURES: Array<string>; | |
| constructor(name: string) { | |
| this.name = name; | |
| } | |
| cook(feature, ingredients) | |
| } | |
| const UCM = new UCM | |
| const sugar = new Ingredient('sugar', 1, 'kg') | |
| const coffee = new Ingredient('coffee', 1, 'kg') | |
| UCM.addIngredient(sugar) | |
| UCM.addIngredient(coffee) | |
| const espresso = new Recipe('espresso') | |
| UCM.addRecipe(espresso) | |
| const coffeeMaker = new Feature('coffeeMaker') | |
| UCM.plug(coffeeMaker) | |
| UCM.cook('espresso') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment