java模式之工厂模式和抽象工厂模式

工厂模式是创建模式,什么是工厂模式呢。顾名思义,我们的社会发展了,以前是作坊式的,现在是工厂式了。生产产品不需要一家一家作坊式的生产了,放到工厂里一起生产。再举个通俗的例子,以前找人才一个个的找,现在有了人才市场,就跑到人才市场统一找,多方便啊。


采用工厂模式主要是为了解决新类型对象的增加对原来系统的影响,当然这影响越小越好。所以工厂模式实现了创建实例与使用实例的分开,
把创建实例集中在一起控制,这样,新类型对象的增加只局限于Factory对象中。

注意以上说的工厂模式其实包括工厂模式和抽象工厂模式。


抽象工厂模式在创建对象实例上稍微比较复杂一点,举个例子,
一个农场中由一个人来管理农场中的所有产品,这就是简单工厂模式,这种模式只适合小的农场。
当农场规模扩大后,一个人来管理显示不合适,这时就需要多个人来管理,相当于有多个具体工厂类,每个人管理不同的产品,如张三管理大
白菜,李四来管理苹果,这是工厂模式.
如果农场进一步扩大,比如大白菜这个品种,有北方的大白菜,有南方的大白菜,或者更多,其它的产品与大白菜类似,分成许多地域的品种
,这时就要采用抽象工厂模式。
农场的抽象工厂模式:
一个抽象产品类,用来管理南方的产品,包括大白菜等
另一个抽象产品类,用来管理北方的产品,包括大白菜等

一个抽象工厂类,多个具体工厂类,其中一个专门管理各地的大白菜,其它类似。
一般情况下使用工厂类的比较多,如之前我们做过的项目,对于operator对象的生成使用的都是工厂模式。
  Operator operator = (Operator) Class.forName(name.trim())
                .newInstance();
根据关键字name的不同动态取得不同的operator对象。源代码如下:
 public class OperatorFactoryImpl implements OperatorFactory{

    private static final Logger logger = Logger.getLogger(OperatorFactoryImpl.class);
    private static OperatorFactoryImpl instance =  new OperatorFactoryImpl();
   
    private static final String KEY_MAPPING_PROPERTIES="operatorMapping.properties";
   
    public static OperatorFactoryImpl getInstance(){
        return instance;
    }
    public OperatorFactoryImpl(){
        Properties p = new Properties();
     
        try {
            p.load(getClass().getResourceAsStream(KEY_MAPPING_PROPERTIES));
           Enumeration en =p.keys();
           while(en.hasMoreElements()){
               String key = en.nextElement().toString();
               operators.put(key,p.get(key));
           }
          
        } catch (IOException e) {
            e.printStackTrace();
        }
      
       
    }
      
    private HashMap operators = new HashMap();
   
    public Operator create(String key) {
        String name = (String)operators.get(key);
        logger.debug("the name is "+name);
        if(name!=null){
            try {
                 Operator operator = (Operator) Class.forName(name.trim())
                .newInstance();
                return operator;
            } catch (InstantiationException e) {            
                e.printStackTrace();
            } catch (IllegalAccessException e) {           
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
              
                e.printStackTrace();
            }
        }
       
        return null;
    }
}


下面举例一个DAO模式中实现抽象工厂,顺便也看看是怎么使用DAO模式的。
为DAO实现工厂类的策略

1 采用工厂方法设计模式
如果一个DAO 工厂只为一个数据库的实现,(比如ORACLE)而创建很多的DAO的时候,实现该策略时,我们考虑采用工厂方法设计模式. 假设该工厂

类创建了CustomerDAO, AccountDAO, OrderDAO 等一些对象。

2 使用抽象工厂设计模式:

如果考虑为三种不同类型的数据库来实现这个策略,我们可以考虑采用抽象工厂设计模式. 假设. 这个工厂创建了CustomerDAO, AccountDAO,

OrderDAO的一系列的DAO, 该策略运用了在抽象工厂中产生的工厂类中的工厂方法的实现.

代码说明:

以下代码举例说明了DAO设计模式的具体实现:
我们以使用抽象工厂的设计模式来对付多种类型数据库为例,在以下的例子中只具体列出CLOUDSCAPE 数据库类型的DAO设计模式的具体实现,其

他类型数据库DAO设计模式的实现大同小异.

1 // Abstract class DAO Factory
public abstract class DAOFactory {

  // List of DAO types supported by the factory
  public static final int CLOUDSCAPE = 1;
  public static final int ORACLE = 2;
  public static final int SYBASE = 3;
  ...

  // There will be a method for each DAO that can be
  // created. The concrete factories will have to
  // implement these methods.
// 所有实现该抽象工厂的工厂类中必须有的方法,用这些方法来创建具体的DAO类.
  public abstract CustomerDAO getCustomerDAO();
  public abstract AccountDAO getAccountDAO();
  public abstract OrderDAO getOrderDAO();

//该抽象类的静态方法,用他来创建其他具体的DAO工厂类
  public static DAOFactory getDAOFactory(
      int whichFactory) {
 
    switch (whichFactory) {
      case CLOUDSCAPE:
          return new CloudscapeDAOFactory();
      case ORACLE    :
          return new OracleDAOFactory();     
      case SYBASE    :
          return new SybaseDAOFactory();
      ...
      default           :
          return null;
    }
  }
}


2 以下是Cloudscape DAO FACTORY 类的实现,在他里面实现了该类型数据库的连接,以及实现了他所继承的抽象工厂类中所必须实现的那些方法

,在这些方法中创建具体的DAO对象.

// Cloudscape concrete DAO Factory implementation
import java.sql.*;

public class CloudscapeDAOFactory extends DAOFactory {
  public static final String DRIVER=
    "COM.cloudscape.core.RmiJdbcDriver";
  public static final String DBURL=
    "jdbc:cloudscape:rmi://localhost:1099/CoreJ2EEDB";

  // method to create Cloudscape connections
//建立Cloudscape 连接
  public static Connection createConnection() {
    // Use DRIVER and DBURL to create a connection
    // Recommend connection pool implementation/usage
  }
//创建 CustomerDAO 对象 当然返回的是一个该类实现的接口,他的好处就是实现了实现细节的隐蔽.
  public CustomerDAO getCustomerDAO() {
    // CloudscapeCustomerDAO implements CustomerDAO
    return new CloudscapeCustomerDAO();
  }
//创建 AccountDAO 对象 当然返回的是一个该类实现的接口,他的好处就是实现了实现细节的隐蔽.
  public AccountDAO getAccountDAO() {
    // CloudscapeAccountDAO implements AccountDAO
    return new CloudscapeAccountDAO();
  }
//创建 OrderDAO 对象 当然返回的是一个该类实现的接口,他的好处就是实现了实现细节的隐蔽.

  public OrderDAO getOrderDAO() {
    // CloudscapeOrderDAO implements OrderDAO
    return new CloudscapeOrderDAO();
  }
  ...
}

3 以下代码就是具体DAO类实现的接口也就是CloudscapeCustomerDAO()实现的接口: CustomerDAO .在该接口中定义了所有的业务方法.


// Interface that all CustomerDAOs must support
public interface CustomerDAO {
  public int insertCustomer(...);
  public boolean deleteCustomer(...);
  public Customer findCustomer(...);
  public boolean updateCustomer(...);
  public RowSet selectCustomersRS(...);
  public Collection selectCustomersTO(...);
  ...
}

4 以下CloudscapeCustomerDAO类实现的具体业务细节和数据操作细节, 他是要向客户数据端隐蔽的.

import java.sql.*;
public class CloudscapeCustomerDAO implements
    CustomerDAO {
  public CloudscapeCustomerDAO() {
    // initialization
  }
  // The following methods can use
  // CloudscapeDAOFactory.createConnection()
  // to get a connection as required
  public int insertCustomer(...) {
    // Implement insert customer here.
    // Return newly created customer number
    // or a -1 on error
  }
  public boolean deleteCustomer(...) {
    // Implement delete customer here
    // Return true on success, false on failure
  }
  public Customer findCustomer(...) {
    // Implement find a customer here using supplied
    // argument values as search criteria
    // Return a Transfer Object if found,
    // return null on error or if not found
  }
  public boolean updateCustomer(...) {
    // implement update record here using data
    // from the customerData Transfer Object
    // Return true on success, false on failure or
    // error
  }
  public RowSet selectCustomersRS(...) {
    // implement search customers here using the
    // supplied criteria.
    // Return a RowSet.
  }
  public Collection selectCustomersTO(...) {
    // implement search customers here using the
    // supplied criteria.
    // Alternatively, implement to return a Collection
    // of Transfer Objects.
  }
  ...
}

5 下面的代码是数据客户端向DAO中传输数据的, 他其实就是一个JAVABEAN;

public class Customer implements java.io.Serializable {
  // member variables
  int CustomerNumber;
  String name;
  String streetAddress;
  String city;
  ...

  // getter and setter methods...
  ...
}


6  最后就是客户数据端对这个设计的应用:
...
// create the required DAO Factory
DAOFactory cloudscapeFactory =  
  DAOFactory.getDAOFactory(DAOFactory.DAOCLOUDSCAPE);
// Create a DAO
CustomerDAO custDAO =
  cloudscapeFactory.getCustomerDAO();
// create a new customer
int newCustNo = custDAO.insertCustomer(...);
// Find a customer object. Get the Transfer Object.
Customer cust = custDAO.findCustomer(...);
// modify the values in the Transfer Object.
cust.setAddress(...);
cust.setEmail(...);
// update the customer object using the DAO
custDAO.updateCustomer(cust);
// delete a customer object
custDAO.deleteCustomer(...);
// select all customers in the same city
Customer criteria=new Customer();
criteria.setCity("New York");
Collection customersList =
  custDAO.selectCustomersTO(criteria);
// returns customersList - collection of Customer
// Transfer Objects. iterate through this collection to
// get values.

言而简之,以下6步完成该模式的实现:

1 创建一个抽象工厂类,他包含两个重要的部分: 第一部分是 一些抽象方法,这些方法是所有实现该抽象工厂的具体工厂类所必须实现的. 第二

部分 就是一个静态方法,该方法来创建一个具体类型数据源的工厂对象,比如文中的CloudscapeDAOFactory().

2 然后,分别创建各个类型数据源的工厂类,(本文以CloudscapeDAOFactory为例).在这个工厂类中里面也有两个重要组成部分: 第一部分就是实

现在他继承的那个抽象工厂类中的左右抽象方法,在该方法中创建具体的DAO对象(这些对象的类在第4不具体定义实现),本文中三个方法分别创

建了3个具体的DAO对象,当然为了实现细节的隐蔽,这些方法返回的是这些具体DAO类门实现的接口(这些接口在第3步实现).

3 定义具体DAO类的接口,并在接口中定义所有的业务方法,和数据操作方法.

4 定义具体的DAO类,在这个类中才是实际的业务方法,和数据的操作的实现.

5 定义数据传输对象,他是用来在客户端和DAO之间传递数据的,他其实就是一个JAVABEAN.

6 完成以上5步之后我们就可以在数据客户端使用以上由DAO设计模式定义好的各个类了(见最后一个代码例子块).

以上6步大家在编程的时需具体体会,一般来说,数据库中的一个表就可以对应一个数据传递类也就是在第4步中定义的那个类,类中的属性就是表

中的字段,然后加上相应的GET,SET 方法. 然后再按模式和以上步骤来定义具体的类.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值