Re: oop programming

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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" <Reginald_Blue@xxxxxxxxxxx> wrote in message
news:da4gcu$nlq$1@xxxxxxxxxxxxxxxxxxxxxx
> Sharon wrote:
>> "wozza" <wozza96@xxxxxxxxxxxxxxxxxx> wrote in message
>> news:42c5ab87$0$9732$afc38c87@xxxxxxxxxxxxxxxxxxxxxxx
>>> "Sharon" <Sharon669@xxxxxxxxxxx> wrote in message
>>> news:evGnEznfFHA.3916@xxxxxxxxxxxxxxxxxxxxxxx
>>>> 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" <Reginald_Blue@xxxxxxxxxxx> wrote in message
news:da4gcu$nlq$1@xxxxxxxxxxxxxxxxxxxxxx
> Sharon wrote:
>> "wozza" <wozza96@xxxxxxxxxxxxxxxxxx> wrote in message
>> news:42c5ab87$0$9732$afc38c87@xxxxxxxxxxxxxxxxxxxxxxx
>>> "Sharon" <Sharon669@xxxxxxxxxxx> wrote in message
>>> news:evGnEznfFHA.3916@xxxxxxxxxxxxxxxxxxxxxxx
>>>> 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]
>
>


.



Relevant Pages

  • oop programming
    ... The interface represents a general printer, and each class is a specific ... class Hpv40: IfPrinter ... public void printDocument(){ ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Speed of interfaces vs inheritance
    ... implementation of an interface. ... Another thing to note is the degree of actual polymorphism at the call site. ... But that kind of optimisation makes considering call sites difficult. ... abstract public void abstractCall; ...
    (comp.lang.java.programmer)
  • Re: HashMap get/put
    ... interface IFace ... public void DoitNow() ... DoitNow is generated as a non-virtual method. ... But, change DoitNowto be virtual, add an override in Derived, and then rebuild Assembly1.dll. ...
    (comp.lang.java.programmer)
  • Re: pls clarify important concepts...
    ... >>methods in that interface.. ... > public class MyClass implements MyInterface { ... > public void myMethod() { ... > public class UserDaoJDBC implements UserDao { ...
    (comp.lang.java.help)
  • Re: Correct use of State Pattern
    ... Unless you want to use reflection, you will probably need to have a different interface for each state. ... public void becomeImpregnated; ... public Tadpole hatch; ... public Frog metamorphose() { ...
    (comp.lang.java.programmer)