JAVA设计模式学习之----创建模式:工厂模式(工厂方法)

来源:互联网 发布:网络用语飒是什么意思 编辑:程序博客网 时间:2024/05/27 12:22

工厂模式分为工厂方法和抽象工厂。。
工厂模式用于生成对象。。。
一般我们生成对象使用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接口:

  1. package com.foactory.eus.interfaces;
  2. public interface Person {
  3.     public static final String className = "Person";
  4.     String getName();
  5.     int getAge();
  6. }


接口的实现类:Zhangshan

  1. package com.foactory.eus.implement;
  2. import com.foactory.eus.interfaces.Person;
  3. public class Zhangshan implements Person{
  4.     /* (non-Javadoc)
  5.      * @see com.foactory.eus.interfaces.Person#getName()
  6.      */
  7.     public String getName() {
  8.         System.out.println("this is zhangshan is name");
  9.         return "";
  10.     }
  11.     public int getAge() {
  12.         return 0;
  13.     }
  14. }


接口的实现类:Zhangsi

  1. package com.foactory.eus.implement;
  2. import com.foactory.eus.interfaces.Person;
  3. public class Zhangsi implements Person {
  4.     /* (non-Javadoc)
  5.      * @see com.foactory.eus.interfaces.Person#getAge()
  6.      */
  7.     public int getAge() {
  8.         // TODO Auto-generated method stub
  9.         return 0;
  10.     }
  11.     /* (non-Javadoc)
  12.      * @see com.foactory.eus.interfaces.Person#getName()
  13.      */
  14.     public String getName() {
  15.         // TODO Auto-generated method stub
  16.         System.out.println("this is zhangsi is name");
  17.         return null;
  18.     }
  19. }


再看看工厂类及方法:PersonFactory

  1. package com.foactory.eus.factory;
  2. import com.foactory.eus.implement.Zhangshan;
  3. import com.foactory.eus.implement.Zhangsi;
  4. import com.foactory.eus.interfaces.Person;
  5. public class PersonFactory {
  6.     
  7.     public static  Person instance(int which){
  8.         if(which == 1){
  9.             return new Zhangshan();
  10.         }else{
  11.             return new Zhangsi();
  12.         }
  13.     }
  14.     
  15.     public static void main(String[] args){
  16.         Person p1 = PersonFactory.instance(1);
  17.         p1.getName();
  18.         
  19.         Person p2 = PersonFactory.instance(2);
  20.         p2.getName();
  21.         
  22.         
  23.     }
  24. }
    1. if(which == 2){
    2.    return new Zhangwu();
    3. }



上面的main方法用来测试的,打印输出:

  1. this is zhangshan is name
  2. this is zhangsi is name

你可以看到这时候如果我们有新的Person的实现类的话,我们只需在PersonFactory 类中加入一段代码就可以了。
如增加
Zhangwu的实例如下:

  1. if(which == 2){
  2.    return new Zhangwu();
  3. }

好了,上面讲的就是打工厂模式中的工厂方法了。如果系统不是很复杂,用这个就可以了。。。

原创粉丝点击