wood burning stoves 2.0*
The moose likes OO, Patterns, UML and Refactoring and the fly likes design pattern - Builder pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "design pattern - Builder pattern" Watch "design pattern - Builder pattern" New topic
Author

design pattern - Builder pattern

sowmya thiru
Greenhorn

Joined: Jun 09, 2003
Posts: 19
Hi,
Im a rookie in DP. Im trying out bruce eckel questions on DP. one excercise on Builder is
"Break a text file up into an input stream of words (consider using regular expressions for this). Create one Builder that puts the words into a java.util.TreeSet, and another that produces a java.util.HashMap containing words and occurrences of those words (that is, it does a word count)."
Here is my solution. I want somebody to validate/finetune it. I have not coded word count algorithm
-------------------------------------------
public class Builder {
public Builder()
{}
public product getFinishedProduct(String str) {
return null ;
}
}
------------------------------
import java.util.*;
public class CountBuilder extends Builder {

private product p ;
public CountBuilder()
{
p = new product();
System.out.println("count builder constructor");
}
public product getFinishedProduct(String str) {
HashMap hm = new HashMap();
hm.put("one" ,"two");
p.addHashMap(hm);
return p;
}
}
------------------------------
import java.util.*;
public class SetBuilder extends Builder {
private product p ;
public SetBuilder()
{
p = new product();
System.out.println(" SetBuilder constructor");
}
public product getFinishedProduct(String str) {
StringTokenizer st = new StringTokenizer(str);
TreeSet ts = new TreeSet();
while (st.hasMoreTokens()) {
ts.add(st.nextToken());
}

p.addTreeSet(ts);
return p;
}
}
------------------------------
import java.io.*;
public class director {
private Builder builder ;
public director(Builder b) {
this.builder = b ;
}
public product produceProduct() {
BufferedReader in;
try
{
in = new BufferedReader(new FileReader("abc.txt"));
return builder.getFinishedProduct(in.readLine());
}
catch (Exception e) {
e.printStackTrace();
return null ;
}
}
}
------------------------------
import java.util.*;
public class product {
HashMap hm ;
TreeSet ts ;
public product() {
}
public void addHashMap(HashMap hm){
hm = hm;
}
public void addTreeSet(TreeSet ts) {
this.ts = ts;
}
public String toProduct() {
if (hm!=null) {
return hm.toString();
}
else
{
return ts.toString();
}
}
}
------------------------------
public class testDirectory {
public static void main(String arr[]){
director d = new director(new SetBuilder());
product p = d.produceProduct();
System.out.println(p.toProduct());
}
}
------------------------------
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Moving this topic to OO, Patterns, UML and Refactoring forum....


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The first question we should ask ourselves is: why use a Builder in this case? What is the purpose of using this pattern?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
wood burning stoves
 
subject: design pattern - Builder pattern