RSS

What is Singleton design pattern ?

Checkout the working code from my git hub repository using below link. It contains all possible ways of implementation of singleton with multi-thread issue which creates multiple instance of singleton.

https://github.com/premaseem/designPatterns/tree/master/SingletonPattern/src/com/premaseem/singleton

Singleton is a part of Gang of Four design pattern and it is categorized under creational design patterns.


In Java the Singleton pattern will ensure that there is only one instance of a class is created in the Java Virtual Machine. It is used to provide global point of access to the object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects. In terms of practical use Singleton patterns are used in logging, caches, thread pools, configuration settings, device driver objects.
For example, if you have a license for only one connection for your database or your JDBC driver has trouble with multithreading, the Singleton makes sure that only one connection is made or that only one thread can access the connection at a time.

 
 

Tags:

What is Abstract Factory Design Pattern ?

Download the working code from my git hub repository from this link https://github.com/premaseem/designPatterns/tree/master/AbstractFactoryPattern

Basic Definition of Abstract Factory :

Abstract Factory patterns works around a super-factory which creates other factories. This factory is also called as Factory of factories. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

In Abstract Factory pattern an interface is responsible for creating a factory of related objects, without explicitly specifying their classes.

 

AbstractFactory1

 

The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes.[1] In normal usage, the client software creates a concrete implementation of the abstract factory and then uses the generic interface of the factory to create the concrete objects that are part of the theme. The client doesn’t know (or care) which concrete objects it gets from each of these internal factories, since it uses only the generic interfaces of their products.[1] This pattern separates the details of implementation of a set of objects from their general usage and relies on object composition, as object creation is implemented in methods exposed in the factory interface.[2]

An example of this would be an abstract factory class DocumentCreator that provides interfaces to create a number of products (e.g. createLetter() and createResume()). The system would have any number of derived concrete versions of the DocumentCreator class like FancyDocumentCreator or ModernDocumentCreator, each with a different implementation of createLetter() and createResume() that would create a corresponding object like FancyLetter or ModernResume. Each of these products is derived from a simple abstract class like Letter or Resume of which the client is aware. The client code would get an appropriate instance of the DocumentCreator and call its factory methods. Each of the resulting objects would be created from the same DocumentCreator implementation and would share a common theme (they would all be fancy or modern objects). The client would only need to know how to handle the abstract Letter or Resume class, not the specific version that it got from the concrete factory.

A factory is the location of a concrete class in the code at which objects are constructed. The intent in employing the pattern is to insulate the creation of objects from their usage and to create families of related objects without having to depend on their concrete classes.[2] This allows for new derived types to be introduced with no change to the code that uses the base class.

Use of this pattern makes it possible to interchange concrete implementations without changing the code that uses them, even at runtime. However, employment of this pattern, as with similar design patterns, may result in unnecessary complexity and extra work in the initial writing of code. Additionally, higher levels of separation and abstraction can result in systems which are more difficult to debug and maintain. Therefore, as in all software designs, the trade-offs must be carefully evaluated.

 
 

Tags: , , ,

what’s the difference between a simple factory, a factory method design pattern, and an abstract factory?

Thing that all three have in common is that they are responsible for creating objects.  The calling class (which we call the “client”) wants an object, but wants the factory to create it. I guess if you wanted to sound really professional, you could say that factories are used to encapsulate instantiation.

So what’s the difference between a simple factory, a factory method design pattern, and an abstract factory?

- A Simple factory is normally called by the client via a static method, and returns one of several objects that all inherit/implement the same parent.
- The Factory Method design is really all about a “create” method that is implemented by sub classes.
- Abstract Factory design is about returning a family of related objects to the client.  It normally uses the Factory Method to create the objects.

You can checkout the working project from my git hub repository, here is the link : https://github.com/premaseem/designPatterns/commit/94cdd85ac5f7080fb1be03dc885d6a6dc3b1ad8c#diff-d41d8cd98f00b204e9800998ecf8427e

Let us take a detailed look in each of them with minute details.

Simple Factory
The easiest way for me to remember this is that a simple factory is called directly by the class which wants to create an object (the calling class is referred to as the “client”).  The simple factory returns one of many different classes.  All the classes that a simple factory can return either inherit from the same parent class, or implement the same interface.

simpleFactory

Step One: you call a method in the factory.  Here it makes sense to use a static method.  The parameters for your call tell the factory which class to create.
Step Two:  the factory creates your object. The only thing to note is that of all the objects it can create, the objects have the same parent class, or implement the same interface.
Step Three: factory returns the object, and this is why step two makes sense.  Since the client didn’t know what was going to be returned, the client is expecting a type that matches the parent class /interface.

Please note that this is not an “official” design pattern.

 

Factory Method
The official definition of the pattern is something like: a class which defers instantiation of an object to subclasses.  An important thing to note right away is that when we’re discussing the factory pattern, we’re not concentrating on the implementation of the factory in the client, but instead we’re examining the manner in which objects are being created.
In this example the client doesn’t have a direct reference to the classes that are creating the object, but instead has reference to the abstract “Creator”. (Just because the creator is abstract, doesn’t mean this is the Abstract Factory!).  It is a Factory Method because the children of “Creator” are responsible for implementing the “Create” method. Another key point is that the creator is returning only one object.   The object could be one of several types, but the types all inherit from the same parent class.

factoryMethod

Step One: the client maintains a reference to the abstract Creator, but instantiates it with one of the subclasses.  (i.e. Creator c = new ConcreteCreator1(); )
Step Two: the Creator has an abstract method for creation of an object, which we’ll call “Create”.  It’s an abstract method which all child classes must implement.  This abstract method also stipulates that the type that will be returned is the Parent Class or the Interface of the “product”.
Step Three: the concrete creator creates the concrete object.  In the case of Step One, this would be “Child Class A”.
Step Four: the concrete object is returned to the client.  Note that the client doesn’t really know what the type of the object is, just that it is a child of the parent.

 

Abstract Factory
This is biggest pattern of the three.  I also find that it is difficult to distinguish this pattern from the Factory Method at a casual glance.  For instance, in the Factory Method, didn’t we use an abstract Creator?  Wouldn’t that mean that the Factory Method I showed was a actually an Abstract Factory?  The big difference is that by its own definition, an Abstract Factory is used to create a family of related products (Factory Method creates one product).

AbstractFactory

Step One:  the client maintains a reference to an abstract Factory class, which all Factories must implement.  The abstract Factory is instantiated with a concrete factory.
Step Two: the factory is capable of producing multiple types.  This is where the “family of related products” comes into play.  The objects which can be created still have a parent class or interface that the client knows about, but the key point is there is more than one type of parent.
Step Three: the concrete factory creates the concrete objects.
Step Four: the concrete objects are returned to the client.  Again, the client doesn’t really know what the type of the objects are, just that are a children of the parents.
See those concrete factories?  Notice something vaguely familiar?  There using the Factory Method to create objects.

 
 

Tags: ,

What is EBOLA?

There is currently an Ebola outbreak putting all of us at risk. Pls help educate all around u by sending this msg to all your contacts..

eloba

What is EBOLA?

It’s a virus that attacks a person blood system:

Ebola is what scientists call a haemorraghic fever – it operates by making its victims bleed from almost
anywhere on their body.
Usually victims bleed to
death.

Ebola is highly contagious;
Being transmitted via
contact with body fluids
such as blood, sweat, saliva,
semen or other body discharges.

Ebola is however NOT AN AIRBORNE VIRUS!

EXTREMELY deadly:
About 90% of people that
catch Ebola will die from it.
It’s one of the deadliest
diseases in the world,
killing in just a few weeks.

Untreatable(no cure):
Ebola has no known treatment or cure.
Victims are usually treated for symptoms with the faint hope that they
recover.

How Do I Know Someone has Ebola?

•Fever
•Headache
•Diarrhoea
•Vomiting
•Weakness
•Joint & Muscle pains
•Stomach Pain
•Lack of Appetite

Protect Yourself:
•Wash Your Hands
with Soap
Do this a lot. You can
also use a good hand
sanitizer. Avoid
unnecessary physical contact with people.

■Restrict yourself to food you prepared yourself.

■Disinfect Your Surroundings
The virus cannot
survive disinfectants,heat, direct sunlight,detergents and soaps.

Clean up!:
•Fumigate If you have Pests.
•Rodents can be carriers of Ebola.
•Fumigate your environment & dispose off the carcasses properly!
•Dead bodies CAN still
transmit Ebola.
•Don’t touch them without
protective gear or better yet avoid them altogether.

Protect Yourself:
•Use protective gear if you
must care or go near
someone you suspect has
Ebola.

Report:
•Report any suspicious
symptoms in yourself or
anyone else IMMEDIATELY.

Do not delay!

Educate Everyone:
•Tell your neighbours,
colleagues and domestic
staff (anyone really). Basically you’re safer when
everyone is educated.

 
Leave a comment

Posted by on August 29, 2014 in Uncategorized

 

Composition of Trust

When we say we trust some body then in the back of mind we think several things before forming any opening. Composition of professional trust can be described as mentioned in this diagram

 

trust

 
Leave a comment

Posted by on August 26, 2014 in Leadership, Life, Management

 

FIVE FACTORS THAT INFLUENCE THE MIND AND PRODUCTIVITY

Every place you are in has a different impact on the mind. Even in your house you can see that you feel differently in different rooms. A place where there there has been singing, chanting and meditation has a different influence on the mind. Suppose you like a particular place; you may find that a little later it will not be the same.

Time is also a factor. Different times of the day and year have different influences on the mind.

Different types of food that you take influence you for several days.

Past impressions- Karmas- have a different impact on the mind. Awareness, alertness, knowledge and meditation all help erase the past impressions.

Associations & actions, or the people and events you are associated with, also influence your mind. In certain company your mind behaves in one way and with others in a different way.When you do something which you are good at, you feel more relaxed and peace of mind, however if you do things which you do not like, you mind is not focused and energy seems to scattered around. 

For better productivity, you can try a place you like, with a group of people you feel comfortable, then try to have some nice food together and try some fun activities, like games, dance, signing, meditation, study or work together. 

 
 

Tags:

Image

Health Tip : 10 reason to eat Guavas

gauva

 
Leave a comment

Posted by on August 21, 2014 in food, Health and Fitness

 

Tags:

 
Follow

Get every new post delivered to your Inbox.

Join 1,061 other followers

%d bloggers like this: