After ~10 months of procedural PHP, I'm now trying to wrap my head around basic OOP principles and design patterns. This is a hobby, and I haven't nearly as much time as I'd like to pursue it, so please forgive the rather low level of this question.
My site (currently 100% procedural) is at heart a library. Visitors send the Library script 2 datapoints - an item type
and item code
.
Library.php
uses the item type to select an include, and the include grabs the code to hit the database and then build the page.
Some examples:
[type] [code]
game RoTo
map 32
unit 216
An example link would be library.php?type=game&code=RoTo
Everything works nicely as is, but as I get started with OOP I see obvious easy entry points and inheritance paths for "objectifying" this system.
class LibraryObject
{
protected $_name;
protected $_description;
}
class Game extends LibraryObject
{
protected $_releaseDate;
etc.
}
I'm also excited about the flexibility some well-written classes will give me.
The design pattern idea is tripping me up, though. It seems like a Factory pattern, but I'm confused about the differences between F and AF. I've read other SO questions specifically asking that question, and I've read the examples on OODesign but I feel like they're written in a different language and it's rather frustrating.
Perhaps if someone could explain it using my own data structures it would make more sense to me?
Sorry for the trouble.
The difference between Factory and Abstract Factory is pretty simple. In the latter, the factory itself is abstract (!) and cannot be instantiated directly, but must be sub-classed.
Per example, Factory:
class Document {
public function createPage() {
return new Page;
}
}
class LandscapeDocument extends Document {
public function createPage() {
return new LandscapePage;
}
}
In Abstract Factory:
abstract class Document {
abstract public function createPage();
}
class PortraitDocument extends Document {
public function createPage() {
return new PortraitPage;
}
}
class LandscapeDocument extends Document {
public function createPage() {
return new LandscapePage;
}
}
In short, the Factory pattern has a default implementation in the factory class itself. The Abstract Factory requires all sub-classes to implement their own version of the factory methods.
That's all there is to it.