工厂类 java_设计模式之工厂模式(java)

工厂模式的定义为:Define an interface for creating an object ,but let subclass decide which class to instantiate,Factory Methods lets  a class defer instantiation to subclass.(定义一个用于创建对象的接口,让其子类决定实例化哪一个类,工厂方法使一个类的实例化延迟到了其子类)。

工厂模式的UML为:

d61b5e7568d68b34eb79a1526059a639.png

其java实现为:

productInf

/**

*

*/

package com.helloclq.factory;

/**

* @author chengliqun

*

*/

public interface ProductInf {

public void doSthA();

public void doSthB();

}

其两个实现,ProductA+ProductB分别为:

/**

*

*/

package com.helloclq.factory;

/**

* @author chengliqun

*

*/

public class ProductA implements ProductInf {

/* (non-Javadoc)

* @see com.helloclq.factory.ProductInf#doSthA()

*/

@Override

public void doSthA() {

// TODO Auto-generated method stub

System.out.println("ProductA-----------doSthA" );

}

/* (non-Javadoc)

* @see com.helloclq.factory.ProductInf#doSthB()

*/

@Override

public void doSthB() {

// TODO Auto-generated method stub

System.out.println("ProductA-----------doSthB" );

}

}

/**

*

*/

package com.helloclq.factory;

/**

* @author chengliqun

*

*/

public class ProductB implements ProductInf {

/* (non-Javadoc)

* @see com.helloclq.factory.ProductInf#doSthA()

*/

@Override

public void doSthA() {

// TODO Auto-generated method stub

System.out.println("ProductB-----------doSthA" );

}

/* (non-Javadoc)

* @see com.helloclq.factory.ProductInf#doSthB()

*/

@Override

public void doSthB() {

// TODO Auto-generated method stub

System.out.println("ProductB-----------doSthB" );

}

}

抽象工厂为:

/**

*

*/

package com.helloclq.factory;

/**

* @author chengliqun

*

*/

public abstract class AbsFactory {

public abstract T createProduct(Class c);

}

工厂子类为:

/**

*

*/

package com.helloclq.factory;

/**

* @author chengliqun

*

*/

public class ProductFactory extends AbsFactory {

/* (non-Javadoc)

* @see com.helloclq.factory.AbsFactory#createProduct(java.lang.Class)

*/

@Override

public T createProduct(Class c) {

// TODO Auto-generated method stub

ProductInf product = null;

try {

product = (ProductInf)Class.forName(c.getName()).newInstance();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return (T) product;

}

}

测试如下:

package com.helloclq.factory;

public class Test {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

AbsFactory factory = new ProductFactory();

ProductInf productA = factory.createProduct(ProductA.class);

productA.doSthA();

productA.doSthB();

}

}

输出结果为:

ProductA-----------doSthA

ProductA-----------doSthB

工厂模式有很多变形:

1.简单工厂模式

如果我们不需要很多工厂,一个模块就一个工厂,我们就没必要生产出工厂,直接用一个静态方法就能生产Product,这样就很简单了

/**

*

*/

package com.helloclq.factory;

/**

* @author chengliqun

*

*/

public class ProductFactory {

/* (non-Javadoc)

* @see com.helloclq.factory.AbsFactory#createProduct(java.lang.Class)

*/

@Override

public static T createProduct(Class c) {

// TODO Auto-generated method stub

ProductInf product = null;

try { product = (ProductInf)Class.forName(c.getName()).newInstance();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return (T) product;

}

}

2.工厂容器

如果我们在工厂中放一个容器,用来保存生产出来的产品,这样的话,我们可以延迟产品类的释放,也可以设置要保存的最大数量,JDBC中的连接池就是这个原理弄出来的

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值