Sunday, December 1

This pattern talks about restoring the original state at any point of time . For example

I start with knowing the mileage of bike around reference value 80KM/h

I store this value in an string instance say : mileage

My reference value / Original value is 80KM/h

Now I start knowing mileage of various bikes and keep updating the the latest known mileage in same string instace : mileage

current value might change from 80 to 70 to 50 to 40 to 100 and so on depending upon the mileage of bikes.

So at any point of time If i want to know the reference value , It's no more there is mileage string.

So how do I make sure that I keep on working on current value as well as be able to retrieve the original value at any point of time.

There might be many ways you ca think across , Memento design pattern suggest a standard way to do that.

Pattern revolves around two contributors

Originator
Care Taker

Originator stores the original value in a non-modified way.Care Taker takes care of returing current or original value at any point of time
Client directly interacts with Care taker to get current or original value

Now lets see how simple is it to implement it in java :

Originator class stores the original value and it can't be modified .

public class Originator {

private final String mileage ="80";

public String getFinalInstance(){
return mileage;
}

}CareTaker provides method to get to current or original mileage at any point of time

And here is our Client who interacts with CareTaker to play with current value and reach original value at any point of time.

public class MementoClient {

public MementoClient() {
// TODO Auto-generated constructor stub
}

public static void main(String args[]){

System.out.println("Some original reference value is already set .......");
CareTaker instance =new CareTaker("50");
System.out.println("Let us create an instance with current value ::: 50");
System.out.println("Lets find out the original reference value ");
System.out.println("it is :::" + instance.restoreDefaultInstance());
System.out.println("and the current value is :: ");
System.out.println(instance.getCurrentValue());
System.out.println("Lets change the value to 40");
instance.saveCurrentInstance("40");
System.out.println("Now current value is ::: " + instance.getCurrentValue());
System.out.println("Let's again change the value to :::100");
instance.saveCurrentInstance("100");
System.out.println("Now current value is : " + instance.getCurrentValue());
System.out.println("Now I want to get to the original value");
System.out.println("it is ::: " + instance.restoreDefaultInstance());
}

}

Executing this application as java application will print below logs on console :

===============================Some original reference value is already set .......
Let us create an instance with current value ::: 50
Lets find out the original reference value
it is :::80
and the current value is ::
50
Lets change the value to 40
Now current value is ::: 40
Let's again change the value to :::100
Now current value is : 100
Now I want to get to the original value
it is ::: 80
================================

Friday, November 29

Lets talk about observer design pattern today--Lets take in consideration a real time scenarion There is a News Control Center that controls current news and subscribers . Lets say there are two subscribers Ching and ChongChing and Chong are registered by News Control center for current news If at any point of time current news are updated News needs to be informed to its all subscribers. Subscriber will not keep on checking news all the time If any current news is updated. They will register once for all and Newswill ensure that they are timely informed about every new news. So how will that hapend

Ching and Chong will act as observers / listener . News will see If any new news is updated it is informed to its subscribers Below Code details this and implement the scenario in java code .NewsControlCenter Class will hold the main control to apply for subscriber registeration and bring in new news to the agency==================public class NewsControlCenter { /** * @param args */ public static void main(String[] args) { Subscriber sub1=new Subscriber("ching"); Subscriber sub2=new Subscriber("chong"); News news=new TimesNews(); news.registerSubscriber(sub1); news.registerSubscriber(sub2); System.out.println("==========news changed======"); news.updateNews("Narayan traced"); System.out.println("==========news changed again ======"); news.updateNews("Govt declared prize money on informing Narayan's whereabout."); }}

Sunday, October 6

What is builder design pattern ?This is used to segregate the logic for creation of complex objects. For example

If we want to create an object of class representing real Estate residential project . We need to take into account lot of factors in building full fledged object . Object will consist of features like payment plan layout construction plan builder information land details finance details location detailssalient features

and so on.....

So we won't prefer to embed the logic of creation of this instance in actual business logic and unnecessarily clutter the business logic flow Instead It would be good to have a dedicated service which can build up this object and once prepared can return it to business logic . Thus actual business logic remains agnostic of all object creation complexities..

So how do we achieve that in Object oriented language .

Let us try to understand this with code. As usual I have written lot of System.out.println statements in the code to bring are execution flow steps in print statements . This code can be directly copied and executed .All steps of design pattern will be clearly written on console.

//This is the Client class which basically place an order. Here this client first place an order of //commercial project and after it's successful delivery It approaches for residential project and //place an order for that.

package realEstate;

publicclass Client {

/**

* @param args

*/

publicstaticvoid main(String[] args) {

projectOwner owner=new projectOwner(new CommercialProjectBuilder());

owner.placeRoder();

owner.getProject();

System.out.println("CLIENT :::: Thank you for timely delivry of commerical project");

// This is project Owner . Client passes the type of project It is looking for : commercial or residential //and creates Project Owner instance . Owner will further place order of construction to Commercial //or Residential department based of which object is passed by client

class projectOwner{

ProjectBuiding building;

projectOwner(ProjectBuiding building){

this.building=building;

}

voidplaceRoder(){

building.constructBase();

building.constructFloors();

building.doFinishing();

building.decorate();

}

ProjectBuidinggetProject(){

returnbuilding;

}

}

// Interface for Residential and commercial project builder classes

interface ProjectBuiding{

void constructBase();

void constructFloors();

void doFinishing();

void decorate();

}

// entire process and logic of building a residential project is encapsulated in this class

class ResidentialProjectBuilder implements ProjectBuiding {

ResidentialProjectBuilder(){

System.out.println("ResidentialProjectBuilder:::Thank you for reaching us..We deal in Residential Projects..");

}

publicvoid constructBase() {

System.out.println("ResidentialProjectBuilder:::Construction is already started.. Promise to deliver on time ");

}

publicvoid constructFloors() {

System.out.println("ResidentialProjectBuilder::::Construction is on full Swing.. Pay installments timely ");

}

publicvoid doFinishing() {

System.out.println("ResidentialProjectBuilder::::About to deliver .. Have litte more Patience ");

}

publicvoid decorate() {

System.out.println("ResidentialProjectBuilder:::IT is well decorated.. Ready to move");

}

}

// entire process and logic of building a Commercial project is encapsulated in this class

Subscribe via email

About Me

Mobile NO : 7503454801
Email address : manojbharal2@gmail.com
About Blog
Blog consist of posts on java j2ee technical topics. It covers all range of post starting with basic java questions up to advanced j2ee architecture. I am owning this blog. Content being published here is authentic and original . This is growing blog and visitors can expect huge relevant and useful information to enhance their knowledge on subject.
About me
I have total of around 8 years of experience in java j2ee technologies and am working on multiple business domain technical projects in all range of industries .