【设计模式】轻巧的变化不同数据库操作
一,概述 抽象工厂:提供一个创建一些列相关或相互依赖的接口,而无需指定他们具体的类。 抽象工厂模式是所有形态的工厂模式中最为抽象和最具一般性的一种形态。 抽象工厂模式是指当有多个抽象角色时,使用的一种工厂模式。 抽象工厂模式可以向客户端提供一
一,概述
抽象工厂:提供一个创建一些列相关或相互依赖的接口,而无需指定他们具体的类。
抽象工厂模式是所有形态的工厂模式中最为抽象和最具一般性的一种形态。
抽象工厂模式是指当有多个抽象角色时,使用的一种工厂模式。
抽象工厂模式可以向客户端提供一个接口,使客户端在不必指定产品的具体的情况下,创建多个产品族中的产品对象。根据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; } }

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

MySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.

The relationship between SQL and MySQL is the relationship between standard languages and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!