zoukankan      html  css  js  c++  java
  • 设计模式11:抽象工厂模式

    阅读材料:http://baike.xsoftlab.net/view/209.html#3_2

    抽象工程模式解决的问题:

    当前有一套产品,称为A;另一套产品称为B。客户端代码需要在两套产品中切换。

    抽象工厂模式相当于在简单工厂模式之上又抽象了一层,使得客户端代码在两套产品切换中比较方便。

    但是抽象工厂模式还是会引发大量的改动。如果要减少改动,可以使用反射+简单工厂解决这个问题。

    UML:

    User相关代码:

    package abstractfactory;
    
    public abstract class IUser {
    	
    	public abstract void select();
    	
    	public abstract void insert();
    }
    
    package abstractfactory;
    
    public class MySQLUser extends IUser {
    
    	@Override
    	public void select() {
    		System.out.println("MySQL select User");
    	}
    
    	@Override
    	public void insert() {
    		System.out.println("MySQL insert User");
    	}
    
    }
    
    package abstractfactory;
    
    public class OracleUser extends IUser {
    
    	@Override
    	public void select() {
    		System.out.println("Oracle select User");
    	}
    
    	@Override
    	public void insert() {
    		System.out.println("Oracle insert User");
    	}
    
    }
    

    Department相关代码:

    package abstractfactory;
    
    public abstract class IDepartment {
    	
    	public abstract void select();
    	
    	public abstract void insert();
    
    }
    
    package abstractfactory;
    
    public class MySQLDepartment extends IDepartment {
    
    	@Override
    	public void select() {
    		System.out.println("MySQL select Department");
    	}
    
    	@Override
    	public void insert() {
    		System.out.println("MySQL insert Department");
    	}
    
    }
    
    package abstractfactory;
    
    public class OracleDepartment extends IDepartment {
    
    	@Override
    	public void select() {
    		System.out.println("Oracle select Department");
    	}
    
    	@Override
    	public void insert() {
    		System.out.println("Oracle insert Department");
    	}
    
    }
    

    DataBase类:

    使用反射确定要实例化哪个子类,数据库类型存储在xml中。

    package abstractfactory;
    
    import java.io.IOException;
    import javax.xml.parsers.*;
    import org.w3c.dom.*;
    import org.xml.sax.SAXException;
    
    public class DataBase {
    	private static String databaseName = null;
    	
    	static{
    		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
    		DocumentBuilder builder = null;
    		try {
    			builder = factory.newDocumentBuilder();
    		} catch (ParserConfigurationException e) {
    			e.printStackTrace();
    		}     
            Document document = null;
    		try {
    			document = builder.parse("src\abstractfactory\Settings.xml");
    		} catch (SAXException | IOException e) {
    			e.printStackTrace();
    		}  
            Element database = (Element) document.getDocumentElement();
            databaseName = database.getAttribute("Name");
    	}
    	
    	public static IUser getUserHandler() 
    			throws InstantiationException, 
    			IllegalAccessException, 
    			ClassNotFoundException {
    		String classname = "abstractfactory." + databaseName + "User";
    		return (IUser) Class.forName(classname).newInstance();
    	}
    	
    	public static IDepartment getDepartmentHandler() 
    			throws InstantiationException, 
    			IllegalAccessException, 
    			ClassNotFoundException {
    		String classname = "abstractfactory." + databaseName + "Department";
    		return (IDepartment) Class.forName(classname).newInstance();
    	}
    }
    

    settings.xml

    <?xml version="1.0" encoding="UTF-8"?><DataBase Name="Oracle"/>

    Demo:

    package abstractfactory;
    
    public class AbstractFactoryDemo {
    	
    	public static void main(String[] args) {
    		IUser user;
    		IDepartment department;
    		try {
    			user = DataBase.getUserHandler();
    			user.select();
    			user.insert();
    			department = DataBase.getDepartmentHandler();
    			department.select();
    			department.insert();
    		} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
    			e.printStackTrace();
    		}
    		
    	}
    	
    }
    

      

  • 相关阅读:
    Hibernate实体对象三种状态
    tar命令: 对某目录文件打tar包时,排除指定的目录或文件
    开发项目时,提示 找不到类的解决方法,以及如何设置编译源目录
    当html中存在url中如: onclick="toView('参数1')", 参数1是特别字符,如&asop;&quot;' "等时,浏览器解析时会报错。解决方法如文中描述
    oracle表分区心得
    启动系统相关服务笔记整理
    使用PSD设计网页页面
    JAR、WAR、EAR 区别
    设置 MyEclipse 默认打开文件方式
    前端性能优化
  • 原文地址:https://www.cnblogs.com/zcy-backend/p/6701090.html
Copyright © 2011-2022 走看看