[Zope] Gaining Enlightenment

jiva@devware.com jiva@devware.com
Tue, 7 Mar 2000 10:11:02 -0700


I don't claim to be an OO master or a ZOPE master, but I'll give this
a shot anyway and share some recent revelations I have had about OO in
general.

First off, the examples that people always use to demonstrate OO
concepts wind up being more distracting than they are worth.  For
instance modeling a can of spam in OO.  That would be a logical thing
to do, if you were writing a control program for a can of spam.  But
NOT if you were writing a database app!  This, I think is one of the
most confusing parts for database programmers coming to OO.

Second, so if you don't model your can of spam, then what DO you
model?  Well, you model *your data* and if your data is a database,
then that's what you model!  Soo, you create an object of type
"Products_Table" which contains methods to operate on the entire
table.  Then you create objects of types "Products" which contain
methods to operate on the objects.  But since your products can be
many different kinds, you don't "hard code" what kind of product they
are, you just make a generic object that contains generic functions.
If you wind up needing to do something special for a particular
product, *then* you create a subclass of product for that kind of
product that you can use for that.  Ie:  "Delicate_Product" might be a
subclass of "Product" that might have a special some special methods
for dealing with products of that type.  Something like "find" should
be a method of the table that returns an instance of your product.

So, your loop becomes something like this (pseudo code that looks like
c++?):

while(sData = Input.Read())
{
	MyProduct = new Product(sData); // object init's itself
	if(MyProduct.Validate())
	{
		MyProduct.Write(Table);
	}
	delete MyProduct;
}

On Tue, Mar 07, 2000 at 09:47:21AM +0000, Richard Moon wrote:
> Dear Zopers,
> 
> I read Chris McDonough's How To on Gaining Zope Enlightenment by Grokking 
> Object Orientation. It raised a few questions in my RDBMS-oriented head. So 
> I mailed Chris with these questions but Chris explained he just couldn't 
> answer them right now and asked me to post them to the list. They seem very 
> basic, very fundamental questions to me.
> 
> So here is my email to Chris - If I get any answers here I would like to 
> include my questions and your answers in a How-To to help those who come 
> after me !
> 
> Thanks for any help !
> 
> Here's the original email
> 
> 
> Chris,
> I enjoyed your How-To on Gaining Enlightenment very much. I've been using 
> Zope for a couple of months now and I've made some progress - my first 
> project has been to use it to give access to established databases over an 
> Intranet. Like you I think its wonderful, but like you I keep getting 
> stuck. Never mind, with the help of the mailing lists and long hours 
> reading books on Python etc I've made progress.
> BUT ... (there's always a but isn't there) - no matter how much I read and 
> play around with OO - I still don't 'get it'. My background is many years 
> working with relational databases on Unix - (and there are thousands like me).
> I just don't get the bit about storing data as objects. Could we discuss 
> this a bit more and perhaps publish my questions and your answers on 
> Zope.org ?? This may help me and the thousands like me.
> OK, you create your class of Spam, which has attributes of cansize and 
> texture and it has methods that display these attributes. No doubt you 
> could add other attributes, such as selling price and develop a method to 
> increase the price by a percentage. OK so far ?
> Now we create an object (which is an instance of the class Spam) - which we 
> call myfirstcan. We can define the attributes for that object when we 
> create it or later and we can use the methods defined in the Spam class to 
> do so. OK so far ?
> 
> What I don't get is how you store and retrieve the objects you create. 
> Let's go to a more realworld example - how would you implement a stock 
> system using objects ? Let's take the simple table Stock which has three 
> columns
> Product_code Description Price
> and we could enter rows of data into it, for example -
> ABC Spluggifier 20.00
> CDE Super-spluggifier 25.00
> 
> Now if I was writing a conventional database system I would maintain the 
> above by defining a program which perhaps prompted the user for the 
> required columns (Product, Description,Price) and then I would store the 
> users input in a row in a table. Perhaps another program (the price change 
> program) would use a stored procedure to allow price change. OK ?
> Now in OO terms I see that I could create a class of Stock with attributes 
> Product, Description and Price. I could create a method to allow price 
> changes. Very neat.
> Then I can create an object( i.e a particular stock item) by saying 
> something like
> product_1 = Stock(product_code="ABC", Description="Spluggifier", Price-"20.00")
> Now I can create another object by saying
> product_2 = etc
> but that is not the way you would do it surely. You would be effectively 
> 'hard-coding' each product in your system.
> Surely you would have a program which did something like
> WHILE the user is entering data
> PROMPT user for data
> VALIDATE data
> WRITE data to database
> END WHILE
> How do you do that in OO terms ?
> How then do you search and retrieve an object with a particular value of 
> product_code ?
> How do you write a program which updates the price of all stock items where 
> the product_code begins with AB by 2%.?
> Thanks for reading this far. Hope you can stop my head spinning !
> 
> Regards
> 
> Richard
> 
> 
> 
> Richard Moon
> richard@dcs.co.uk
> 
> 
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists - 
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope-dev )

-- 
If it were not for the presents, an elopement would be preferable.
		-- George Ade, "Forty Modern Fables"