Design Mode: lightweight change of different database operations

Source: Internet
Author: User
I. Overview Abstract Factory: provides an interface for creating column-related or interdependent operations without specifying their specific classes. Abstract Factory mode is the most abstract and general factory mode in all forms. Abstract Factory mode refers to a factory mode used when multiple abstract roles exist. Abstract Factory mode can provide client with 1

I. Overview Abstract Factory: provides an interface for creating column-related or interdependent operations without specifying their specific classes. Abstract Factory mode is the most abstract and general factory mode in all forms. Abstract Factory mode refers to a factory mode used when multiple abstract roles exist. Abstract Factory mode can provide client with 1

I. Overview

Abstract Factory: provides an interface for creating column-related or interdependent operations without specifying their specific classes.

Abstract Factory mode is the most abstract and general factory mode in all forms.

Abstract Factory mode refers to a factory mode used when multiple abstract roles exist.

Abstract Factory mode provides an interface to the client to create product objects in multiple product families without specifying the specific product. According to the LSP principle, any place that accepts the parent type should be able to accept the child type. Therefore, what the system actually needs is only some instances with the same type as those of abstract products, rather than the instances of these abstract products. In other words, it is an example of the concrete sub-classes of these abstract products. The factory class is responsible for creating instances of concrete subclasses of abstract products.

2. Example

The concept is too abstract and hard to understand. Let's take a look at the example and understand the usage and benefits of abstract factories and abstract factories step by step.

Question: How can I change a previously written website to an Access database or an Oracle database using SQL Server?

1) The most basic database access program

Disadvantage: If you want to change SQL Server database operations to Access database operations, you need to override the operation class.

If there are many tables, each table operation needs to change the matching Access database operation.

Class Program {static void Main (string [] args) {User user = new User (); SqlserverUser su = new SqlserverUser (); su. insert (user); su. getUser (1); Console. read () ;}} class User // User table (User id, User name) {private int _ ID; public int id {get {return _ id ;} set {_ id = value ;}} private string _ name; public string Name {get {return _ name ;}set {_ name = value ;}}} class SqlserverUser // operation Database User table class {public void Insert (User user) {Console. writeLine ("adding a record to the User table in Sqlserver");} public User GetUser (int id) {Console. writeLine ("getting a User table record by ID in Sqlserver"); return null ;}}


2) data access programs using the factory method mode

Defines an interface used to create objects, so that the subclass determines which class to instantiate.

Disadvantage: You still need to specify AccessFactory ()

Only one user table can cope with this problem. If you add another department table, it will be hard to cope with this problem.

Class Program {static void Main (string [] args) {User user = new User (); // The table class to be processed // AbstractFactory factory = new SqlServerFactory (); IFactory factory = new AccessFactory (); // abstract factory to generate a specific class IUser iu = factory. createUser (); iu. insert (user); iu. getUser (1); Console. read () ;}} class User // user table class {private int _ id; public int ID {get {return _ id ;}set {_ id = value ;}} private string _ name; public string Name {get {return _ name;} set {_ name = value ;}}} interface IUser // User table interface {void Insert (user User); user GetUser (int id);} class SqlserverUser: IUser // sqlServer User table {public void Insert (user User user) {Console. writeLine ("adding a record to the User table in Sqlserver");} public User GetUser (int id) {Console. writeLine ("getting a User table record by ID in Sqlserver"); return null ;}} class AccessUser: IUser // Access operation User table {public void Insert (user User) {Console. writeLine ("adding a record to the User table in Access");} public User GetUser (int id) {Console. writeLine ("Get a User table record by ID in Access"); return null ;}} interface ifacloud // factory interface {IUser CreateUser ();} class SqlServerFactory: ifacloud // sqlServer object factory {public IUser CreateUser () {return new SqlserverUser () ;}} class AccessFactory: ifacloud // access Object factory {public IUser CreateUser () {return new AccessUser ();}}

3) Abstract Factory Model

A department table is added. You can use SQLServer factory and Access factory to generate objects for the Operation Department table and user table.

Class Program {static void Main (string [] args) {User user = new User (); Department dept = new Department (); // define actfactory factory = new SqlServerFactory (); IFactory factory = new AccessFactory (); IUser iu = factory. createUser (); iu. insert (user); iu. getUser (1); IDepartment id = factory. createDepartment (); id. insert (dept); id. getDepartment (1); Console. read () ;}} class User // User table {private int _ id; public int ID {get {return _ id;} set {_ id = value ;}} private string _ name; public string Name {get {return _ name;} set {_ name = value ;}} class Department // Department table {private int _ id; public int ID {get {return _ id;} set {_ id = value;} private string _ deptName; public string DeptName {get {return _ deptName ;} set {_ deptName = value ;}} interface IUser // User table operation interface {void Insert (user User); user GetUser (int id);} class SqlserverUser: IUser // specific sqlServer User table {public void Insert (user User user) {Console. writeLine ("adding a record to the User table in Sqlserver");} public User GetUser (int id) {Console. writeLine ("getting a User table record by ID in Sqlserver"); return null ;}} class AccessUser: IUser // specific Access User table {public void Insert (user User user) {Console. writeLine ("adding a record to the User table in Access");} public User GetUser (int id) {Console. writeLine ("Get a User table record by ID in Access"); return null ;}} interface IDepartment // Department Table interface {void Insert (department Department department ); department GetDepartment (int id);} class SqlserverDepartment: IDepartment // table of the specific sqlServer Operation Department {public void Insert (department Department) {Console. writeLine ("adding a record to the Department table in Sqlserver");} public Department GetDepartment (int id) {Console. writeLine ("getting a Department table record by ID in Sqlserver"); return null ;}} class AccessDepartment: IDepartment // specific Access operation Department table {public void Insert (department Department) {Console. writeLine ("adding a record to the Department table in Access");} public Department GetDepartment (int id) {Console. writeLine ("obtain a Department table record by ID in Access"); return null ;}} interface IFactory // factory interface {IUser CreateUser (); IDepartment CreateDepartment ();} class SqlServerFactory: ifacloud // return different table objects of the sqlServer operation {public IUser CreateUser () {return new SqlserverUser ();} public IDepartment CreateDepartment () {return new SqlserverDepartment () ;}} class AccessFactory: ifacloud // different table objects returned for the Access operation {public IUser CreateUser () {return new AccessUser ();} public IDepartment CreateDepartment () {return new AccessDepartment ();}}


Iii. Advantages and disadvantages of the abstract factory Model

1) Advantages:

Easy to switch product series, such as changing from SQL server operation series to Access operation series. You only need to change the SQLServer factory to: IFactory factory = new AccessFactory ();

Separate the instance creation process from the client. The client operates the instance through their abstract interfaces. The product class name is also separated by the implementation of the specific factory and will not appear in the code.

2) Disadvantages:

If you want to add a project, you need to add the IProject, SqlServerProject, and AccessProject classes. You also need to change ifacloud, SqlserverFactory, and AccessFactory to implement this. That is to say, you need to add three classes, change three classes.

4. Use a simple factory to improve the Abstract Factory

Add a DataAccess and CreateDepartment to replace the abstract factory mode.

Disadvantage: If you need to add an Oracle operation, you can add an OracleFactory factory class. Now you need to change the two classes, in violation of the open and closed principle.

Class Program {static void Main (string [] args) {User user = new User (); Department dept = new Department (); IUser iu = DataAccess. createUser (); iu. insert (user); iu. getUser (1); IDepartment id = DataAccess. createDepartment (); id. insert (dept); id. getDepartment (1); Console. read () ;}} class User // User table {private int _ id; public int ID {get {return _ id;} set {_ id = value ;}} private string _ name; public string Name {get {return _ name;} set {_ name = value ;}} class Department // Department table {private int _ id; public int ID {get {return _ id;} set {_ id = value;} private string _ deptName; public string DeptName {get {return _ deptName ;} set {_ deptName = value ;}} interface IUser // Department table operation interface {void Insert (User user User); User GetUser (int id);} class SqlserverUser: IUser // SqlServer User table {public void Insert (user User user) {Console. writeLine ("adding a record to the User table in Sqlserver");} public User GetUser (int id) {Console. writeLine ("getting a User table record by ID in Sqlserver"); return null ;}} class AccessUser: IUser // Access operation User table {public void Insert (user User) {Console. writeLine ("adding a record to the User table in Access");} public User GetUser (int id) {Console. writeLine ("Get a User table record by ID in Access"); return null ;}} interface IDepartment // Department table {void Insert (department Department department ); department GetDepartment (int id);} class SqlserverDepartment: IDepartment // SqlServer {public void Insert (Department department Department) {Console. writeLine ("adding a record to the Department table in Sqlserver");} public Department GetDepartment (int id) {Console. writeLine ("obtain a Department table record by ID in Sqlserver"); return null ;}} class AccessDepartment: IDepartment // SAccess Operation Department table {public void Insert (department Department) {Console. writeLine ("adding a record to the Department table in Access");} public Department GetDepartment (int id) {Console. writeLine ("obtain a Department table record by ID in Access"); return null ;}} class DataAccess // simple factory operation employee table {private static readonly string db = "Sqlserver"; // private static readonly string db = "Access"; public static IUser CreateUser () {IUser result = null; switch (db) {case "Sqlserver": result = new SqlserverUser (); break; case "Access": result = new AccessUser (); break ;} return result;} public static IDepartment CreateDepartment () // simple factory operation department table {IDepartment result = null; switch (db) {case "Sqlserver": result = new SqlserverDepartment (); break; case "Access": result = new AccessDepartment (); break;} return result ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.