当前位置:网站首页>Abstract factory pattern
Abstract factory pattern
2022-07-27 07:47:00 【Visit the world】
Definition
Abstract factory pattern (Abstract FactoryPattern) Create other factories around one super factory . This super factory is also called the factory of other factories .
It is suggested to understand Simple factory model Understanding abstract factory patterns .
type
Create pattern
Example
We now have two ways to save data , Database and KeyValue localization .
But databases and KeyValue There are several kinds. , Different situations may require different libraries , In order to facilitate the extension, we define an interface unified capability .
Just like the simple factory model , For the time being, there is only one way to add data .
public interface IKeyValueData {
void add();
}
Define its implementation class , And implement IKeyValueData Interface
SharedPreferences
public class SharedPreferences implements IKeyValueData {
@Override
public void add() {
}
}
DataStore
public class DataStore implements IKeyValueData{
@Override
public void add() {
}
}
MMKV
public class MMKV implements IKeyValueData{
@Override
public void add() {
}
}
Here is actually a simple factory model , Just one factory is needed to establish . here , Don't rush to create a factory , Let's create another type first .
Define the interface of the database
public interface IDataBase {
void insert();
}
Implementation class
MySql database
public class MySql implements IDataBase{
@Override
public void insert() {
}
}
Oracle database
public class Oracle implements IDataBase{
@Override
public void insert() {
}
}
MongoDB database
public class MongoDB implements IDataBase{
@Override
public void insert() {
}
}
Now it's time to create a factory , Simple factory mode because there is only one product family ( Database and KeyValueData) So you can directly create a factory class . however , Abstract factory itself is the factory of many factories , Therefore, an interface is needed to define the product family of the factory .
Defining interfaces
public interface IDataFactory {
IKeyValueData getKeyValueData();
IDataBase getDataBase();
}
From this interface, it can be determined that there are two product families .
Create a factory
Database factory
public class DataBaseFactory implements IDataFactory {
private boolean isMysql;
private boolean isMongoDB;
@Override
public IKeyValueData getKeyValueData() {
return null;
}
@Override
public IDataBase getDataBase() {
if (isMysql) {
return new MySql();
}
if (isMongoDB) {
return new MongoDB();
}
return new Oracle();
}
}
KeyValue factory
public class KeyValueIDataFactory implements IDataFactory {
private boolean isMMKV;
private boolean isDataStore;
@Override
public IKeyValueData getKeyValueData() {
if (isMMKV) {
return new MMKV();
}
if (isDataStore) {
return new DataStore();
}
return new SharedPreferences();
}
@Override
public IDataBase getDataBase() {
return null;
}
}
Usage mode
public class Test {
public static void main(String[] args) {
IDataBase dataBase = new DataBaseFactory().getDataBase();
IKeyValueData keyValueData = new KeyValueIDataFactory().getKeyValueData();
dataBase.insert();
keyValueData.add();
}
}
Class diagram
The abstract factory will be realized , Look at the diagram .
Advantages and disadvantages
advantage
- Product grade is easy to expand
- Conform to the principle of dependency abstraction
- Simplify callers , There is no need to pay attention to how the factory is created
shortcoming
- Product families are hard to expand
- Increases the abstractness of the system and the difficulty of understanding ;
Example
If one day I want to add a new product family , For example, I want to add IO To serialize local data directly , I need to change IDataFactory Interface .
public interface IDataFactory {
IKeyValueData getKeyValueData();
IDataBase getDataBase();
IDataBase getIOData();
}
This led me to achieve all IDataFactory All classes of the interface should be rewritten getIOData Method .

Then you can realize yourself IO.
But it's very convenient if we only add one product grade
newly added SQLite database
public class SQLite implements IDataBase{
@Override
public void insert() {
}
}
Then modify the creation logic of the database factory .
difficulty
Understanding of product family and product grade . In this example, product families are databases and KeyValue.MySql Product grade .
summary
Abstract factories may not be used in many places , Need to make trade-offs and changes according to needs .
边栏推荐
- 存储过程与函数
- 防止Cookie修改id欺骗登录
- shell循环练习
- 小程序消息推送配置 Token校验失败,请检查确认
- 「翻译」SAP变式物料的采购如何玩转?看看这篇你就明白了
- C language programming | program compilation and preprocessing
- C language implementation of guessing numbers Games project practice (based on srand function, rand function, switch statement, while loop, if condition criterion, etc.)
- What other methods are available for MySQL index analysis besides explain
- 【万字长文】吃透负载均衡,和阿里大牛的技术面谈
- linux中mysql表名区不区分大小写
猜你喜欢

【小程序】如何获取微信小程序代码上传密钥?

DEMO SUBMIT 某程序并获取该程序ALV数据

Comprehensive analysis of ADC noise-02-adc noise measurement method and related parameters

What about idea Chinese garbled code

C#winform 窗体事件和委托结合用法

Plato farm is expected to further expand its ecosystem through elephant swap

Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community

Grayog log server single node deployment

如何在电脑端登陆多个微信

C#委托的使用案例
随机推荐
【小程序】uniapp发行微信小程序上传失败Error: Error: {'errCode':-10008,'errMsg':'invalid ip...
How to get DDL information of an object
安装tensorflow
Flink de duplication (I) summary of common de duplication schemes in Flink and Flink SQL
C common function integration-2
IDEA中文乱码怎么办
实用的新药研发项目管理平台
Install tensorflow
C winfrom common function integration-2
一体化实时HTAP数据库StoneDB,如何替换MySQL并实现近百倍分析性能的提升
Synchronized lock
STM32_ Find the cause of entering hardfault_ Handler's function
鲁迅:我不记得说没说过,要不你自己查!
我是不是被代码给耽误了……不幸沦为一名程序员……
flink中维表Join几种常见方式总结
C#winform 窗体事件和委托结合用法
DEMO SUBMIT 某程序并获取该程序ALV数据
An open source OA office automation system
The first open source MySQL native HTAP database in China will be released soon! Look at the three highlights first, limited to the surrounding areas, waiting for you~
MySQL table name area in Linux is not case sensitive