Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

design pattern - Builder pattern

 
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());
}
}
------------------------------
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this topic to OO, Patterns, UML and Refactoring forum....
 
author
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?
 
bacon. tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!