|
| 1 | +import { bodyParser } from '@koa/bodyparser'; |
| 2 | +import debug from 'debug'; |
| 3 | +import Koa from 'koa'; |
| 4 | +import compose from 'koa-compose'; |
| 5 | + |
| 6 | +class Context { |
| 7 | + /** |
| 8 | + * @type {Strategy} The Context maintains a reference to one of the Strategy |
| 9 | + * objects. The Context does not know the concrete class of a strategy. It |
| 10 | + * should work with all strategies via the Strategy interface. |
| 11 | + */ |
| 12 | + private plugins: Strategy[]; |
| 13 | + app; |
| 14 | + use; |
| 15 | + /** |
| 16 | + * Usually, the Context accepts a strategy through the constructor, but also |
| 17 | + * provides a setter to change it at runtime. |
| 18 | + */ |
| 19 | + constructor() { |
| 20 | + // init |
| 21 | + const app = new Koa(); |
| 22 | + app.use(bodyParser()); |
| 23 | + |
| 24 | + this.app = app; |
| 25 | + this.use = app.use; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Usually, the Context allows replacing a Strategy object at runtime. |
| 30 | + */ |
| 31 | + public plugin(strategy: Strategy) { |
| 32 | + this.plugins.push(strategy); |
| 33 | + } |
| 34 | + |
| 35 | + public mount() { |
| 36 | + console.dir('s'); |
| 37 | + // mount |
| 38 | + for (const plugin of this.plugins) { |
| 39 | + console.log(plugin); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * The Context delegates some work to the Strategy object instead of |
| 45 | + * implementing multiple versions of the algorithm on its own. |
| 46 | + */ |
| 47 | + public start(): void { |
| 48 | + this.mount(); |
| 49 | + |
| 50 | + console.log("Context: Sorting data using the strategy (not sure how it'll do it)"); |
| 51 | + // const result = this.strategy.doAlgorithm(['a', 'b', 'c', 'd', 'e']); |
| 52 | + // console.log(result.join(',')); |
| 53 | + |
| 54 | + // ... |
| 55 | + // return listenerCount() |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * The Strategy interface declares operations common to all supported versions |
| 61 | + * of some algorithm. |
| 62 | + * |
| 63 | + * The Context uses this interface to call the algorithm defined by Concrete |
| 64 | + * Strategies. |
| 65 | + */ |
| 66 | +interface Strategy { |
| 67 | + doAlgorithm(data: string[]): string[]; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Concrete Strategies implement the algorithm while following the base Strategy |
| 72 | + * interface. The interface makes them interchangeable in the Context. |
| 73 | + */ |
| 74 | +class ConcreteStrategyA implements Strategy { |
| 75 | + public doAlgorithm(data: string[]): string[] { |
| 76 | + return data.sort(); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +class ConcreteStrategyB implements Strategy { |
| 81 | + public doAlgorithm(data: string[]): string[] { |
| 82 | + return data.reverse(); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * The client code picks a concrete strategy and passes it to the context. The |
| 88 | + * client should be aware of the differences between strategies in order to make |
| 89 | + * the right choice. |
| 90 | + */ |
| 91 | +// const context = new Context(new ConcreteStrategyA()); |
| 92 | +// console.log('Client: Strategy is set to normal sorting.'); |
| 93 | +// context.start(); |
| 94 | + |
| 95 | +// console.log(''); |
| 96 | + |
| 97 | +// console.log('Client: Strategy is set to reverse sorting.'); |
| 98 | +// context.plugin(new ConcreteStrategyB()); |
| 99 | +// context.start(); |
0 commit comments