Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Factory / Abstract Factory confusion

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.

like image 431
Drew Avatar asked Jan 18 '11 01:01

Drew


People also ask

What is difference between Abstract Factory and factory?

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.

What is the difference between Abstract Factory pattern and factory pattern?

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.

Is Abstract Factory a factory of factories?

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.

What is the difference between simple factory and Factory Method?

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.


2 Answers

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.

like image 196
netcoder Avatar answered Oct 08 '22 14:10

netcoder


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

  • A creational pattern where the logic for creating the instance lies in the hands of the Factory class.
  • A Factory Pattern creates only one type of object instance. In your case it would create objects of type LibraryObject assuming that the LibraryObject is top most object in the hierarchy tree.

Abstract Pattern (Factory of Factories)

  • A creational pattern where the logic for creating the instance lies in the hands of the classes that implement the Factory interface / abstract class.
  • An Abstract Factory pattern can create objects of different types, so you can use the concrete implementations of Factory interface / abstract class to create objects of the types that you desire. That is why it is called a 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/

like image 1
Nyal Fernandes Avatar answered Oct 08 '22 14:10

Nyal Fernandes