The abstract factory mode may be ProgramThe most familiar design pattern of the Members. It usually seems that no hero is talking about this (maybe afraid of price loss), especially in. under the net development platform, the light of the abstract factory has long been covered by reflection technology, and the status of the abstract factory model has gradually declined, gradually fading out our sight.
The purpose of the abstract engineering mode is to provide an interface for creating correlated or dependent objects or object families. The main purpose of the abstract factory is to strip the dependency between the consumer of the object and the specific object, so that the object can be changed without touching the consumer of the object.
A key point or necessary condition for using an abstract factory is that the objects to be created are related or mutually dependent. For example, a random image production factory needs to create a background for the image. The interface is as follows:
Public interface istagefactory
{
Public ibackground createbackground ();
Public iforeground createforeground ();
}
Because the two objects created in this scenario are directly correlated, the prospect of a fish cannot be displayed on the desert background.
If there is no correlation or dependency between the created objects, do not use the abstract factory. In this application scenario, the abstract factory is a collection object, introduce unnecessary dependencies to the consumer of the factory interface (maybe the use class of a factory does not need to depend on a type created by the factory at all), and does not meet the single responsibility principle. In addition, the laziness of programmers may combine the responsibilities of creating more unnecessary types into the factory interface.
However, if the created object is correlated or interdependent, it is best to use the abstract factory mode. Although reflection can achieve the same purpose, reflection constraints are not mandatory, errors may be introduced in the configuration information, making it difficult to track and discover bugs.