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 main difference between a “factory method” and an “abstract factory” is that the factory method is a single method, and an abstract factory is an object. The factory method is just a method, it can be overridden in a subclass, whereas the abstract factory is an object that has multiple factory methods on it.
Factory Method pattern is responsible for creating products that belong to one family, while Abstract Factory pattern deals with multiple families of products. Factory Method uses interfaces and abstract classes to decouple the client from the generator class and the resulting products.
Abstract Factory patterns work around a super-factory which creates other factories. This factory is also called as factory of factories. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.
Use the Factory method when you want to make a Framework where you want to take a control from the creation of the Object to the management of that Object. That's unlike the Simple factory, where you only care about the creation of a product, not how to create and manage it.
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.
Here is another way you can look at it:
To clear the bushes:
A factory pattern is a creational pattern. That is it is used to create instances for use.
Factory Pattern
Abstract Pattern (Factory of Factories)
A good reference would be this link below, I would suggest you read the Factory Method pattern as well:
http://www.oodesign.com/creational-patterns/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With