From: Corey Date: 2004-11-12T06:42:01+09:00 Subject: advice for a class implementation? ( ugh, long... ) Hello - Ok, as I'm getting up to speed w/ ruby, I decided to write an absurdly over-featured quote generator. My question is more of a general OOP design issue, hope no one minds! I'll try to keep things terse. [ sorry... this post ended up becoming _way_ more involved than I anticipated... yikes. ] First, the purpose of this program is to provide lots ( and lots... ) of control over the maintainance and interaction of a collection of quotes. I decided to use xml so I could get some experience w/ rexml. ( I'll later be expanding the functionality to a sql db store as well. ) Here's an example of my xml representation of a quote: Primus Frizzle Fry To defy the laws of tradition - is a crusade, only of the brave. http://lyrics.rare-lyrics.com/P/Primus/To-Defy-The-Laws-Of-Tradition.html blah blah blah ( yeah, I know... but remember this is _purposefully_ over-engineered ) So, after some thought I figured I have two classes here: "Quote", and "QuoteCollection". ( a single Quote doesn't need an 'id' attribute, but a Quote within a QuoteCollection does. ) A Quote obviously consists of nothing more than the information/attributes which I have defined to represent a single quote object - it needs to be responsible for: 1- generating brand new quotes 2- accessing each attribute 3- writing/modifying each attribute 4- instantiating new quotes pulled from a QuoteCollection (here's the problem, see further down below) A Quote's attributes/accessors would be the following: quote.source quote.origin quote.attribution quote.medium quote.style quote.category quote.subject quote.quotation quote.url quote.comment Then I have the QuoteCollection ( it'll be Enumerable ) class, which is exactly what it says it is - being responsible for the following: 1- selecting random quotes 2- allow searching of quotes 3- modifying existing quotes 4- inserting new quotes 5- removing quotes 6- viewing all authors, sources, mediums, origins, categories & subjects ( for increased assistance in searching ) A QuoteCollection would contain many individual quotes, each marked up in xml; an extra attribute, 'id', would be necessary for a QuoteCollection Quote. A QuoteCollection's attributes/accessors would be the following: qc.authors qc.sources qc.mediums qc.origins qc.categories qc.subjects qc.quote ( qc.quote would be a Quote object, whichever one was currently being operated on; so most/all the QuoteCollection methods would be passing around and operating on actual whole Quote objects, ( as opposed to quote xml strings, or single Quote/quote attributes/values ) ) SO, my question concerns how best ( or most proper ) to internaly represent a Quote object within the Quote class... Since I opted to go the route of using an xml file for the persistence of QuoteCollections, it would seem natural that the Quote class would represent Quotes as xml also -- but the problem is that I intend to later extend QuoteCollection to be able to use either an xml file backend _or_ an sql database backend. I'm confused because I'm thinking that a Quote is simply a collection of a particular set of attributes which expresses what I have defined to be a quote - the format which I decide to persist those attributes shouldn't be relevent (certainly shouldn't be hardcoded/assumed - for I later expand QuoteCollection to use either an xml file _or_ a sql db backend ). Thanks for bearing with me so far... I hope I can word the rest so that you know what the hell it is that I'm on about! How should an xml QuoteCollection interact with a Quote object? They're tightly coupled: I believe I'll be mixing-in Quote into QuoteCollection, and QuoteCollection will be returning and dealing with individual Quote objects... so I guess when a QuoteCollection is instantiated in order to, say, modify an individual quote - that it needs to pull this xml'ified quote and then make an actual Quote object out of it, so that it can then be edited ( see #2/3 of the Quote responsibilities above ), after which this modified Quote instance/object would of course then need to be inserted back into the QuoteCollection - which means it would need to be re-translated back into xml form... upon which is looses it's "Quote'iness"?? This all seems horribly inefficient and awkward... I realize that I'm missing something, and that I'm definitely making it more complex than it needs - but I'm pretending that this is a "professional grade" api, so I'd like to approach and implement it in the same way an expert/professional would, which is why I'm abstracting Quote from QuoteCollection; pretending that a Quote would be re-usable in a variety of different cases -- not only within the context of an xml-file-persisted program, and in fact not necessarily within the context of xml whatsoever. So how should I internally represent a Quote object? Should I represent Quote's as a hash or struct, and define a 'xml_to_quote' and 'quote_to_xml' method? Where would these methods logically go, in Quote or QuoteCollection? Should I just go and make it a trait of Quote's to be xml? i.e., define a "quote" as not only a particular collection of attributes, but rather a particular collection of attributes which are marked up in xml? Remember that later on ( for further learning ) I intend on allowing a QuoteCollection to be persisted in a sql backend, so such a requirement would seem strange - I'd be stuck doing the same sort of awkward/inefficient translation between simple data/strings and xml. Maybe I need an abstract class? If you've actualy followed me this far ( amazing! ) - thanks! All clues/hints/advice very much appreciated, thankyou for your time! Phew... Corey -- "Giving money and power to government is like giving whiskey and car keys to teenage boys." - P.J. ORourke