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.
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 ]
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() )
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.
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".
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.
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 ]