首页 > 其他分享 >11-抽象工厂模式

11-抽象工厂模式

时间:2023-01-28 23:22:42浏览次数:37  
标签:11 String System 工厂 抽象 abstractFactory println com public

11-抽象工厂模式

概念

抽象工厂模式,提供一个创建一系列相关或相互依赖对象的接口,而无需指定他们具体的类

举例

项目中需要将数据库从SqlServer换成Access,User表和Department表的插入和获取都需要重写

package com.niao.abstractFactory;

public interface IUser {
    void insert(User user);

    User get(String id);
}

package com.niao.abstractFactory;

public class SqlServerUser implements IUser{
    @Override
    public void insert(User user) {
        System.out.println("在sqlServer中插入user数据");
    }

    @Override
    public User get(String id) {
        System.out.println("在sqlServer中获取user数据");
        return null;
    }
}
package com.niao.abstractFactory;

public class AccessUser implements IUser{
    @Override
    public void insert(User user) {
        System.out.println("在Access中插入user数据");
    }

    @Override
    public User get(String id) {
        System.out.println("在Access中获取user数据");
        return null;
    }
}
package com.niao.abstractFactory;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
    private String id;
    private String name;
    private String password;
}
package com.niao.abstractFactory;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Department {
    private String id;
    private String name;
    private int peopleNum;
}
package com.niao.abstractFactory;

public interface IDepartment {
    void insert(Department department);

    Department get(String id);
}
package com.niao.abstractFactory;

public class AccessDepartment implements IDepartment{
    @Override
    public void insert(Department department) {
        System.out.println("在Access中插入department数据");
    }

    @Override
    public Department get(String id) {
        System.out.println("在Access中获取department数据");
        return null;
    }
}
package com.niao.abstractFactory;

public class SqlServerDepartment implements IDepartment{
    @Override
    public void insert(Department department) {
        System.out.println("在SqlServer中插入department数据");
    }

    @Override
    public Department get(String id) {
        System.out.println("在SqlServer中获取department数据");
        return null;
    }
}
package com.niao.abstractFactory;

public interface IFactory {
    IUser createUser();

    IDepartment createDepartment();
}
package com.niao.abstractFactory;

public class AccessFactory implements IFactory{
    @Override
    public IUser createUser() {
        return new AccessUser();
    }

    @Override
    public IDepartment createDepartment() {
        return new AccessDepartment();
    }
}
package com.niao.abstractFactory;

public class SqlServerFactory implements IFactory{
    @Override
    public IUser createUser() {
        return new SqlServerUser();
    }

    @Override
    public IDepartment createDepartment() {
        return new SqlServerDepartment();
    }
}
package com.niao.abstractFactory;

import com.sun.org.slf4j.internal.Logger;
import com.sun.org.slf4j.internal.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertyUtil {
    private static Properties props;
    static{
        loadProps();
    }

    synchronized static private void loadProps(){
        System.out.println("开始加载properties文件内容.......");
        props = new Properties();
        InputStream in = null;
        try {
//       <!--第一种,通过类加载器进行获取properties文件流-->
                    in = PropertyUtil.class.getClassLoader().getResourceAsStream("application.properties");
//       <!--第二种,通过类进行获取properties文件流-->
                    //in = PropertyUtil.class.getResourceAsStream("/jdbc.properties");
                    props.load(in);
        } catch (FileNotFoundException e) {
            System.out.println("jdbc.properties文件未找到");
        } catch (IOException e) {
            System.out.println("出现IOException");
        } finally {
            try {
                if(null != in) {
                    in.close();
                }
            } catch (IOException e) {
                System.out.println("jdbc.properties文件流关闭出现异常");
            }
        }
        System.out.println("加载properties文件内容完成...........");
        System.out.println("properties文件内容:" + props);
    }

    public static String getProperty(String key){
        if(null == props) {
            loadProps();
        }
        return props.getProperty(key);
    }

    public static String getProperty(String key, String defaultValue) {
        if(null == props) {
            loadProps();
        }
        return props.getProperty(key, defaultValue);
    }
}
package com.niao.abstractFactory;

public class Client {
    public static void main(String[] args) {
        // 抽象工厂模式实现
//        IFactory factory = new SqlServerFactory();
//        IFactory factory = new AccessFactory();
//        IDepartment department = factory.createDepartment();

        // 简单工厂模式实现
        IDepartment department = DataAccess.createDepartment();


        Department department1 = new Department("1", "教育部", 50);
        department.insert(department1);

    }
}
package com.niao.abstractFactory;


public class DataAccess {

//    private static final String DB = "SqlServer";
    private static final String DB = PropertyUtil.getProperty("database.type");

    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;
    }
}
database.type=Access

待实现

使用反射代替switch还未实现,遗留

标签:11,String,System,工厂,抽象,abstractFactory,println,com,public
From: https://www.cnblogs.com/Oh-mydream/p/17071510.html

相关文章

  • Hugging News #0113:DreamBooth 编程马拉松活动保姆级视频教程来了!
    每一周,我们的同事都会向社区的成员们发布一些关于HuggingFace相关的更新,包括我们的产品和平台更新、社区活动、学习资源和内容更新、开源库和模型更新等,我们将其称之为......
  • 《RPC实战与核心原理》学习笔记Day11
    13|优雅关闭:如何避免服务停机带来的业务损失?我们在RPC架构下,需要考虑当服务重启时,如何做到让调用方系统不出问题。当服务提供方要上线时,一般是通过部署系统完成实例重......
  • 接口与抽象类
    什么是抽象类?什么是接口?抽象类抽象类不允许实例化,只能被继承。抽象类可以包含属性和方法,方法可以包含代码实现子类继承抽象类时需要实现抽象类中的所有方法注:抽象类......
  • 112. 路经总和
    问题描述https://leetcode.cn/problems/path-sum/description/解题思路我们可以对叶子结点进行判断,如果叶子结点的值等于targetSum,那么就算是找到了。代码#Definiti......
  • leetcode_数据结构_入门_118. 杨辉三角
    118.杨辉三角给定一个非负整数numRows,生成「杨辉三角」的前numRows行。在「杨辉三角」中,每个数是它左上方和右上方的数的和。方法一:数学思路及解法......
  • 111. 二叉树的最小深度
    问题描述https://leetcode.cn/problems/minimum-depth-of-binary-tree/description/解题思路这个题目不难,但对退出条件要求高。经过对题意的分析,我们对于root为None的......
  • 110. 平衡二叉树
    问题描述https://leetcode.cn/problems/balanced-binary-tree/description/解题思路这题一开始朴素的思路就是,对于每个节点,都计算其是不是平衡二叉树。计算平衡二叉树......
  • 上古神兵,先天至宝,Win11平台安装和配置NeoVim0.8.2编辑器搭建Python3开发环境(2023最
    毫无疑问,我们生活在编辑器的最好年代,Vim是仅在Vi之下的神级编辑器,而脱胎于Vim的NeoVim则是这个时代最好的编辑器,没有之一。异步支持、更好的内存管理、更快的渲染速度、更......
  • Win11没有关机键怎么办?
    1、首先,按键盘上的Win+R组合键,打开运行;2、输入 gpedit.msc命令,按确定或回车,打开本地组策略编辑器;3、本地组策略编辑器窗口,左侧点击展开:用户配置-管理模板-“开......
  • 解决pl2303在 windows 10 或windows 11 显示“PL2303HXA自2012已停产,请联系供货商”问
    解决pl2303USB转串口,在windows10或windows11显示“PL2303HXA自2012已停产,请联系供货商”问题,用如下驱动试试:Win10驱动下载:https://pan.baidu.com/s/1FgV-VPErQMJsV......