While building solution over force.com platform, you can easily observe MVC pattern across the platform, but when you scale to enterprise level solution, design pattern you chose, on top of MVC play a key role in putting direction the custom development and specially designing enterprise level of services. There are common design patterns and associated best practices for Apex Developer and how can you leverage those patterns.
Supported Keywords in Apex
Abstract Factory pattern
When to use Factory and Abstract Factory Pattern ?
Using Singleton Pattern in Factory Pattern
Another important aspect to consider when writing your factory class is that, it does not make sense to create a new factory object for each invocation as it is shown in the sample code, which is just fine for the illustration purpose.
InstallFactory factory = new SimpleInstallFactory();
To overcome this, you can incorporate the singleton design pattern into your factory pattern code. The singleton design pattern will create only a single instance of your SimpleInstallFactory class.
Since an abstract factory pattern is unlike factory pattern, where you need to have an instance for each of the two factories (i.e. SimpleInstallFactory and ComplexInstallFactory) returned, you can still incorporate the singleton pattern as an access point and have an instance of a HashMap, store your instances of both factories.
What is Singleton Pattern?￼
A singleton is a class that can be instantiated only one time. Repeated calls always return the same instance. Ensures that a class has only one instance, and provide a global point of access.