The decorator pattern
The primary idea behind the decorator pattern is that you start your design with a plain object, which has some basic functionality. As the design evolves, you can use existing decorators to enhance your plain object. This is a very popular pattern in the OO world and especially in Java. Let's take an example of BasicServer
—a server with very basic functionality. This basic functionality can be decorated to serve specific purposes. We can have two different cases where this server can serve both PHP and Node.js and serve them on different ports. These different functionality are decorated to the basic server:
var phpServer = new BasicServer(); phpServer = phpServer.decorate('reverseProxy'); phpServer = phpServer.decorate('servePHP'); phpServer = phpServer.decorate('80'); phpServer = phpServer.decorate('serveStaticAssets'); phpServer.init();
The Node.js server will have something as follows:
var nodeServer = new BasicServer(); nodeServer = nodeServer.decorate('serveNode...