Abstract Factory mode: provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes.
The biggest advantage of the abstract factory mode is that it is easy to switch the product series. Because of the specific factory classes, such as ifacw.factory = new AccessFactory (), you only need to appear at initialization once in an application, this makes it very easy to change the specific factory of an application. It only needs to change the specific factory to use different product configurations.Our design cannot prevent required changes, so our ideal is to minimize the changes. Now, if you want to change the database access, you only need to change the specific factory.
The second advantage is that it separates the process of creating an instance from the client. The client operates the instance through their abstract interface, and the specific class name of the product is also separated by the implementation of the specific factory, does not appear in customer code.
In fact, in the code in the second round, the client only knows IUser and IDepartment. I don't know whether it is implemented using SQL Server or Access.
However, what should we do if we add another Project Table Project?
First, you need to add three classes, IProject, SqlServerProject, and AccessProject. You also need to change ifacloud, and SqlServerFactory and AccessFactory.
Now we need to correct the above link code, and the previous simple factory model is required. Http://www.cnblogs.com/aehyok/archive/2013/05/10/3072008.html
db= === ===
In the past, an interface and two interfaces were implemented. Now we can use a DataAccess class instead.
The client call method has also been changed.
==
Select the database that is defined in DataAccess = Of course, it is better to change the configuration file.
If you add a project as described in the preface, you only need to add the same method to the DataAccess class.
But there is still a problem, as we mentioned earlier, for example, we need to use the Oracle database. It turns out that you only need to add the OracleFactory. Now it is quite troublesome.
If you do not know much about reflection, you can simply take a look at my previous entry of reflection blog http://www.cnblogs.com/aehyok/archive/2013/03/25/2963287.html
First, we need to reference using System. Reflection to reference Reflection to use Reflection to optimize the inherent limitations of the abstract factory model.
By the way, let's take a look at the simple use of reflection to obtain the instance method.
IUser result = SqlServerUser (); // This is a common method
Next let's take a look at the reflection method. Don't forget to reference it first.
IUser result = (IUser)Assembly.Load().CreateInstance();
Now we can find that we can use strings to instantiate objects with reflection, while variables can be replaced, and the conventional writing is written in the program.
Next let's take a look at the abstract factory after reflection optimization, which is actually the optimization of the preceding DataAccess class.
db = AssemblyName = ClassName = AssemblyName++db+= (IUser)Assembly.Load().CreateInstance( ClassName =+ +db+
In fact, we can simply optimize the configuration file.
<?xml version= key= value= key= value= sku=/></startup></configuration>
db = ConfigurationManager.AppSettings[ AssemblyName = ClassName = AssemblyName++db+= (IUser)Assembly.Load().CreateInstance( ClassName =+ +db+
In this case, you only need to configure the database in the configuration file.
Therefore, where a simple factory is used, reflection can be used to remove the coupling caused by switch or if and contact Branch judgment.
It turns out that it is good to learn so much from the abstract factory.
Final Version of the abstract factory mode instance code download link for http://url.cn/GgWA2K