about
12/05/2022
ObjectFactories Repo
ObjectFactories code

ObjectFactories  Repository

Ways of building abstract creational facilities for Dependency Inversion

Quick Status Code functions correctly no known defects Demonstration code yes Documentation yes Test cases none planned Static library none planned Build requires C++17 option Planned design changes None
Fig 1. Widget Factory
Fig 1. Abstract Factory

1.0 Concept

When clients bind to an interface implemented by some class, they bind to the invariant text of the interface, not the possibly varying class implementation. So any changes to the class's implementation won't cause build failures.
However, if the client creates an instance of the class by calling new and binds that to an interface pointer, the creation process binds the client to the implementation (the client had to use the class name).
Object factories allow us to bind not only to the interface abstraction, but also to the factory abstraction for creating instances. As long as we don't put any implementation details in the factory's header file the client only binds to abstractions and is not dependent on the specific class implementation.
Dependency Inversion is the name we use for this approach. Instead of clients depending on implementation details, both the client and the implementation depend on abstractions: Object Factory for creation, and Interface for operation.

2.0 Design

This repository contains three example Dependency Inversion Principle (DIP) designs:
  • Widget Factory A classic form of DIP and most frequently used.
  • Abstract Factory Supports adding additional interfaces to provide language needed by new functionality, without breaking clients using the original capability.
    In this design, a class that needs to be modified inherits from the originial interface, and adds inheritance from a new interface.
    Clients only using the original capability have pointers only to that interface. Clients that need the new functionality obtain a pointer to the new interface using dynamic_cast.
  • Program To Interface This example builds a factory using a map with keys that identify classes used to create instances, and values that are creational functions for for instances of each class.

3.0 Build

These demonstration codes were built with Visual Studio Community Edition - 2019 and tested on Windows 10.
  Next Prev Pages Sections About Keys