Pub Sub Pattern
Publish-Subscribe pattern
Why do we use Pub-Sub pattern?
- Modular: When we wants to add new features or change features, we just need to change the code we need.
- Separate: We don’t have to know other parts of the code, easy for us to make change
Concepts
- message: It is usually a text string(Piece of data that clarifies what the message is about). E.g. KEY_EVENT_DOWN
- publisher: Send commands to subscribers
- subscriber: Receive commands, run the command.
How to create a Pub-Sub pattern
1 | class EventEmitter { |
How to use Pub-Sub pattern
1 | const Messages = { |