创建型——工厂模式(工厂方法)
JAVA设计模式学习之----创建模式:工厂模式(工厂方法)
工厂模式分为工厂方法和抽象工厂。。
工厂模式用于生成对象。。。
一般我们生成对象使用new 如Person p = new Person();来实现,但我们在创建这个对象的时候可能要做一些其它的事情。。
当然java中通过重载构造函数可以实现如Person p = new Person(String name);,但通过增加参数的方法还是不能满足我们的需求,如是如果我要新建对象的时候要进行数据库的相关操作,那么采用重载构造函数的方法肯定会使构造函数中的代码相当长,不仅仅是因为不好看,主要是因为它违背了面向对象的设计原则:高内聚低耦合性。
好了,以Person类为例:现在有两个类继承Person,分别为Zhangshan,Zhangsi,那么我们采用工厂模式来产生对象,根据面向接口编程的原则我们将Person抽象成一个接口,那么有的人会说Zhangshan,Zhangsi实现Person接口然后实例化的时候用
Person p1 = new Zhangshan();
Person p2 = new Zhangsi();
不就要以了吗?为什么要工厂模式呢。
我们没有想到,有一天我们会有更多的类继承Person类,那么是不是我们都要使用Person pn = new ....的形式来产生实例呢?
显然不是明智之举。。。现在我们看工厂模式的代码:
首先Person接口:
package com.foactory.eus.interfaces;
public interface Person{
public static final String className= "Person" ;
String getName();
int getAge();
}
接口的实现类:Zhangshan
package com.foactory.eus.implement;
import com.foactory.eus.interfaces.Person;
public class Zhangshan implements Person{
public StringgetName(){
System. out .println( "thisiszhangshanisname" );
return "" ;
}
public int getAge(){
return 0;
}
}
接口的实现类:Zhangsi
package com.foactory.eus.implement;
import com.foactory.eus.interfaces.Person;
public class Zhangsi implements Person{
public int getAge(){
return 0;
}
public String getName(){
System. out .println( "thisiszhangsiisname" );
return null ;
}
}
再看看工厂类及方法:PersonFactory
package com.foactory.eus.factory;
import com.foactory.eus.implement.Zhangshan;
import com.foactory.eus.implement.Zhangsi;
import com.foactory.eus.interfaces.Person;
public class PersonFactory{
public static Person instance( int which){
if (which==1){
return new Zhangshan();
} else {
return new Zhangsi();
}
}
public static void main(String[]args){
Person p1=PersonFactory.instance(1);
p1.getName();
Person p2=PersonFactory.instance(2);
p2.getName();
}
}
上面的main方法用来测试的,打印输出:
this is zhangshan is name
this is zhangsi is name
你可以看到这时候如果我们有新的Person的实现类的话,我们只需在PersonFactory 类中加入一段代码就可以了。
如增加 Zhangwu的实例如下:
if (which==2){
return new Zhangwu();
}
好了,上面讲的就是打工厂模式中的工厂方法了。如果系统不是很复杂,用这个就可以了。。。
以上的实现我们可称之为简单工厂
下面的代码是针对本文最上面的类图所设计的代码,我们称为工厂方法
以下是工厂类为抽象类,由其子类创建对象实例的例子。
package com.lwf.factorymethod;
public interface Product {
void getName();
void getNum();
}
package com.lwf.factorymethod;
public class ConcreateProductA implements Product {
public void getName() {
System.out.println("con productA");
}
public void getNum() {
System.out.println("con productA");
}
}
package com.lwf.factorymethod;
public class ConcreateProductB implements Product {
public void getName() {
System.out.println("con productB");
}
public void getNum() {
System.out.println("con productB");
}
}
package com.lwf.factorymethod;
public abstract class Factory {
public abstract Product createProduct();
}
package com.lwf.factorymethod;
public class ProductFactoryA extends Factory {
public Product createProduct() {
return new ConcreateProductA();
}
}
package com.lwf.factorymethod;
public class ProductFactoryB extends Factory {
public Product createProduct() {
return new ConcreateProductB();
}
}
package com.lwf.factorymethod;
public class TestFactoryMethod {
public static void main(String[] args) {
Factory factory = new ProductFactoryA();
Product product = factory.createProduct();
product.getName();
product.getNum();
factory = new ProductFactoryB();
product = factory.createProduct();
product.getName();
product.getNum();
}
}
输出结果为:
con productA
con productA
con productB
con productB
附件为相应代码