• 设计模式——抽象工厂模式(AbstractFactoryPattern)


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

    UML图:

    IFactory:

    package com.cnblog.clarck;
    
    public interface IFactory {
        IUser createUser();
        
        IDepartment createDepartment();
    }

    SqlserverFactory:

    package com.cnblog.clarck;
    
    public class SqlserverFactory implements IFactory {
    
        @Override
        public IUser createUser() {
            return new SqlserverUser();
        }
    
        @Override
        public IDepartment createDepartment() {
            return new SqlserverDepartment();
        }
    
    }
    AccessFactory:
    package com.cnblog.clarck;
    
    public class AccessFactory implements IFactory {
    
        @Override
        public IUser createUser() {
            return new AccessUser();
        }
    
        @Override
        public IDepartment createDepartment() {
            return new AccessDepartment();
        }
    
    }

    IUser:

    package com.cnblog.clarck;
    
    public interface IUser {
        void insert(User user);
        
        User getUser(int id);
    }

    AccessUser:

    package com.cnblog.clarck;
    
    public class AccessUser implements IUser {
        
        @Override
        public void insert(User user) {
            System.out.println("在Access中给User表增加一条记录");
        }
    
        @Override
        public User getUser(int id) {
            System.out.println("在Access中根据ID得到User表一条记录");
            return null;
        }
    
    }

    SqlserverUser:

    package com.cnblog.clarck;
    
    public class SqlserverUser implements IUser {
    
        @Override
        public void insert(User user) {
            System.out.println("在SQL Server中给User表增加一条记录");
        }
    
        @Override
        public User getUser(int id) {
            System.out.println("在SQL Server中根据ID得到User表一条记录");
            return null;
        }
    }

    IDepartment:

    package com.cnblog.clarck;
    
    /**
     * IDepartment 接口,用于客户端访问,解除与具体数据库访问的耦合
     * 
     * @author clarck
     * 
     */
    public interface IDepartment {
        public void insert(Department department);
    
        public Department getDepartment(int id);
    }

    AccessDepartment:

    package com.cnblog.clarck;
    
    public class AccessDepartment implements IDepartment {
    
        @Override
        public void insert(Department department) {
            System.out.println("在Access中给Department表增加一条记录");
        }
    
        @Override
        public Department getDepartment(int id) {
            System.out.println("在Access中根据ID得到Department表一条记录");
            return null;
        }
    
    }

    SqlserverDepartment:

    package com.cnblog.clarck;
    
    public class SqlserverDepartment implements IDepartment {
        
        @Override
        public void insert(Department department) {
            System.out.println("在SQL Server中给Department表添加一条记录");
        }
    
        @Override
        public Department getDepartment(int id) {
            System.out.println("在SQL Server中根据ID得到Department表的一条记录");
            return null;
        }
    
    }

    Department:

    package com.cnblog.clarck;
    
    public class Department {
        private int mID;
        private String mDeptName;
    
        public int getID() {
            return mID;
        }
    
        public void setID(int mID) {
            this.mID = mID;
        }
    
        public String getDeptName() {
            return mDeptName;
        }
    
        public void setDeptName(String mDeptName) {
            this.mDeptName = mDeptName;
        }
    
    }

    User:

    package com.cnblog.clarck;
    
    public class User {
        private int mID;
        private String mName;
    
        public int getID() {
            return mID;
        }
    
        public void setID(int mID) {
            this.mID = mID;
        }
    
        public String getName() {
            return mName;
        }
    
        public void setName(String mName) {
            this.mName = mName;
        }
    
    }

     Client:

    package com.cnblog.clarck;
    
    public class Client {
        public static void main(String[] args) {
            User user = new User();
            Department department = new Department();
            
            //IFactory factory = new SqlserverFactory();
            IFactory factory = new AccessFactory();
            
            IUser iu = factory.createUser();
            iu.insert(user);
            iu.getUser(1);
            
            IDepartment iDepartment = factory.createDepartment();
            iDepartment.insert(department);
            iDepartment.getDepartment(1);
        }
    }
  • 相关阅读:
    VS2012快捷操作功能
    SQL Union和SQL Union All用法
    安装sql server 2008 R2出现 创建usersettings/microsoft.sqlserver.configuration.landingpage.properties.setter
    编码的来历和使用 utf-8 和GB2312比较
    Java内存机制,内存地址
    spring
    使用CMD建立指定格式的文件
    在命令提示符窗口下(cmd)使用指令操作并编译java代码,运行java编译代码
    匿名内部类的使用总结
    网页中插入FLASH(swf文件)的html代码
  • 原文地址:https://www.cnblogs.com/tanlon/p/3451477.html
Copyright © 2020-2023  润新知