【设计模式】轻巧的变化不同数据库操作
一,概述 抽象工厂:提供一个创建一些列相关或相互依赖的接口,而无需指定他们具体的类。 抽象工厂模式是所有形态的工厂模式中最为抽象和最具一般性的一种形态。 抽象工厂模式是指当有多个抽象角色时,使用的一种工厂模式。 抽象工厂模式可以向客户端提供一
一,概述
抽象工厂:提供一个创建一些列相关或相互依赖的接口,而无需指定他们具体的类。
抽象工厂模式是所有形态的工厂模式中最为抽象和最具一般性的一种形态。
抽象工厂模式是指当有多个抽象角色时,使用的一种工厂模式。
抽象工厂模式可以向客户端提供一个接口,使客户端在不必指定产品的具体的情况下,创建多个产品族中的产品对象。根据LSP原则,任何接受父类型的地方,都应当能够接受子类型。因此,实际上系统所需要的,仅仅是类型与这些抽象产品角色相同的一些实例,而不是这些抽象产品的实例。换言之,也就是这些抽象产品的具体子类的实例。工厂类负责创建抽象产品的具体子类的实例。
二,示例
概念太抽象了,理解起来实在费劲。看例子,一步一步理解什么是抽象工厂以及抽象工厂的用法及好处。
题目:以前写的网站,用SQL Server 但是后来要更改成Access的数据库或者是Oracle的数据库,改怎么做?
1)最基本的数据库访问程序
缺点:如果想将SQL Server数据库操作更改为Access的数据库操作,则需要重写操作类。
如果表多的话,每一个表的操作都需要更改匹配的 Access数据库操作
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 //用户表(用户ID,用户名) { 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 //操作数据库中User表的类 { public void Insert(User user) { Console.WriteLine("在Sqlserver中给User表增加一条记录"); } public User GetUser(int id) { Console.WriteLine("在Sqlserver中根据ID得到User表一条记录"); return null; } }
2)使用工厂方法模式的数据访问程序
定义一个用于创建对象的接口,让子类决定实例化哪一类。
缺点:还是需要指定 AccessFactory()
仅仅有一个用户表时候可以应付,如果再添加一个部门表就难以应付
class Program { static void Main(string[] args) { User user = new User(); //要处理的表类 //AbstractFactory factory = new SqlServerFactory(); IFactory factory = new AccessFactory();//抽象工厂,生成具体的类 IUser iu = factory.CreateUser(); iu.Insert(user); iu.GetUser(1); Console.Read(); } } class User //user表类 { 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 //用户表接口 { void Insert(User user); User GetUser(int id); } class SqlserverUser : IUser //sqlServer操作用户表 { public void Insert(User user) { Console.WriteLine("在Sqlserver中给User表增加一条记录"); } public User GetUser(int id) { Console.WriteLine("在Sqlserver中根据ID得到User表一条记录"); return null; } } class AccessUser : IUser //Access操作用户表 { public void Insert(User user) { Console.WriteLine("在Access中给User表增加一条记录"); } public User GetUser(int id) { Console.WriteLine("在Access中根据ID得到User表一条记录"); return null; } } interface IFactory //工厂接口 { IUser CreateUser(); } class SqlServerFactory : IFactory //sqlServer对象工厂 { public IUser CreateUser() { return new SqlserverUser(); } } class AccessFactory : IFactory //access对象工厂 { public IUser CreateUser() { return new AccessUser(); } }
3)抽象工厂模式
增加了部门表,通过SQLServer 工厂和 Access 工厂可以生成 操作部门表和用户表的对象
class Program { static void Main(string[] args) { User user = new User(); Department dept = new Department(); //AbstractFactory 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 //用户表 { 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 //部门表 { 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 //用户表操作接口 { void Insert(User user); User GetUser(int id); } class SqlserverUser : IUser //具体的sqlServer操作用户表 { public void Insert(User user) { Console.WriteLine("在Sqlserver中给User表增加一条记录"); } public User GetUser(int id) { Console.WriteLine("在Sqlserver中根据ID得到User表一条记录"); return null; } } class AccessUser : IUser //具体的Access操作用户表 { public void Insert(User user) { Console.WriteLine("在Access中给User表增加一条记录"); } public User GetUser(int id) { Console.WriteLine("在Access中根据ID得到User表一条记录"); return null; } } interface IDepartment //部门表接口 { void Insert(Department department); Department GetDepartment(int id); } class SqlserverDepartment : IDepartment // 具体sqlServer操作部门表 { public void Insert(Department department) { Console.WriteLine("在Sqlserver中给Department表增加一条记录"); } public Department GetDepartment(int id) { Console.WriteLine("在Sqlserver中根据ID得到Department表一条记录"); return null; } } class AccessDepartment : IDepartment // 具体Access操作部门表 { public void Insert(Department department) { Console.WriteLine("在Access中给Department表增加一条记录"); } public Department GetDepartment(int id) { Console.WriteLine("在Access中根据ID得到Department表一条记录"); return null; } } interface IFactory //工厂接口 { IUser CreateUser(); IDepartment CreateDepartment(); } class SqlServerFactory : IFactory //返回sqlServer 操作的不同表对象 { public IUser CreateUser() { return new SqlserverUser(); } public IDepartment CreateDepartment() { return new SqlserverDepartment(); } } class AccessFactory : IFactory //返回Access 操作的不同表对象 { public IUser CreateUser() { return new AccessUser(); } public IDepartment CreateDepartment() { return new AccessDepartment(); } }
三,抽象工厂模式的优点和缺点
1)优点:
易于交换产品系列,比如从SQLServer 操作系列改变为Access操作系列。仅仅需要将SQLServer工厂改为: IFactory factory = new AccessFactory();
让具体的创建实例过程与客户端分离,客户端是通过他们的抽象接口操作实例,产品的类名也被具体工厂的实现分离,不会出现在代码中
2)缺点:
如果需要增加一个项目表(project),则需要增加 IProject 、SqlServerProject、AccessProject类,而且还需要更改IFactory、SqlserverFactory、AccessFactory才可以实现,也就是说增加三个类,改变三个类。
四,用简单工厂改进抽象工厂
增加一个DataAccess,和CreateDepartment 代替抽象工厂模式
缺点:如果需要增加一个Oracle操作,本来增加一个OracleFactory工厂类就可以,现在需要更改以上两个类,违反了开放封闭原则。
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 //用户表 { 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 //部门表 { 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 //部门表操作接口 { void Insert(User user); User GetUser(int id); } class SqlserverUser : IUser //SqlServer操作用户表 { public void Insert(User user) { Console.WriteLine("在Sqlserver中给User表增加一条记录"); } public User GetUser(int id) { Console.WriteLine("在Sqlserver中根据ID得到User表一条记录"); return null; } } class AccessUser : IUser //Access操作用户表 { public void Insert(User user) { Console.WriteLine("在Access中给User表增加一条记录"); } public User GetUser(int id) { Console.WriteLine("在Access中根据ID得到User表一条记录"); return null; } } interface IDepartment // 部门表 { void Insert(Department department); Department GetDepartment(int id); } class SqlserverDepartment : IDepartment//SqlServer操作部门表 { public void Insert(Department department) { Console.WriteLine("在Sqlserver中给Department表增加一条记录"); } public Department GetDepartment(int id) { Console.WriteLine("在Sqlserver中根据ID得到Department表一条记录"); return null; } } class AccessDepartment : IDepartment//SAccess操作部门表 { public void Insert(Department department) { Console.WriteLine("在Access中给Department表增加一条记录"); } public Department GetDepartment(int id) { Console.WriteLine("在Access中根据ID得到Department表一条记录"); return null; } } class DataAccess //简单工厂操作员工表 { 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()//简单工厂操作 部门表 { IDepartment result = null; switch (db) { case "Sqlserver": result = new SqlserverDepartment(); break; case "Access": result = new AccessDepartment(); break; } return result; } }

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Table of Contents Astar Dapp Staking Principle Staking Revenue Dismantling of Potential Airdrop Projects: AlgemNeurolancheHealthreeAstar Degens DAOVeryLongSwap Staking Strategy & Operation "AstarDapp Staking" has been upgraded to the V3 version at the beginning of this year, and many adjustments have been made to the staking revenue rules. At present, the first staking cycle has ended, and the "voting" sub-cycle of the second staking cycle has just begun. To obtain the "extra reward" benefits, you need to grasp this critical stage (expected to last until June 26, with less than 5 days remaining). I will break down the Astar staking income in detail,

In the Java framework, the difference between design patterns and architectural patterns is that design patterns define abstract solutions to common problems in software design, focusing on the interaction between classes and objects, such as factory patterns. Architectural patterns define the relationship between system structures and modules, focusing on the organization and interaction of system components, such as layered architecture.

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

To avoid PHP database connection errors, follow best practices: check for connection errors and match variable names with credentials. Use secure storage or environment variables to avoid hardcoding credentials. Close the connection after use to prevent SQL injection and use prepared statements or bound parameters.
