简单工厂模式

来源:互联网 发布:java脚本语言 编辑:程序博客网 时间:2024/06/06 12:27

一、什么是简单工厂模式

         简单工厂模式属于类的创建型模式,又叫做静态工厂方法模式。通过专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类。

二、模式中包含的角色及其职责

1.工厂(Creator)角色
简单工厂模式的核心,它负责实现创建所有实例的内部逻辑。工厂类可以被外界直接调用,创建所需的产品对象。

2.抽象(Product)角色
简单工厂模式所创建的所有对象的父类,它负责描述所有实例所共有的公共接口。

3.具体产品(Concrete Product)角色

简单工厂模式所创建的具体实例对象

三、简单工厂模式的优缺点

在这个模式中,工厂类是整个模式的关键所在。它包含必要的判断

逻辑,能够根据外界给定的信息,决定究竟应该创建哪个具体类的

对象。用户在使用时可以直接根据工厂类去创建所需的实例,而无

需了解这些对象是如何创建以及如何组织的。有利于整个软件体系

结构的优化。

不难发现,简单工厂模式的缺点也正体现在其工厂类上,由于工厂类集中

了所有实例的创建逻辑,所以“高内聚”方面做的并不好。另外,当系统中的

具体产品类不断增多时,可能会出现要求工厂类也要做相应的修改,扩展

性并不很好。


package com.dw.test;//2.抽象(Product)角色简单工厂模式所创建的所有对象的父类,它负责描述所有实例所共有的公共接口。public interface Gather {   void get();}
package com.dw.test;//3.具体产品(Concrete Product)角色简单工厂模式所创建的具体实例对象public class apple implements Gather {   //苹果的get方法public void get() {System.out.println("我是苹果");}}

package com.dw.test;/*1.工厂(Creator)角色简单工厂模式的核心,它负责实现创建所有实例的内部逻辑。 * 工厂类可以被外界直接调用,创建所需的产品对象。 * */public class GatherFactory {   //获取apple的实例public static Gather getApple() {return new apple();}//获取orange的实例public static Gather getOrange() {return new orange();}/** * 获取所有产品对象 * @throws IllegalAccessException  * @throws InstantiationException  * @throws ClassNotFoundException  */public static Gather getGather(String type) throws InstantiationException, IllegalAccessException, ClassNotFoundException{if(type.equalsIgnoreCase("apple")){   returnapple.class.newInstance();}else if(type.equalsIgnoreCase("orange")){return orange.class.newInstance();}else{System.out.println("找不到响应的实例化类");return null;}/*    //为达到通用Class<Gather>gather=(Class<Gather>) Class.forName(type.trim());     return gather.newInstance();*/}}

package com.dw.test;public class MainClass {public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {      /*apple apl=new apple();apl.get();orange org=new orange();org.get();*//*Gather gather=new orange();Gather gather1=new apple();gather.get();gather1.get();*///获得工厂类的实例/*Gather gather=GatherFactory.getApple();Gather gather1=GatherFactory.getOrange();gather.get();gather1.get();*///Gather gather=GatherFactory.getGather("apple");   Gather gather1=GatherFactory.getGather("orange");gather1.get();//gather1.get();}}






0 0