Tuesday, February 7, 2017

Design Pattern

Design Pattern
-----------------
* Creatational Design Pattern- the way to create object.
used when we need to make decision for creating the instance of class.
Type-
1) Factory Design pattern- It says that let create the abstract class or interface for creating an object but let the subclass decide which class to instanciated.
 * also known as virtual constructor

 Ex -

 package inv.learning.designpattern;


interface Animal{

String name="";
String getType();

}
class Dog implements Animal{

@Override
public String getType() {
// TODO Auto-generated method stub
return "Dog";

}

}
class Cat implements Animal{

@Override
public String getType() {
// TODO Auto-generated method stub
return "Cat";

}

}

class AnimalFactory{

static Animal getAnimal(String str){
if(str==null)
return null;
else if (str.equalsIgnoreCase("CAT"))
return new Cat();
else if (str.equalsIgnoreCase("DOG"))
return new Dog();
else
return null;
}

}
public class FactoryDesign {

public static void main(String[] args) {
// AnimalFactory factory = new AnimalFactory();
Animal dog = AnimalFactory
.getAnimal("Cat");
System.out.println(dog.getType());
Animal cat= AnimalFactory.getAnimal("DOG");
System.out.println(cat.getType());
}

}

------------------------------------------------------------------------------------------------------
2) Abstract Factory Pattern-define an interface or abstract class for creating families of related (or dependent) objects but without specifying their concrete sub-classes.



-------------------
3) Singleton Design pattern- define a class which only have one instance and provides a global  point of  access to it.

Ex -
public class Singleton {

public static void main(String[] args) {
MySingletonClass temp=MySingletonClass.getInstance();


}

}
class MySingletonClass {

private final static MySingletonClass myclass=null;
private MySingletonClass(){}
static MySingletonClass getInstance(){
if(myclass == null)
return new MySingletonClass();
else
return myclass;

}
}

----------------------------
3) Prototype design pattern- cloning of an existing object instead of creating new one and can also be customized as per the requirement.
we use this pattern when creating object is expensinve.
t reduces the need of sub-classing.
It hides complexities of creating objects.
The clients can get new objects without knowing which type of object it will be.
It lets you add or remove objects at runtime.

Example-

public class Prototype {
public static void main(String[] args) {
Employee emp= new Employee(10, "vishal", "g4s");
System.out.println("address of first emp object "+emp.address);
Employee emp2=(Employee)emp.getClone();
System.out.println("address of sec emp object "+emp2.address);
}
}
interface InfPrototype{
InfPrototype getClone();
}
class Employee implements InfPrototype
{ int age;
String name;
public String address;

public Employee(){

}
public Employee(int age,String name,String address)
{
this.age=age;
this.name=name;
this.address=address;
}
@Override
public InfPrototype getClone() {
// TODO Auto-generated method stub
return new Employee(age,name,address);
}

}
--------------------------------
4) Builder Design Pattern-  wehen we construct a complex object from simple object using step by step approach.
It provides clear separation between the construction and representation of an object.
It provides better control over construction process.
It supports to change the internal representation of objects.


-----------------------------------------------------------------------------------------------------------------------
STRUCTURAL DESIGN Pattern-simplifies the structure by identifying the relationships

1) Adapter Design Pattern- converts the interface of a class into another interface that a client wants
 it is also known as wrapper.
 When an object needs to utilize an existing class with an incompatible interface.
When you want to create a reusable class that cooperates with classes which don't have compatible interfaces.
When you want to create a reusable class that cooperates with classes which don't have compatible interfaces.



2)Bridge Pattern-Decouple the functional abstraction from the implementation so that the two can vary independently
The Bridge Pattern is also known as Handle or Body.

When you don't want a permanent binding between the functional abstraction and its implementation.
When both the functional abstraction and its implementation need to extended using sub-classes.
It is mostly used in those places where changes are made in the implementation does not affect the clients.

Thanks for reading
-Noeik

0 comments:

Post a Comment