Wednesday, October 7, 2015

Singleton Pattern

This is one of the simplest, yet very useful design pattern. Singleton design pattern can be used when it is required to create just one object of a class. As an example, if you have a ApplicationManager class in your application where all other components need to get help from, you need to have access to ApplicationManager object. But just think that you need to share some objects through your ApplicationManager. Then of course you do not want to have different object referring to different ApplicationManager objects. You need to have one common object shared with all of them. And singleton pattern is the way to achieve this.

I believe an example code snippet will help a lot in understanding this simply.

public class ApplicationManager {

    private static ApplicationManager applicationManager = new ApplicationManager();

    private ApplicationManager() { }

    public static ApplicationManager getInstance() {
        return applicationManager;
    public void yourMethod() {

So you see the constructor is private, so no one can initialize an object of ApplicationManager and if they want to use it, can call getInstance() method and use the already created static ApplicationManager object. So no more than one object is created and global access is provided.

Monday, October 5, 2015

Producer Consumer Pattern

Producer Consumer Design Pattern is one of the mainly used design patterns. And I am pretty sure that, many who does not know that this is an example of a design pattern too have used it. This design pattern is mainly used to introduce decoupling between consuming something and the relevant producing procedure. In other words it implements an efficient and smooth data sharing between the producer of the data and the data consumer. This decoupling is mainly done maintaining a queue for the data items.

By using a queue even the producer and consumer have different rates of producing and consuming data, the data sharing floor will be smooth through the queue. Producer do not have to wait until the consumer finishes data item 1 to deliver data item 2. And producer do not have any overhead of managing the produced items even though there are many consumers as far as all the consumers are consuming the items in that same queue.

So this design pattern can be easily used to perform data sharing between the producer parties and consumer parties in order to achieve good synchronization among them.

Importance of Design Patterns

Design Patterns is one of the most interesting topics in software development. They give well structured and standard solutions for solving common software implementation problems. To understand the design patterns, you should understand the problem that they solve. If you understand them, when you get some problem related to any of the design patterns you know, you can easily map the solution to your problem.

But if ‘Design Patterns’ is a new term for you even though you are doing good software implementation may be still you don’t have to worry. Because may be you are already using design patterns without knowing the correct terminology for those patterns. Still knowing them will help you a lot in improving your implementation as well as it will make it easy for others to quickly understand the functionalities of your implementation. And it will help a lot in smoothly communicating your solutions to the others. As an example if you say that you are using Factory method to solve problem X, others will just understand it without you explaining how you are doing it.

So Design patterns make your code handy, make it more readable and self explanative as well as will be helpful in smooth communication about your implementations.

Here are some commonly used design patterns

  • Producer Consumer    (Oh... You were using this right? That is why I added it first )
  • Singleton
  • Factory/Builder
  • Observer
  • Decorator
  • Command
  • Proxy
  • Strategy