Page 1 of 1
OO Design Question
Posted: Mon Feb 18, 2008 10:28 am
by Sekka
I need to consult an OO boffin of sorts. I am having trouble getting my head around part of the OO design concept.
I have made a class that will control a blog post. It will load it, alter it, update it, etc, basically control all the data relating to that blog post only.
Now, I want to display on my website a list of recent blog articles. Now, the best way I see of doing this is just to query the DB for the latest posts and display the data. But, this makes my class relatively useless as I am bypassing it completely.
I then started forward thinking to when I would actually use my class, and the only time I think I would use it is when I am loading a specific blog post page or just loading data related to 1 blog post, or when I am administering it through the CMS.
Does this sound right or have I deviated from the OO path in my system?
Re: OO Design Question
Posted: Mon Feb 18, 2008 11:22 am
by aliasxneo
Use a static function.
That's the way I always do it. Since most of my objects take an ID in the constructor which it uses to wrap around a specific entry in the database, using static functions was pretty much my only option for handling more than one object.
Re: OO Design Question
Posted: Mon Feb 18, 2008 12:37 pm
by Christopher
If you have a class that defines one blog post, then just create another class that finds and returns a list of blog post objects.
Re: OO Design Question
Posted: Mon Feb 18, 2008 2:32 pm
by Sekka
Thanks for the replies.
I've been thinking about this since I left work this afternoon.
What I'm probably going to do is create a base class that holds the data for a specific post, and the methods that format that data in a specific way. This will essentially be a data container that is simply passed raw data to store and output on request.
I can then build around it other classes that use and extend it. For example,
* A method to get the recent posts can query the posts and create an array of data container classes to be used else where.
* A class can extend the data container for admin purposes and manipulate and store the data.
This will give me a nice degree of separation I think and allow me to accomplish what I need.
This sound like OO implementation or am I completely off the reservation?
Re: OO Design Question
Posted: Mon Feb 18, 2008 3:05 pm
by Christopher
I would not create a base class until you know you need it. Have you thought about Row Data Gateway or Table Data Gateway for this?
Re: OO Design Question
Posted: Mon Feb 18, 2008 3:25 pm
by Sekka
I just Googled those.
My core classes essentially are those at the moment. They have no display logic or anything, they simply load data, allow manipulation through the API, and then update the data, etc.
I just need to rethink how I have things laid out. I definitely need to separate the admin methods from my class anyway as they are rarely needed.
Re: OO Design Question
Posted: Thu Feb 28, 2008 10:35 pm
by hongster
Sekka wrote:... Now, I want to display on my website a list of recent blog articles. Now, the best way I see of doing this is just to query the DB for the latest posts and display the data. But, this makes my class relatively useless as I am bypassing it completely. ...
Yes, you can bypass the class completely but the is procedural programming. One of the features of OO programming is encapsulation. The retrieval of data from DB can be placed in a class. When the method of retrieving data is changed (e.g. different database, reading from text file, ...), changes to your code will be limited to 1 class. It give you flexibility.
A class is useful for grouping related functions. These functions may have a tight coupling with each other. Data that are shared among these functions will become the class's members.
These are just some of my thoughts, not sure if it helps you

Re: OO Design Question
Posted: Fri Feb 29, 2008 1:13 am
by Kieran Huggins
Embed the DB calls (Create, Read, Update, Delete) in the Post class. You can then use it to control all the data for each blog post.
If you want to get a series of posts, you can fetch the IDs of matching posts and instantiate a Post object for each one.
There ways to make this more efficient, but it's a damn fine place to start when earning OOP. Refactor when you feel the pinch, keep it simple until then
We're here to help!
Re: OO Design Question
Posted: Fri Feb 29, 2008 7:59 am
by Sekka
Thanks for the comments guys.
I ended up writing like an 'interface' class if you will with methods for loading multiple blog posts, then those methods returned an array of objects.
I realised I was thinking too much like a pre-optimiser and I needed to start focusing on OO, which I have done.
Thanks!