Topic: Is there an OOP way to solve this problem ?
Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Thu, 29 Dec 1994 23:55:29 GMT Raw View
In article <3d5335$m9j@mozo.cc.purdue.edu> kavuri@lips.ecn.purdue.edu (Surya N Kavuri ) writes:
>Net Gurus:
>
>The idea of using information on the type of an object
>to perform an action is considered non-oop.
Not quite. IMHO: using TYPE information is fine, but
using CLASS information is not. And C++ provides only the latter:
RTTI is not really type information. (The class tells you not
only what type it is, but also how its attributes are represented)
In particular, knowing what type something is when it could
be more than one, is necessary and supported in some languages with
discriminated unions. C++ does not provide this vital facility,
and the emulation of it is horrendous. RTTI is the easiest way to
get around this deficiency. :-(
>
>Suppose I have:
>class Employee
>class Salaried and class Waged derived from class Employee
>class Engineer and class Salesman derived from Salaried
>class Chemical, Mechanical and Electrical derived from
>Engineer.
>
>If I have a collection of Engineers and I need to find the
>average salary of chemical engineers I can do it very well
>with double dispatch you discussed.
If you have a collection of Engineers and you need
to find the average salary of chemical engineers, you have a
problem. You need to discriminate type. To do this, you should
be using a discriminated union in the collection OR keep
separate collections. (I tend to do the latter).
In principle, this kind of conditional processing
is best done with flags and switches. IMHO.
>Suppose I have a collection of all Employees. Can I use
>a similar technique to easily compute:
>Average salaries of all Engineers
>Average salaries of all chemical engineers
>etc.,
>
>It appears using type information simplifies this well
>but without too much complexity can we develop an
>object-oriented view ?
What you are trying to do is not, IMHO, a good use of object
orientation.
When you want common attributes, use the global collection,
when you want to select on kind, use the collection built for that
purpose. The subcollection of all "chemical engineers with
pay between 100K and 200K with PhD from non-US universities,
and who are married with 3-5 kids aged less than 12" is the kind
of query that a traditional relational database handles superbly.
And they do it with homogenous collections entirely.
>The part I dislike about
>using a virtual function to provide a creation_type
>is that the base class should know about the derived
>class type. (This is bad both aesthetically and in
>practice when the base class is not available in
>source).
>
>Is there an oop way of dealing with the above problem ?
Yes. Dont. Use a relational database. This kind of
problem has already been solved.
If you really want to use OOP, you can use agents.
"Teach" each agent to understand questions in some query language
and send them messages asking questions. Collect and analyse the answers.
This is called doing a survey. This is highly dynamic, and well suited
to more dynamic languages like Smalltalk and LISP than C++.
C++ is a more traditional language which solves a smaller set
of fixed problems FAST -- but you have to write code to solve
each problem that way. To solve more general problems without
compilation requires an interpreter (there is no way to escape
having to "program" the problem, only a way to make the
programming more declarative and more "natural").
--
JOHN (MAX) SKALLER, INTERNET:maxtal@suphys.physics.su.oz.au
Maxtal Pty Ltd,
81A Glebe Point Rd, GLEBE Mem: SA IT/9/22,SC22/WG21
NSW 2037, AUSTRALIA Phone: 61-2-566-2189
Author: kavuri@lips.ecn.purdue.edu (Surya N Kavuri )
Date: 19 Dec 1994 22:56:37 GMT Raw View
Net Gurus:
The idea of using information on the type of an object
to perform an action is considered non-oop.
Suppose I have:
class Employee
class Salaried and class Waged derived from class Employee
class Engineer and class Salesman derived from Salaried
class Chemical, Mechanical and Electrical derived from
Engineer.
If I have a collection of Engineers and I need to find the
average salary of chemical engineers I can do it very well
with double dispatch you discussed.
Suppose I have a collection of all Employees. Can I use
a similar technique to easily compute:
Average salaries of all Engineers
Average salaries of all chemical engineers
etc.,
It appears using type information simplifies this well
but without too much complexity can we develop an
object-oriented view ? The part I dislike about
using a virtual function to provide a creation_type
is that the base class should know about the derived
class type. (This is bad both aesthetically and in
practice when the base class is not available in
source).
Is there an oop way of dealing with the above problem ?
Surya Kavuri
Author: ruurd@autpels.maxine.wlink.nl (Ruurd Pels)
Date: 21 Dec 1994 02:05:56 +0100 Raw View
Surya N Kavuri <kavuri@lips.ecn.purdue.edu> wrote:
>The idea of using information on the type of an object
>to perform an action is considered non-oop.
>
>Suppose I have:
>class Employee
>class Salaried and class Waged derived from class Employee
Hmmm. An employee HAS-A wage or HAS-A salary. Besides, it has difficulties.
>class Engineer and class Salesman derived from Salaried
>class Chemical, Mechanical and Electrical derived from Engineer.
>Suppose I have a collection of all Employees. Can I use
>a similar technique to easily compute:
>Average salaries of all Engineers
>Average salaries of all chemical engineers
>etc.,
You might want to derive Salary and Wage from EmployeePayments, and include
a EmployeePayment pointer in Employee. Furthermore, in the inheritance tree
of Employee, you might have to use a type field in Employee, or use RTTI
to select those objects that match your selection. Then, a collection of
Employee* would make it a rather flexible way to deal with that.
--
Grtz, RFP ;-)
|o| Ruurd Pels, Kamgras 187, 8935 EJ Leeuwarden, The Netherlands |o|
|o| EU.net!sun4nl!wtrlnd!maxine!autpels!ruurd |o|