diff --git a/factory/simple-factory/main.js b/factory/simple-factory/main.js index 8d0f201..5808439 100644 --- a/factory/simple-factory/main.js +++ b/factory/simple-factory/main.js @@ -1,21 +1,21 @@ -"use strict"; +'use strict' function Admin() { - console.log('Admin created!'); + console.log('Admin created!') } function Customer() { - console.log('Customer created!'); + console.log('Customer created!') } -var UserFactory = {}; +const UserFactory = {} UserFactory.createUser = function(type) { - if (type == 'admin') { - return new Admin(); - } else if (type == 'customer') { - return new Customer(); - } -}; + const allTypes = { + admin: Admin, + customer: Customer + } + return new allTypes[type]() +} -var customer = UserFactory.createUser('admin'); -var customer = UserFactory.createUser('customer'); \ No newline at end of file +var customer = UserFactory.createUser('admin') +var customer = UserFactory.createUser('customer') diff --git a/pub-sub/README.MD b/pub-sub/README.MD new file mode 100644 index 0000000..8ddf792 --- /dev/null +++ b/pub-sub/README.MD @@ -0,0 +1,46 @@ +# PUB SUB PATTERN + + + +## Pub/sub messaging has the following benefits: + +- It decouples subsystems that still need to communicate. Subsystems can be managed independently, and messages can be properly managed even if one or more receivers are offline. + +- It increases scalability and improves responsiveness of the sender. The sender can quickly send a single message to the input channel, then return to its core processing responsibilities. The messaging infrastructure is responsible for ensuring messages are delivered to interested subscribers. + +- It improves reliability. Asynchronous messaging helps applications continue to run smoothly under increased loads and handle intermittent failures more effectively. + +- It allows for deferred or scheduled processing. Subscribers can wait to pick up messages until off-peak hours, or messages can be routed or processed according to a specific schedule. + +- It enables simpler integration between systems using different platforms, programming languages, or communication protocols, as well as between on-premises systems and applications running in the cloud. + +- It facilitates asynchronous workflows across an enterprise. + +- It improves testability. Channels can be monitored and messages can be inspected or logged as part of an overall integration test strategy. + +- It provides separation of concerns for your applications. Each application can focus on its core capabilities, while the messaging infrastructure handles everything required to reliably route messages to multiple consumers. + +## When to use this pattern + +Use this pattern when: + +- An application needs to broadcast information to a significant number of consumers. + +- An application needs to communicate with one or more independently-developed applications or services, which may use different platforms, programming languages, and communication protocols. + +- An application can send information to consumers without requiring real-time responses from the consumers. + +- The systems being integrated are designed to support an eventual consistency model for their data. + +- An application needs to communicate information to multiple consumers, which may have different availability requirements or uptime schedules than the sender. + +This pattern might not be useful when: + +- An application has only a few consumers who need significantly different information from the producing application. + +- An application requires near real-time interaction with consumers. + +### References + + - [Microsoft](https://docs.microsoft.com/en-us/azure/architecture/patterns/publisher-subscriber) + - [Youtube - Dev Pleno - PT-BR](https://www.youtube.com/watch?v=B3YqxAFs-IQ) diff --git a/pub-sub/exemples/exemple-pub-sub.js b/pub-sub/exemples/exemple-pub-sub.js new file mode 100644 index 0000000..caedc26 --- /dev/null +++ b/pub-sub/exemples/exemple-pub-sub.js @@ -0,0 +1,44 @@ +class Socket { + constructor(channels = {}) { + this.channels = channels + } + subscribe(channelName, callback) { + const currentCbs = this.channels[channelName] || [] + currentCbs.push(callback) + this.channels[channelName] = currentCbs + } + publish(channelName, message) { + if (this.channels[channelName]) { + this.channels[channelName].forEach(fn => fn(message)) + } + } + unsubscribe(channelName, callback) { + const index = (this.channels[channelName] || []).indexOf(callback) + if (index >= 0) { + this.channels[channelName].splice(index, 1) + } + } + broadcast(message) { + Object.keys(this.channels).forEach(channel => + this.publish(channel, message) + ) + } +} + +const socket = new Socket() + +socket.subscribe('test1', message => { + console.log('test1', message) +}) + +socket.subscribe('basket', message => { + console.log('basket', message) +}) + +socket.publish('test1', { + message: 'just a test ' +}) + +socket.broadcast({ + message: 'TEST BROADCAST' +}) diff --git a/pub-sub/images/publish-subscribe.png b/pub-sub/images/publish-subscribe.png new file mode 100644 index 0000000..165489d Binary files /dev/null and b/pub-sub/images/publish-subscribe.png differ