抽象工厂模式(Abstract Factory)

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

主方法

public class main {
    public static void main(String[] args) {
        User user = new User();
        Department dept = new Department();
       
 //        IFactory factory = new SqlServerFactory();//选择Sql Server数据库
        IFactory factory = new AccessFactory();//选择Access数据库

        IUser iUser = factory.CreateUser();
        iUser.set(user);
        iUser.get(1);

        IDepartment iDepartment = factory.CreateDepartment();
        iDepartment.set(dept);
        iDepartment.get(1);
    }
}

数据库表interface接口类

/**
 * 数据库表接口,解除与具体数据库访问的耦合
 */
public interface IUser {
    void set(User user);
    User get(int id);
}

/**
 * 数据库表接口,解除与具体数据库访问的耦合
 */
public interface IDepartment {
    void set(Department department);
    Department get(int id);
}

数据库表实体类

/**
 * 数据库表实体类
 */
public class User {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

/**
 * 数据库表实体类
 */
public class Department {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

数据库访问类

/**
 * 用于访问Access的User表
 */
public class AccessUser implements IUser {
    public void set(User user) {
        System.out.println("在Access中给User表增加一条记录");
    }

    public User get(int id) {
        System.out.println("在Access中根据ID得到User表一条记录");
        return null;
    }
}

/**
 * 用户访问Access的Department表
 */
public class AccessDepartment implements IDepartment {
    public void set(Department department) {
        System.out.println("在Access中给Department表增加一条记录");
    }

    public Department get(int id) {
        System.out.println("在Access中根据ID得到Department表一条记录");
        return null;
    }
}

/**
 * 用于访问SQL Server的User表
 */
public class SqlserverUser implements IUser {
    public void set(User user) {
        System.out.println("在SQL Server中给User表增加一条记录");
    }

    public User get(int id) {
        System.out.println("在SQL Server中根据ID得到User表一条记录");
        return null;
    }
}

/**
 * 用于访问SQL Server的Department表
 */
public class SqlserverDepartment implements IDepartment {
    public void set(Department department) {
        System.out.println("在SQL Server中给Department表增加一条记录");
    }

    public Department get(int id) {
        System.out.println("在SQL Server中根据ID得到Department表一条记录");
        return null;
    }
}

抽象接口类

/**
 * 定义一个创建访问User和Department表对象的抽象工厂接口
 */
public interface IFactory {
    IUser CreateUser();
    IDepartment CreateDepartment();
}

工厂具体实现类

/**
 * 用于实例化SqlserverUser和SqlserverDepartment的工厂类
 */
public class SqlServerFactory implements IFactory {
    public IUser CreateUser() {
        return new SqlserverUser();
    }

    public IDepartment CreateDepartment() {
        return new SqlserverDepartment();
    }
}

/**
 * 用于实例化AccessUser和AccessDepartment的工厂类
 */
public class AccessFactory implements IFactory {
    public IUser CreateUser() {
        return new AccessUser();
    }

    public IDepartment CreateDepartment() {
        return new AccessDepartment();
    }
}

你可能感兴趣的:(抽象工厂模式(Abstract Factory))