Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need an idea to improve design...  RSS feed

 
S Thanigaivel
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ramen Chatterjee
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
S Thanigaivel
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ramen,

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

Thanigaivel S.
 
S Thanigaivel
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if this code happened during startup:

How could you replace your switch/case now?
 
S Thanigaivel
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Ramen Chatterjee
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
rama murthy
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
rama murthy
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any suggestion to remove the hard coded path.
 
S Thanigaivel
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
S Thanigaivel
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the PrinterBank's invoke i done the following change to implement the EXIT functionality. Suggest me some better steps to do this.



Thanks!
 
rama murthy
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Thanigaivel

Here is the code to remove hard-coding path.

 
S Thanigaivel
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
rama murthy
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It worked perfectly for me.

I executed the code and got results.
 
S Thanigaivel
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!