Forums Register Login

Need an idea to improve design...

+Pie Number of slices to send: Send
In the below code I have implemented the polymorphism concept.
I appreciate ideas to improve the code standard




I don't want switch case to be part of my code...
is there any better idea that replaces the switch case...

Thanks!
[ June 22, 2006: Message edited by: S Thanigaivel ]
+Pie Number of slices to send: Send
Hi

What polymorphism gives you is the ability to deal with a Printer without caring which printer it is. So anywhere you use a printer in your code it would br like:


You can litter your code with statements like that without being tied to an implementation. However, at some point you still need to decide on the printer to instantiate:



For your example, you are using user input to determine the printer. So you could have a getPrinter() method the returns a Printer object. The getPrinter() does the case statement to instantiate the printer.

Now, you could go even further, by delegating Printer creation to another object. Objects that create families of similar objects (all the may kinds of printers) are called 'factories' and the factory pattern is a well recognised software design.

This is the beginner forum so im not going to weigh you down with details, but by centralising the creation of Printers you isolate an area of potential change. If you need to change how you create Printers you only need to do it in one place. All your calling code is protected.

Hope this helps

Ramen
+Pie Number of slices to send: Send
Hi Ramen,

Surely your ideas were helpful to me. Thanks a lot!

Thanigaivel S.
+Pie Number of slices to send: Send
Hi,

I have made the following changes to PrinterManager class.
Now will it be right if i call the PrinterManager as a Factory class and getPrinter() as Factory method.



Thanks!
[ June 22, 2006: Message edited by: S Thanigaivel ]
+Pie Number of slices to send: Send
What if this code happened during startup:

How could you replace your switch/case now?
+Pie Number of slices to send: Send
Hi,

I hope Stan James suggested me to make use of hash table at start up.
I tried the same, but ended up with confusions...

So I took the below approach...

Here PrinterManager class delegates printer Instantiation task to printerBank class.











I invite forum members suggesstions and comments on this design

Thanks!
+Pie Number of slices to send: Send
Hi

I think what Stan is getting at is this; you have a set number of printers you support. You identify them by the numbers 1, 2, 3, etc. Ok so far. In your program you interpret 1 as a type of printer.

However, you could just as easily create all you printers up front and store them in something. Now you can refer to the printer by its position in the storage mechanism. All the client needs to know is the position of the printer (e.g. 1 = laser printer).

Take this one step further and you could list the supported printers and their classes externally, say in a properties file. Now you can have a truly flexible system that allows for runtime creation of the supported printers! (using Class.forName(..) and Class.newInstance() )

Regards

Ramen
+Pie Number of slices to send: Send
 

Take this one step further and you could list the supported printers and their classes externally, say in a properties file. Now you can have a truly flexible system that allows for runtime creation of the supported printers! (using Class.forName(..) and Class.newInstance() )



Took the words right out of my mouth. I've done exactly that in several systems. The old "open closed" principle ... the class is open for extension through configuration, but closed for modification of the switch case statement.

+Pie Number of slices to send: Send
 

Originally posted by S Thanigaivel:

I don't want switch case to be part of my code...
is there any better idea that replaces the switch case...

Thanks!


There is a better way to replace all switch case blocks (the same can be said for any conditional logic branch for a given definition of "better" (a mathematician would concur)). The traditional approach is something called a "strategy". You can find out more by googling for "strategy design pattern".

You would start here:
+Pie Number of slices to send: Send
Hello Ramen Chatterjee and Stan James

Here I had assigned the number(Input received from user console) to printer name in property file which can be configured easily. Code works perfectly expect "PrinterFromConfig" where I am hard-coding the path of the properties file.

Here are my questions

(1)How to eliminate the hard coding here.
(2)Is this design ok (or) has to be refined further.
(3)Is the coding standard acceptable.

+Pie Number of slices to send: Send
Any suggestion to remove the hard coded path.
+Pie Number of slices to send: Send
Hi,

Now i have updated my code as below. How to optimize this further? If this is a flexible code, can anyone explain me how flexible is this code by providing suitable enhanments to this same scenario (code).

I developed this code using eclipse and all these sources / classes are under the package pkgPrinter. I have kept the java files in pkgPrinter/src. classes and property file in pkgPrinter/bin.
Now, when I create File reference i m specifying the file path as

fin = new FileInputStream(new File("bin/pkgPrinter/PrinterList.prop").getAbsolutePath());

but i want to specify only the file name i.e., new File("PrinterList.prop")
is that possible to do?

And it will be more useful, if you specify what are all the design pattern we have used in this code and at which point they are used.

Thanks!



Property file: PrinterList.prop









[ June 26, 2006: Message edited by: S Thanigaivel ]

[ June 27, 2006: Message edited by: S Thanigaivel ]

[ June 27, 2006: Message edited by: S Thanigaivel ]
[ June 27, 2006: Message edited by: S Thanigaivel ]
+Pie Number of slices to send: Send
In the PrinterBank's invoke i done the following change to implement the EXIT functionality. Suggest me some better steps to do this.



Thanks!
+Pie Number of slices to send: Send
Hello Thanigaivel

Here is the code to remove hard-coding path.

+Pie Number of slices to send: Send
hi Ramamurthy,

Originally posted by rama murthy:

Here is the code to remove hard-coding path.





But this is not working in my code. I am getting some fatal exception and program get terminated... Any Idea!

Thanks!
+Pie Number of slices to send: Send
It worked perfectly for me.

I executed the code and got results.
+Pie Number of slices to send: Send
I think its due eclipse. Even "fatal exception" msg is displayed in dialog and in the console window.

I welcome more ideas to refine the above code!

Thanks!
This tiny ad will self destruct in five seconds.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1576 times.
Similar Threads
Loop for console input....
how can i use shell32 in my java program
switch ??
input from console
Program example using KeyboardReader
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 05:41:25.