Purpose of the generic object factory?

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Purpose of the generic object factory?

      What's the purpose of having objects created by a generic factory rather than just creating an object normally? e.g from the gcc source code,

      extern GenericObjectFactory<IEvent, EventType> gEventFactory;
      #define REGISTER_EVENT(eventClass) gEventFactory.Register<eventClass>(eventClass::TYPE)
      #define CREATE_EVENT(eventType) gEventFactory.Create(eventType)
    • The purpose of generic object factories is to decouple the implementation from the rest of the engine. I can instantiate an event without actually including (or even knowing about) its implementation.

      Every time you reference a class directly, you are tightly coupling those classes together. If one changes, it might effect the other one. You want to reduce that coupling as much as possible. One of the common pieces of architecture wisdom is that you should program to interfaces, not implementations. In other words, you should use interface classes and only write code against those interfaces rather than writing code directly against the implementation class. That way, the implementation can change while keeping the interface the same.

      Wikipedia has a decent article on factories in object-oriented architecture: