Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/maikenegreiros/observer-pattern
A simple observer pattern implementation in TypeScript.
https://github.com/maikenegreiros/observer-pattern
Last synced: 4 days ago
JSON representation
A simple observer pattern implementation in TypeScript.
- Host: GitHub
- URL: https://github.com/maikenegreiros/observer-pattern
- Owner: maikenegreiros
- License: mit
- Created: 2018-04-26T23:44:43.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-07-10T12:25:49.000Z (over 6 years ago)
- Last Synced: 2024-11-08T23:05:41.673Z (9 days ago)
- Language: TypeScript
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# ts-observer-pattern
A simple observer pattern implementation in TypeScript.```
npm install ts-observer-pattern
```## Example
In the example that follows, I'm implementing a relationship bettween a magazine and it subscribers
```TypeScript
// Magazine.ts
import { Subject } from 'ts-observer-pattern'class Magazine extends Subject
{
private Observers: Observers[]public publish()
{
//Some code here
this.Observer.notify("New edition available") //Here I passed a string,
//but you can pass any data you want
}
}
``````TypeScript
// Subscriber.ts
import { Observer } from 'ts-observer-pattern'class Subscriber implements Observer
{
public update(data): this
{
//Do whatever you want here with the information passed by the Magazine object
}
}
``````TypeScript
// main.ts
import { Magazine } from './Magazine'
import { Subscriber } from './Subscriber'let Coders = new Magazine
let Matt = new Subscriber
let Ana = new SubscriberCoders.attach(Matt)
Coders.attach(Ana)
```