so it will be somthing like :
ifPrinter printerObj = PrinterFactory.getPrinterObject("EpsonP333");
printDocument.setDocument(d1);
printerObj.printDocument();
class PrinterFactory{
public IfPrinter getPrinterObject(string printerName){
if(printerName.equels("Hpv40")
return new Hpv40();
else if(printerName.equels("EpsonP333")
return new EpsonP333();
.....
}
}
interface IfPrinter
{
void printDocument();
void rollEmptyPage();
void cleanPrinterHead();
string getPrinterType();
}
class Hpv40 : IfPrinter
{
public void printDocument(){
do Hpv40 printing ...
}
public string getPrinterType(){ return this.type;}
}
class EpsonP333 : IfPrinter
{
public void printDocument(){
do EpsonP333 printing ...
}
public string getPrinterType(){ return this.type;}
}
class Cannon512: IfPrinter
{
public void printDocument(){
do Cannon512 printing ...
}
public string getPrinterType(){ return this.type;}
}
"Reginald Blue" <Re***********@hotmail.com> wrote in message
news:da**********@trsvr.tr.unisys.com...
Sharon wrote: "wozza" <wozza96@_NO_SPAM_yahoo.com> wrote in message
news:42**********************@news.optusnet.com.au ... "Sharon" <Sh*******@hotmail.com> wrote in message
news:ev**************@tk2msftngp13.phx.gbl...
I have a logical oop problem; I have an interface, and derived
classes
The interface represents a general printer, and each class is a
specific printer
The question is what is the correct way to get a new instance of a
printer by its name?
Where should the getPrinterObject function be?
You have a separate PrinterFactory object that can return a
reference to the correct type.
shuld i add new PrinterFactory class ?
Yes.
how do i implement it ?
Depends on what you're trying to accomplish in the long term. Generally,
you'd have the factory look up the right thing to instantiate, but look up
from where is the main question. Perhaps just a case statement. But
maybe
you want something more dynamic, so you could have it load a new assembly
from disk.
So, generally, it's:
1. Receive request for creation of specific type.
2. Find specific type.
3. Create new instance of specific type.
4. Return new instance (via interface).
--
Reginald Blue
"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my
telephone."
- Bjarne Stroustrup (originator of C++) [quoted at the 2003
International Conference on Intelligent User Interfaces]
"Reginald Blue" <Re***********@hotmail.com> wrote in message
news:da**********@trsvr.tr.unisys.com... Sharon wrote: "wozza" <wozza96@_NO_SPAM_yahoo.com> wrote in message
news:42**********************@news.optusnet.com.au ... "Sharon" <Sh*******@hotmail.com> wrote in message
news:ev**************@tk2msftngp13.phx.gbl...
I have a logical oop problem; I have an interface, and derived
classes
The interface represents a general printer, and each class is a
specific printer
The question is what is the correct way to get a new instance of a
printer by its name?
Where should the getPrinterObject function be?
You have a separate PrinterFactory object that can return a
reference to the correct type.
shuld i add new PrinterFactory class ?
Yes.
how do i implement it ?
Depends on what you're trying to accomplish in the long term. Generally,
you'd have the factory look up the right thing to instantiate, but look up
from where is the main question. Perhaps just a case statement. But
maybe
you want something more dynamic, so you could have it load a new assembly
from disk.
So, generally, it's:
1. Receive request for creation of specific type.
2. Find specific type.
3. Create new instance of specific type.
4. Return new instance (via interface).
--
Reginald Blue
"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my
telephone."
- Bjarne Stroustrup (originator of C++) [quoted at the 2003
International Conference on Intelligent User Interfaces]