Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // serviceRegistry.js
- class ServiceRegistry {
- constructor() {
- if (!ServiceRegistry.instance) {
- this.services = {};
- ServiceRegistry.instance = this;
- }
- return ServiceRegistry.instance;
- }
- }
- const instance = new ServiceRegistry();
- Object.freeze(instance)
- module.exports = instance;
- // Service.js
- class ServiceRegistry {
- constructor() {
- if (!ServiceRegistry.instance) {
- this.services = {};
- ServiceRegistry.instance = this;
- }
- return ServiceRegistry.instance;
- }
- }
- const instance = new ServiceRegistry();
- Object.freeze(instance)
- module.exports = instance;
- // registry.js - utility functions
- class ServiceRegistry {
- constructor() {
- if (!ServiceRegistry.instance) {
- this.services = {};
- ServiceRegistry.instance = this;
- }
- return ServiceRegistry.instance;
- }
- }
- const instance = new ServiceRegistry();
- Object.freeze(instance)
- module.exports = instance;
- // index.js
- class ServiceRegistry {
- constructor() {
- if (!ServiceRegistry.instance) {
- this.services = {};
- ServiceRegistry.instance = this;
- }
- return ServiceRegistry.instance;
- }
- }
- const instance = new ServiceRegistry();
- Object.freeze(instance)
- module.exports = instance;
- // users/index.js
- const Service = require('../Service');
- const { registerService } = require('../registry');
- class UsersService extends Service {
- constructor() {
- super('users');
- }
- hello() {
- console.log("hello from user");
- this.moviesService.hello();
- this.booksService.hello();
- }
- }
- registerService('users', UsersService, ['movies', 'books']);
- module.exports = new UsersService();
- // movies/index.js
- const Service = require('../Service');
- const { registerService } = require('../registry');
- class MoviesService extends Service {
- constructor() {
- super('movies');
- }
- hello() {
- console.log('hello this is movie')
- }
- }
- registerService('movies', MoviesService);
- module.exports = new MoviesService();
- // books/index.js
- const Service = require('../Service');
- const { registerService } = require('../registry');
- class BooksService extends Service {
- constructor() {
- super('books');
- }
- hello() {
- console.log('hello this is book')
- }
- }
- registerService('books', BooksService);
- module.exports = new BooksService();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement