all groups > c# > july 2005 >
You're in the

c#

group:

oop programming


Re: oop programming Reginald Blue
7/1/2005 6:34:39 PM
c#:
[quoted text, click to view]

Yes.

[quoted text, click to view]

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]

Re: oop programming Reginald Blue
7/1/2005 7:09:13 PM
[quoted text, click to view]
<code elided>

Yup, that's the factory pattern.

--
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]

oop programming Sharon
7/1/2005 11:37:44 PM
Hi all,

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?







Example:





ifPrinter printerObj = getPrinterObject("EpsonP333");



printDocument.setDocument(d1);

printerObj.printDocument();





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;}

}



Re: oop programming Sharon
7/1/2005 11:56:11 PM
how do i implement it ?
shuld i add new PrinterFactory class ?


[quoted text, click to view]

Re: oop programming Sharon
7/2/2005 12:00:00 AM
Thank you !


[quoted text, click to view]

Re: oop programming wozza
7/2/2005 12:00:00 AM
[quoted text, click to view]

You have a separate PrinterFactory object that can return a reference to the
correct type.

Re: oop programming Sharon
7/2/2005 12:00:00 AM
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;}

}











[quoted text, click to view]


[quoted text, click to view]

AddThis Social Bookmark Button