抽象工厂――设计模式
一、了解 抽象 工厂 设计 模式 书上说:提供一个创建一系列相关或相互依赖对象的接口,而无需制定他们具体的类。 二、看类图 解说员:图上分两部分来看,一部分是左边的IFactory家族,另一部分是右边的IProduct 家族。 1、先看IProduct家族。 第一层:客户需
书上说:提供一个创建一系列相关或相互依赖对象的接口,而无需制定他们具体的类。
二、看类图
解说员:图上分两部分来看,一部分是左边的IFactory家族,另一部分是右边的IProduct 家族。
1、先看IProduct家族。
第一层:客户需求。可以看出我们需要哪些产品,由客户端决定,我们需要提前设计好第二层的产品。
第二层:ProductA和ProductB,这一系列的产品,具体要根据实际情况来确定。
第二层要说明的是:这个具体的产品的属性是不变的,而我们要对这个产品进行的各种操作是变化的,为了符合开放——封闭,和封装原则,在修改方法的时候不必影响到属性,我们把这个产品类分成了两部分,一部分是具体的名词类,不包含任何方法,只有该类的属性;另一部分是抽象接口:定义对该类的操作方法。这样我们就可以单独的对一个类的属性或者是方法进行分别操作,而互不影响,做到面向对象的封装。他们之间是依赖关系,接口依赖具体类。这个内部关系,在上图中没有变现出来。
也就是说,第三层实现的是第二层的接口方法,访问和操作的是第二层的具体类。
第三层:A1,A2,则是具体的实现类了。他们是ProductA 的具体实现过程。可能实现有多种,SO,具体的实现类也要多个,当然也需要一个统一的接口来统一管理。这里的接口就设在了第二层,有力第二层的接口,我们就可以在三层进行扩展,这也是一个封装。
体现的原则
开放——封闭原则:对扩展开放,对修改封闭。
在这个三层图上,有两个接口,也就是有两个地方可能发生变化,一个是数据库的选择,一个是访问数据库表的选择。
依赖倒转原则:抽象工厂的设计是由上自下逐步抽象的,且是针对接口编程,在最后一层才开始考虑具体的实现,提高了代码的复用,分别体现了:高层模块不依赖低层模块;抽象不依赖细节。
单一原则:第三层的每个类都是对单一功能的实现,对一个数据库表进行访问。
举例说明:用户需要访问数据库,那第二层就是数据库表,这些是哪个数据库都具备的;从第一层到二层扩展为了将来对更多或者其他数据库表的访问;现在我们要访问用户表,或许以后,我们还需要访问公司部门表,或者公司财务表;第三层应该考虑数据库的具体访问,操作实现,可扩展的子类有SQL server,Access 或者oracle。这些都是不同的实现过称,但同属于实现这一级别。可能现在用的是access,将来用SQL server,或者是Oracle,所以我们在要第二层为什么准备了接口,以备第三层的多种实现。
2、看IFactory家族。
此处工厂不是具体的制造产品的工厂,只是一个中介所。为什么这么说?
第一层:定义了一个创建一系列相关或相互依赖对象的接口。
例如我们需要访问的多个数据库表都在其中定义。这也是区别于工厂模式的一点。工厂模式针对的是一类,而抽象工厂针对的是一系列产品。
第二层:具体实现访问方法,他们根据客户端实例化的对象,帮助用户找到Iproduct 家族中第三层的地址,达到实现的目的。
练手:对SQL server和ACCESS两种数据库中的用户表和部门表进行访问。
[csharp]
//定义用户表类——定义了用户表的属性
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; }
}
}
//定义数据库用户表接口——定义了对用户表的进行的操作
interface IUser
{
void Insert(User user);
User GetUser(int id);
}
//定义SQL数据库表
class SqlserverUser:IUser
{
public void Insert(User user)
{
Console.WriteLine("在SQL 中增加一条用户记录");
}
public User GetUser(int id)
{
Console.WriteLine("在SQL 中根据ID得到一条用户记录");
return null;
}
}
//定义Access数据库表
class AccessUser : IUser
{
public void Insert(User user)
{
Console.WriteLine("在Access 中增加一条用户记录");
}
public User GetUser(int id)
{
Console.WriteLine("在Access 中根据ID得到一条用户记录");
return null;
}
}
//定义数据库部门表——属性
class Department
{
private int id;
public int ID
{
get { return id; }
set { id = value; }
}
private string dname;
public string DName
{
get { return dname; }
set { dname = value; }
}
}
//定义操作部门的接口——操作
interface IDepartmnet
{
void Insert(Department department);
Department GetDepartment(int id);
}
//定义用于访问部门的SQL 实现
class SQLdepartment:IDepartmnet
{
public void Insert(Department department)
{
Console.WriteLine("在SQL 中插入一条记录!");
}
public Department GetDepartment(int id)
{
Console.WriteLine("根据ID 号取出部门!");
return null;
}
}
//定义用于访问部门的Access 实现
class Accessdepartment : IDepartmnet
{
public void Insert(Department department)
{
Console.WriteLine("在SQL 中插入一条记录!");
}
public Department GetDepartment(int id)
{
Console.WriteLine("根据ID 号取出部门!");
return null;
}
}
//定义访问数据库表的工厂接口
interface IFactory
{
IUser CreateUser();
IDepartmnet CreatDepartment();
}
//实例化SQL 工厂接口
class SqlFactory : IFactory
{
public IUser CreateUser()
{
return new SqlserverUser();
}
public IDepartmnet CreatDepartment()
{
return new SQLdepartment();
}
}
//实例化Access工厂接口
class AccessFactory : IFactory
{
public IUser CreateUser()
{
return new AccessUser();
}
public IDepartmnet CreatDepartment()
{
return new Accessdepartment();
}
}
static void Main(string[] args)
{
User user = new User();
Department department = new Department();
IFactory factory = new SqlFactory();
IUser iu = factory.CreateUser();
iu.Insert(user);
iu.GetUser(2);//任意的int类型的都可以
IFactory factory1 = new AccessFactory();
IUser au = factory1.CreateUser();
au.Insert(user);
au.GetUser(2);//任意的int类型的都可以
IFactory factoryd = new SqlFactory();
IDepartmnet id=factoryd .CreatDepartment ();
id.Insert (department );
id.GetDepartment (1);
IFactory factorya = new AccessFactory();
IDepartmnet ad=factoryd .CreatDepartment ();
ad.Insert (department );
ad.GetDepartment (1);
www.2cto.com
//IUser au = new AccessUser();
//au.Insert(user);
//au.GetUser(2);//任意的int类型的都可以
Console.Read();
}

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



According to news on July 12, the Honor Magic V3 series was officially released today, equipped with the new Honor Vision Soothing Oasis eye protection screen. While the screen itself has high specifications and high quality, it also pioneered the introduction of AI active eye protection technology. It is reported that the traditional way to alleviate myopia is "myopia glasses". The power of myopia glasses is evenly distributed to ensure that the central area of sight is imaged on the retina, but the peripheral area is imaged behind the retina. The retina senses that the image is behind, promoting the eye axis direction. grow later, thereby deepening the degree. At present, one of the main ways to alleviate the development of myopia is the "defocus lens". The central area has a normal power, and the peripheral area is adjusted through optical design partitions, so that the image in the peripheral area falls in front of the retina.

According to news on May 13, vivoX100s was officially released tonight. In addition to excellent images, the new phone also performs very well in terms of signal. According to vivo’s official introduction, vivoX100s uses an innovative universal signal amplification system, which is equipped with up to 21 antennas. This design has been re-optimized based on the direct screen to balance many signal requirements such as 5G, 4G, Wi-Fi, GPS, and NFC. This makes vivoX100s the mobile phone with the strongest signal reception capability in vivo’s history. The new phone also uses a unique 360° surround design, with antennas distributed around the body. This design not only enhances the signal strength, but also optimizes various daily holding postures to avoid problems caused by improper holding methods.

According to news on July 29, the Honor X60i mobile phone is officially on sale today, starting at 1,399 yuan. In terms of design, the Honor X60i mobile phone adopts a straight screen design with a hole in the center and almost unbounded ultra-narrow borders on all four sides, which greatly broadens the field of view. Honor X60i parameters Display: 6.7-inch high-definition display Battery: 5000mAh large-capacity battery Processor: Dimensity 6080 processor (TSMC 6nm, 2x2.4G A76+6×2G A55) System: MagicOS8.0 system Other features: 5G signal enhancement, smart capsule, under-screen fingerprint, dual MIC, noise reduction, knowledge Q&A, photography capabilities: rear dual camera system: 50 million pixels main camera, 2 million pixels auxiliary lens, front selfie lens: 8 million pixels, price: 8GB

According to news on July 19, Xiaomi MIX Fold 4, the first flagship folding new phone, was officially released tonight and is equipped with a "three-dimensional special-shaped battery" for the first time. According to reports, Xiaomi MIX Fold4 has achieved a major breakthrough in battery technology and designed an innovative "three-dimensional special-shaped battery" specifically for folding screens. Traditional folding screen devices mostly use conventional square batteries, which have low space utilization efficiency. In order to solve this problem, Xiaomi did not use the common winding battery cells, but developed a new lamination process to create a new form of battery, which greatly improved the space utilization. Battery Technology Innovation In order to accurately alternately stack positive and negative electrode sheets and ensure the safe embedding of lithium ions, Xiaomi has developed a new ultrasonic welding machine and lamination machine to improve welding and cutting accuracy.

Xiaomi's Redmi brand is gearing up to add another budget phone to its portfolio - the Redmi 14C. The device is confirmed to be released in Vietnam on August 31st. However, ahead of the launch, the phone's specifications have been revealed via a Vietnamese retailer. Redmi14CR Redmi often brings new designs in new series, and Redmi14C is no exception. The phone has a large circular camera module on the back, which is completely different from the design of its predecessor. The blue color version even uses a gradient design to make it look more high-end. However, Redmi14C is actually an economical mobile phone. The camera module consists of four rings; one houses the main 50-megapixel sensor, and another may house the camera for depth information.

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.

According to news on July 12, Honor Magic V3 was officially released today, bringing the thickness of folding screen mobile phones to 9.2 mm. It is particularly worth mentioning that while pursuing the ultimate in thinness and lightness, Honor MagicV3 also achieves industry-leading waterproof performance through the use of cutting-edge technology. Thanks to its 10-micron precision filling technology, this phone not only reaches the IPX8 waterproof standard, but also maintains touch sensitivity even in humid environments, providing users with a worry-free experience. At the press conference, Honor conducted a bold experiment and directly placed the MagicV3 in a drum washing machine for a 15-minute quick wash test. The results were amazing - not only was the phone safe, but it also demonstrated its excellent waterproof capabilities. glory

According to news on June 13, Motorola officially announced today that Lenovo Motorazr 2024 is scheduled to be released at 14:00 on June 25. It is expected to be the Motorazr50/Ultra series of folding screen mobile phones that have previously entered the Internet. In terms of specifications, the performance of MotorazR50 is impressive. It uses a 6.9-inch 2640x1080 OLED internal screen with a refresh rate of up to 165Hz, providing users with a smooth visual experience. At the same time, it is also equipped with a 3.63-inch 1066×1056 OLED external screen with a refresh rate of 144Hz, making it more convenient for users in daily use. Both the internal and external screens support 1.07 billion color display, with rich and delicate color performance. In terms of hardware configuration, m
