Solved

How to get an instance of two classes implementing an interface

Posted on 2011-09-24
7
205 Views
Last Modified: 2013-11-23
I have an interface, ReportWriter which has two subclasses, csvWriter and XmlWriter which write the report in html and XML formats respectively.

I have sample code below:

Interface ReportWriter{

public createSuccessfulRow(String msg);

public createFailureRow (String msg);

}

class HtmlWriter{
//Implementation
}

class XmlWriter{

//Implementation
}


Class ReportImpl{

//Instantiate a class of instance XmlWriter or HtmlWriter based on the Interface
}

In my class, ReportImpl, I would like to get an instance of the writer from the interface. Can I use a Enumerator?

Please let me know.

Thanks,
Prasanna

0
Comment
Question by:pkrish80
  • 3
  • 2
  • 2
7 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36594278

I am sorry can you elaborate a little bit.

I don't see any standard class called Enmerator
(there is Enumeration, enum type, but I could not locate Enumerator)

And what is it that you want to accomplish?

Yes, you can use your interface name to specify type of your objects
If, say, you have ArrayList of objects which contains instances
of both HTMLWriter and XMLWriter, then
in ReportImpl

you can get elemenst of this ArrayList like that:

ArrayList<ReportWriter> arrayList;

ReportWriter rw = arrayList.get(0);
rw.createSuccesFullRow(message);

rw = arrayList.get(1);
rw.createSuccessfulRow(message);

and in this way have printout in different formats.

Is there some other way how you want to use this interface?



 
0
 

Author Comment

by:pkrish80
ID: 36595818
Hi for_yan,

Yes, In ReportImpl, I want to create an Instance of one of the different type of report writer. I want to know of other ways to get an instance with the Interface other than a arraylist. I wanted ideas around Using Enum type  and Enumeration.

Can I get a specific type of writer with code such as

(ReportWriter) writer = ReportWriter.getWriter(); and it returns a html or csv writer automatically? I know this might be a bit vague. Please do let me know if you have any other ideas?

Thanks

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36595832



Can you explain what it is that you want to accomplish?

With arraylist, inteface and perhapse instanceof operator you should be able to do whatever you need
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 1

Expert Comment

by:dileeph
ID: 36597858
Prasanna,

This is where the FactoryPattern comes in. There are different ways of doing this, but in essence you create a new class ReportWriterFactory. This will have the static ReportWriter getWriter(type) method.

From your ReportImpl class you will then invoke either ReportWriter.getWriter("csv") or ReportWriter.getWriter("xml"); This will then give the correct instance back. Internally the Factory class can be implemented in n number of ways - again all it does is check the type of instance needed and return either new XmlReportWriter() or new CSVReportWriter().

When I said "n" number of ways, one way would be to use an xml file to map the type to instance, you could use singleton writer instances and pre-cache them, instead of type string you can pass type enum ... so many ways.

Please let me know if you need more help.

0
 

Author Comment

by:pkrish80
ID: 36600947
How do I use type enum with in my ReportWriterFactory class? Can you throw more light on it?
0
 

Author Comment

by:pkrish80
ID: 36601028
The ReportWriterFactory class, would that be an abstract class which the csv and xml report writers would sublass?
0
 
LVL 1

Accepted Solution

by:
dileeph earned 125 total points
ID: 36707716
I am attaching a very simple example of how factory pattern can be used in your case. You can do fancy stuff like singletons and Hashmap based caches. If you go for caching please make sure your Writer instances are thread-safe.


 src.zip
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 84
firstswap challenge 20 77
sites similar to codingbat to improve coding hanson skills 3 49
difference of if loops 23 49
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now