AlgorithmsCodilityPatternsTutorials

Another post covering three patterns, you would say i was getting lazy 🙂 But as these patterns are simple enough to explain in few lines, i wont go into much detail. Nevertheless they are worth noting as all of them are frequently used and very useful.

Proxy

“In the proxy pattern, one object acts as an interface to another object. The proxy sits between the client of an object and the object itself and protects the access to that object.”

– Stoyan Stefanov

Imagine you have a service or even better, an external library that takes care of some complex logic in your app. For example, a beerRecipeProvider and later on, after it has been implemented numerous time in your code,  you want to hook some additional behaviour such as logging the ingredients on every call:

Facade

“The Facade Pattern provides the user with a simple interface, while hiding it’s underlying complexity.

– not really sure who said this

In essence, it is basically be a simple function, that calls other interrelated functions and therefore enables you to have an interface(facade) for your functionality.

The important thing to note here is that the facade never adds functionality, that is what the Mediator pattern is for 🙂

Flyweight

This one is a bit more complex. Flyweight pattern can be applied to objects that have two types of states, extrinsic and intrinsic.

A good example would be a group of characters on a webpage, say a paragraph you are reading right now. Every character has it’s own intrinsic properties, such as the letter it is representing, and the position on screen. There is no need to define the color or font-family for each one, so let’s group those extrinsic properties into a separate objects to save memory.

Sounds familiar? You guessed it, in Javascript, you get the flyweight pattern for free! Prototype based inheritance is basically a flyweight pattern. If you are not familiar with prototypal inheritance, then start from my blog post about Prototype pattern.

And that is the last of Structural patterns defined in the GOF book. Next, we will take a look at behavioral design patterns and their implementations.