• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Which design pattern to use?

 
Greenhorn
Posts: 28
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
My method in a Java class reads large files and gathers some information which needs to be persisted somewhere.
Example:
method()
{
while(read large file in chunks till the end of the file)
{
gather the useful data from that data chunk and call another java component (XYZ) to persist the useful data.
}
}
I want XYZ to be pluggable and hide all that complexity.

What is the best design pattern that suits to my requirement?

Thanks in advance
Aadil
 
Marshal
Posts: 76802
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a design pattern for that? Yes, maybe there is because the Gang of Four recognised the Iterator pattern. Now you wouldn't use an Iterator directly but you can get the lines() method of a BufferedReader, which returns a Stream and that Stream performs the iteration for you as if it contained an iterator. It doesn't, but you don't know that. You would also use the try‑with‑resources construct and that is a sort of design pattern.
import static java.util.stream.Collectors.toList;
// ...
try (FileReader fileRead = new FileReader(myFile);
     BufferedReader fileBuff = new BufferedReader(fileRead))
{
     myList = fileBuff.lines()
                      .filter(s -> s.matches(myRegex))
                      .map(s -> new Foo(s))
                      .collect(toList());
}

You are doubtless familiar with the first two lines. It says in Maurice Naftalin's book that it is usual to import methods of the Collectors class statically.
The lines method gets a Stream from the BufferedReader which terminates when the final operation terminates or when the reader runs out of lines (I think).
The filter method tests whether the objects in the Stream fulfil a certain predicate, in this case whether they match a particular regex, and creates a second Stream containing only those elements in the first Stream which fulfilled that predicate.
The map method creates a new Stream<Foo> containing Foos whose constructor takes the Strings in the previous Stream.
The collect method takes a Collector. The toList method returns a Collector instance which creates a List (probably an instance of the well‑known array list).

Now I'll let you work out for yourself how to persist the contents of that List. There are other ways to do the same operation and other ways to write the expressions passed to the Stream methods.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aadil Shaik wrote:My method in a Java class reads large files and gathers some information which needs to be persisted somewhere.
...
What is the best design pattern that suits to my requirement?


Like Campbell, I'm not sure that there's any particular design "pattern" (other than what he suggested), but there are things you might want to consider.

For example: Are there ways to determine discrete "chunks" of data to be persisted in advance? (Eg: a filter like a regex, or a particular type of record, or set of records) If so, you may be able to "parallelize" (ugh) your Stream on that basis, or simply create Threads that process each record type or set independently the old-fashioned way.

Obviously, that would be tougher (or maybe impossible) if the data in these files is sequentially or hierarchically linked (like a DOM), but it's the first thing I'd look at.

HIH

Winston
 
Aadil Shaik
Greenhorn
Posts: 28
1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry. I didn't ask the question with proper explanation.
But i have come up with a technique that decouples the implementation of the logger.



-------------------------------------------

-----------------------------------------



------------------------------------------

Any class that implements DataLogger can have their own implementation. This is my requirement.
But if you have any better ideas please let me know.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aadil Shaik wrote:I am sorry. I didn't ask the question with proper explanation.
But i have come up with a technique that decouples the implementation of the logger.


Well, thanks for the solution, but I hate to say, it doesn't enlighten me (or, I suspect, other people) much.

An interface is supposed to describe a specific contact for behaviour, and all we know about yours is that it presumably "logs" data for 3 separate purposes ('A', 'B' and 'C'). And while I understand that it isn't public (I don't know whether that was intentional or not), you should at least try to make it clear to anyone who's reading your program:
1. What 'A', 'B' and 'C' are.
2. What a "log" is.
and
3. What they log (beyond "data").
otherwise I suspect most people would be as mystified as me as to how we would use it.

The first question I would ask, if a programmer presented that to me, would be: Why not just use Java's Logger?

Sometimes, interfaces can be too abstract.

HIH

Winston
 
He puts the "turd" in "saturday". Speaking of which, have you smelled this tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic