This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Micro Frontends in Action and have Michael Geers on-line!
See this thread for details.
Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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?
 
Everybody's invited. Even this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic