• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

design pattern - Builder pattern

 
sowmya thiru
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this topic to OO, Patterns, UML and Refactoring forum....
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first question we should ask ourselves is: why use a Builder in this case? What is the purpose of using this pattern?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic