For consultationDipIn many cases, it cannot be associated with a specific class instance.NewKeywords violate this principle. Of course, some classes will not change, suchStringThere is no need to isolate these items. If the specific class is very variable, the dependency will cause problems. How can we avoid using the specific class?Factory.
FactoryThe general meaning of the mode is that the role of creating a specific class instance is handled by the "factory class" and corresponding interfaces are returned. You only need to operate these abstract interfaces, to isolate dependencies with specific classes.
In. NetIn enterprise development, this mode is most commonly used.DalLayer components. In this mode, you can create correspondingDalInstance.ProgramYou can easily create applications that support multiple databases. This application is analyzed as an example.
This model is used inDalLayer mechanism:
Where:
BllA group of called clients, which callDalfactoryReturns the correspondingIdalOperation Method.
IdalA group of method interfaces called by the customer, such as the return order number.
DalfactoryFor the factory classBllCall the corresponding static method and return the correspondingIdalImplementation class instance.
Sqlserverdal,OracledalImplementationDalOfSqlserverImplementation andOracleImplementation: there is a way to deal directly with the database.
In this way,DalfactoryBy reading the configuration file. NetReflection mechanism to create an instance of the corresponding database. For the designed software, you only need to change the corresponding configuration to change the database.