Think of the factory class creation method, here is an example written many years ago
1, provides a property for external access
2, the constructor initializes the property
3, create a subclass factory to inherit the properties of the implemented business class
Public Abstract classBllfactory { Public StaticBllfactory Instance {Get;Private Set; } Staticbllfactory () {stringTypeName = configurationmanager.appsettings["BLL"];//The value of the BLL in the configuration file is HomefactoryType t = Type.GetType (TypeName,true,true); Objectobj =activator.createinstance (t); Instance=(bllfactory) obj; } //interface of the user Management module business Public AbstractIUSERBLL USERBLL {Get; } //interfaces for other module services Public AbstractIOTHERBLL OTHERBLL {Get; } //....}
4,IUSERBLL Interface Implementation
Public InterfaceIUSERBLL {/// <summary> ///Account Registration/// </summary> /// <param name= "user" >User basic Information</param> /// <returns>whether the registration was successful</returns> BOOLRegister (userdto user); /// <summary> ///Account Login/// </summary> /// <param name= "user" >Account/Password</param> /// <param name= "ds" >User basic Information</param> /// <returns>whether the login was successful</returns> BOOLLogin (userdto user,refDataTable DT); }
5, sub-class factory Homefactory implementation
Public classHomefactory:bllfactory { Public OverrideIUSERBLL USERBLL {Get { return NewUSERBLL (); } } Public OverrideIOTHERBLL OTHERBLL {Get { return NewOTHERBLL (); } } //...}
6,USERBLL implementation of IUSERBLL interface method
Publicclass userbll:iuserbll { #region IUSERBLL member BOOL iuserbll.register (userdto user) { // Code implementation logic } boolref DataTable dt) { // Code implementation Logic } #endregion }
7,IOTHERBLL,OTHERBLL can refer to the above IUSERBLL,USERBLL implementation
The significance of this implementation is that the configuration file can be flexibly used in different bllfactory implementation of the factory, like a factory and B factory they all produce bicycles, but a factory and B factory use of accessories and assembly method is not the same
And our business logic might be a factory using SQL Server database, while the B factory uses Oracle database, but can achieve the need for business data
8, calling method
BOOL IsOK = BLLFactory.Instance.UserDLL.Register (userdto);
C # Abstract Factory mode