Pages

Showing posts with label Design Patterns. Show all posts
Showing posts with label Design Patterns. Show all posts

Sunday, October 27, 2013

Several ways of creating SingletonClass

In "SingletonClass pattern"  post I have discussed about singleton pattern. In this article, I am going to explain, several ways of creating singleton class.

As we known singleton pattern widely used for creating a one and only instance for a class.

Here is the list of the ways we can create singleton class,


  • Static Blocks.
  • Eager initialization. 
  • Lazy initialization.
  • Enums.



  • Static blocks

                  The instance creation of the class during class loading itself and even the constructor of the class called. Like below we can create static singleton class.


  • Eager initialization. 
                 In Eager initialization is one of the best approach to create singleton class. The instance of an class are created much before it is actually required. So the class instance is required or not but this will be created.


  • Lazy initialization.
                In the above method, we have drawback  whether the instance required or not we are creating a single instance and keep in memory. Even if it is not used in the application we are creating it. To avoid this, we have to use Lazy initialization. we have to create a object when required like shown below.


In this above implementation is not suitable for multi threaded environment because two threads such as Thread1 and Thread2 possess the "Obj" value as "null". We have to make synchronized block the getinstance method for mutli threaded application using double check mechanism like below,



The above code is thread safe now. Even several thread access getInstance method the synchronized method blocks other threads, complete it one by one.

  
  • Enums.
                      In Enums implementation provides implict support for thread safety, one and only instance will  be guaranteed. Hence we don't need to write synchronized blocks like above Lazy implementation.

Here is the code,


Happy Learning :)

2. Design Pattern - Singleton Design Pattern


2.1 Introduction 

Singleton design pattern one of the mostly used and simplest pattern in java. This patterns provides the best way to create the objects.

This pattern comes under the creational pattern type.

"This pattern involves a single class which is responsible to creates own object while making sure that only single object get created. This class provides a way to access its only object which can be accessed directly without need to instantiate the object of the class."


2.2. Implementation

Here is the implementation diagram for singleton pattern.

Singleton class has three methods suchs as, getinstance(), sayHi() and clone. Getinstance method provides gives instance of the class to calling class. 

Let see the code,

SingletonClass.java


 SingletonMain.Java




Output:





Saturday, October 26, 2013

1. Design Patterns - Introduction

In this topic we are going to see about the introduction part of design patterns like purpose, importance and its benefits. So lets jump start.

What is design patterns and its purpose?
                         "Design patterns" one of the best practices which is commonly used  by object - oriented developers. It solves the problems of real world problems  and helps the developers to develop a easy reusable code components.

Benefits of  Design Patterns.

     Here is the list of Benefits

  1. Code readability: They do help you write more understandable code with better names for what you are trying to accomplish.
  2. Code maintainability: Allows your code to be maintained easier because it is more understandable.
  3. Communication: They help you communicate design goals among programmers.
  4. Intention: They show the intent of your code instantly to someone learning the code.
  5. Code re-use: They help you identify common solutions to common problems.
  6. Less code: They allow you to write less code because more of your code can derive common functionality from common base classes.
  7. Tested and sound solutions: Most of the design patterns are tested, proven and sound.


Types of Design Patterns



As illustrated in the above, the design patterns are categories into three types; such as

1. Creation Patterns

      These design patterns provides way to create objects while hiding the creation logic, rather than instantiating objects directly using new operator. This gives program more flexibility in deciding which objects need to be created for a given use case.

2. Structural Patterns
      
     These design patterns concern class and object composition. Concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionalists.

3. Behavioral Patterns
     
     These design patterns are specifically concerned with communication between objects.

All these design pattern concepts are fall in these categories. In forthcoming post, I am going to explain each design patterns and its sole purpose. So keep watching this blog. I am going to update on this post.

Your comments are welcome.