473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

oop programming

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

}


Nov 17 '05 #1
6 999
"Sharon" <Sh*******@hotmail.com> wrote in message
news:ev**************@tk2msftngp13.phx.gbl...
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?


You have a separate PrinterFactory object that can return a reference to the
correct type.
Nov 17 '05 #2
how do i implement it ?
shuld i add new PrinterFactory class ?
"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...
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?


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

Nov 17 '05 #3
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]
Nov 17 '05 #4
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]

Nov 17 '05 #5
Sharon wrote:
so it will be somthing like :

<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]
Nov 17 '05 #6
Thank you !
"Reginald Blue" <Re***********@hotmail.com> wrote in message
news:da**********@trsvr.tr.unisys.com...
Sharon wrote:
so it will be somthing like :

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

Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Martin | last post by:
When was inheritance intruduced into object oriented programming? More generally, does anyone know or have any sources on when the different features were introduced into object oriented...
12
by: G. | last post by:
Hi all, During my degree, BEng (Hons) Electronics and Communications Engineering, we did C programming every year, but I never kept it up, as I had no interest and didn't see the point. But now...
3
by: user | last post by:
Hi all, At the outset, I regret having to post this slightly OT post here. However, I strongly feel that people in this group would be the best to advise me on my predicament. I am working as...
134
by: evolnet.regular | last post by:
I've been utilising C for lots of small and a few medium-sized personal projects over the course of the past decade, and I've realised lately just how little progress it's made since then. I've...
7
by: Robert Seacord | last post by:
The CERT/CC has just deployed a new web site dedicated to developing secure coding standards for the C programming language, C++, and eventually other programming language. We have already...
30
by: Jakle | last post by:
I have been googling, but can seem to find out about C GUI libraries. My main platform is Windows, but it would be nice to find a cross platform library. I've been programming with php, which...
47
by: Thierry Chappuis | last post by:
Hi, I'm interested in techniques used to program in an object-oriented way using the C ANSI language. I'm studying the GObject library and Laurent Deniau's OOPC framework published on his web...
111
by: Enteng | last post by:
Hi I'm thinking about learning C as my first programming language. Would you recommend it? Also how do you suggest that I learn it?What books/tutorials should I read for someone like me? Thanks...
14
by: deko | last post by:
For building Windows desktop apps, the clear favorite is C#. But my clients can't afford to buy Microsoft products. So I need to develop software for Linux users and web applications. In the...
17
by: CoreyWhite | last post by:
I bought this book years ago, when I was just learning C++. Since then I've gone through every math course offered at my college, taken courses on coding C & thinking in terms how how to make the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.