`
pouyang
  • 浏览: 313515 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Design Patterns 创建模式 之 Factory 工厂模式

阅读更多
1 Factory
意图:提供一个创建一系列相关和相互依赖对象的接口,而无需指定他们具体的类。
假设我们需要进行各种数据库操作,可是我们有很多的选择啊,JDBC,Hibernate,Ibatis等等。本来选择一个就OK了,可是变态的Boss为了炫耀我们的技术如何如何强大,竟说我们能够无成本地在这三个解决方案间切换。这下系统该怎么设计呢?
或许你已经注意到每种解决方案自身都是完备而独立的,不错,这便是解决问题的切入点。提供一套解决方案公有行为的接口,各个解决方案分别实现这些接口,同时建立一个创建各个解决方案的工厂(Factory)。这不就完成任务了吗?

1.1 Simple Factory
首先,如果一个客户要用到一款手机,一般的做法是自己去创建一款手机,然后拿来用,如图:

这时,客户需要知道怎么去创建手机,与手机紧密的耦合起来了。如是简单工厂模式如下:

1),手机抽象类:
public abstract class Mobile {
	
}
2),手机实现类
Nokia实现类:
public class MobileNokia extends Mobile{
	MobileNokia() {
		System.out.println("I am Nokia!");
	}
}

Iphone实现类:
public class MoblieIphone extends Mobile{
	MoblieIphone() {
		System.out.println("I am Iphone!");
	}
}
3),工厂类
public class MobileFactory {
		public static Mobile creatMobileInstance(String type) {
		if ("Nokia".equals(type)) {
			return new MobileNokia();
		} else if ("Iphone".equals(type)) {
			return new MoblieIphone();
		} else {
			return null;
		}
	}
}

我要Iphone
MobileFactory.creatMobileInstance("Iphone");

简单工厂模式往往是普通工厂模式的一个特例。简单工厂模式基本是用一个类来模拟工厂,通过该工厂类的静态方法返回具体的产品类,而这些产品又源自一个抽象的产品(当然,这儿可能会存在多种抽象产品,即有多种不同类型的产品,如蔬菜、瓜果等等),即静态工厂模式。书中提及了几个对象:水果以及具体的水果,园丁;园丁根据得到的参量来决定提取某种水果。如果存在多种抽象产品,必须考察抽象产品是由抽象类来实现还是用接口来表现了,如果具体产品之间拥有共同的商业逻辑,一般都应用抽象类来表现,否则应用接口来表现,在一个类型的等级结构中,将共同的部分尽量向上塑造。相应的,工厂也可以分为多个工厂,各自负责不同种类的产品即抽象类。

考虑一下,如果我们新增了一种产品,那么在我们的静态工厂类中就必须再加入返回该新增的产品类的方法,是需要修改这个工厂类的。这个到底应该如何才能不修改工厂类呢?或许我们的工厂类不应该同具体产品类来发生关联关系。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.2 Factory Method

当产品增加的时候,并不需要改动抽象工厂(提供给外部的接口),只需要改动工厂实现类即可。
例子 产品:
public abstract class Mobile {
}
public class MobileNokia extends Mobile{
	MobileNokia() {
		System.out.println("I am Nokia!");
	}
}
public class MoblieIphone extends Mobile{
	MoblieIphone() {
		System.out.println("I am Iphone!");
	}
}

工厂
public interface class Factory {
	public Mobile createMobile();   
}
public class FactoryNokia implements Factory{
	
	public Mobile createMobile() {
		return new MobileNokia() ;
	}
}
public class FactoryIphone implements Factory {
	
	public Mobile createMobile() {
		return new MoblieIphone() ;
	}
}

测试
public class Main {
	public static void main(String[] args) {
		Factory factoryNokia =  new FactoryNokia();
		factoryNokia.createMobile();
		Factory factoryIphone =  new FactoryIphone();
		factoryIphone.createMobile();
	}
}

工厂方法模式是类的创建模式,定义一个创建工厂接口,将实际的创建工作推到子类工厂中。也就是原本的静态类变成了一个抽象类或接口,而将制造的任务交给底下的具体实现类来完成。工厂方法模式似乎同抽象工厂模式很类似,但或许引入产品族后可以做一下比较。

抽象产品A(具体产品A1, 具体产品A2),抽象工厂F可以只有一个具体工厂,实现生成A的抽象方法。即工厂方法模式是将注意集中在可抽象产品概念上(意即多个具体产品可抽象化)。

如果引入产品族的概念,即有多个抽象产品,那么就必须对应不同的抽象产品形成不同的抽象工厂层,这会打破工厂的独立性,乃至形成一个庞大的工厂族。自然工厂方法模式不能很好的解决这种问题。因为有了产品族的引入,自然就引入了抽象工厂模式。


用工厂来作新产品有点浪费....
不如用状态位来区分产品
工厂是用在方法实现上有区别的业务类中的.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.3 Abstract Factory
解决问题的切入点是“每种解决方案自身都是完备而独立的”,所以这些具体的各种解决方案的工厂对Consumer也应该是透明的。于是我们给这些工厂(解决方案)加上一个工厂,也就是我们通过这个工厂来生成一整套的解决方案。这个工厂便是我们要说的AbstractFactory
本着重构的精神,工厂类应该由工厂自身来解决,不应该由客户去创建。把它和接口Factory合而为一,成为一个抽象类。大概这便是这种设计模式名字的由来吧。
例子如下:
产品:
public abstract class Mobile {
}
public class MobileNokia extends Mobile{
	MobileNokia() {
		System.out.println("I am Nokia!");
	}
}
public class MoblieIphone extends Mobile{
	MoblieIphone() {
		System.out.println("I am Iphone!");
	}
}


工厂:
public abstract class AbstractFactory {
	/**
	 * 根据不同的体系要求创建出不同的业务工厂
	 * @param factoryName
	 * @return
	 */
	public static  AbstractFactory  getFactory(String factoryName) {
		 AbstractFactory factory = null;  
		 if ("nokia".equals(factoryName)) {
			 factory = new FactoryNokia();
		 }  else if ("iphone".equals(factoryName)) {
			 factory = new FactoryNokia();
		 }
		 return factory;
	}
	/**
	 * 建立各个业务接口,由各实现的工厂来创建具体的Mobile
	 * @return
	 */
	public abstract Mobile createMobile();   
}
public class FactoryNokia extends AbstractFactory{
	@Override
	public Mobile createMobile() {
		return new MobileNokia() ;
	}
}
public class FactoryIphone extends AbstractFactory {
	@Override
	public Mobile createMobile() {
		return new MoblieIphone() ;
	}
}
public class Main {
	public static void main(String[] args) {
		AbstractFactory factoryNokia =  AbstractFactory.getFactory("nokia");
		factoryNokia.createMobile();
		AbstractFactory factoryIphone =  AbstractFactory.getFactory("iphone");
		factoryIphone.createMobile();
	}
}


小结
正如我上面举的这个例子,抽象工厂模式主要适用于需要提供若干种不同实现体系,但又要求有相同处理要求的时候。如Portal技术中,各种Portlet便是不同的体系,但我们在处理Portal的时候都是把他们统一当作Portlet实现,同样这种解决问题的思路可以用在SOA中。这种设计模式大多出现在各个系统进行集成的时候。

spring中管理bean的方式,对各Factory进行管理

1,首先规定好一种管理格式
<beans>   
  <bean id="A" class="com.javaeye.pouyang.FactoryNokia" />   
  <bean id="B" class="com.javaeye.smartfool.FactoryIphone" />   
</beans> 

2,修改Factory
这样,我们可以在AbstractFactory加入一个读配置的方法getXmlClass,通过输入id值来读出其对应的class名。这样,我们的AbstractFactory变成
public abstract class AbstractFactory {   
   
    // 读取配置文件   
    private static String getXmlClass(String id){   
        ......   
    }   
       
    public static AbstractFactory getFactory(String factoryName) {   
        AbstractFactory ist = null;   
        String className = getXmlClass(factoryName);   
        try {   
            ist = (AbstractFactory) Class.forName(className).newInstance();   
        } catch (Exception e) {   
            e.printStackTrace();   
            ist = null;   
        }    
        return ist;   
    }   
   
    public abstract Service createService();   
}  

这样,就可以解除AbstractFactory对子类的依赖了。
without ejb 很好的一本书! 非常感谢lintomny
分享到:
评论

相关推荐

    Design.Patterns.Explained.Simply

    We've tried hard to avoid both of these categories with Design Patterns Explained Simply. This book is fast and simple way to get the idea behind each of the 29 popular design patterns. The book is ...

    .NET Design Patterns [Kindle Edition]

    After reading this book, you will be able to convincingly leverage these design patterns (factory pattern, builder pattern, prototype pattern, adapter pattern, facade pattern, decorator pattern, ...

    Head First Design Patterns 英文版 Head First设计模式

    《Head First Design Patterns》共有14章,每章都介绍了几个设计模式,完整地涵盖了四人组版本全部23个设计模式。前言先介绍《Head First Design Patterns》的用法;第1章到第11章陆续介绍的设计模式为Strategy、...

    Beginning SOLID Principles and Design Patterns for ASP.NET Developers.pdf

    Beginning SOLID Principles and Design Patterns for ASP.NET Developers ■Chapter 1: Overview of SOLID Principles and Design Patterns ■Chapter 2: SOLID Principles ■Chapter 3: Creational Patterns: ...

    Apress.Pro.Design.Patterns.in.Swift

    Pro Design Patterns in Swift shows you how to harness the power and flexibility of Swift to apply the most important and enduring design patterns to your applications, taking your development ...

    《Java Design Patterns》高清完整英文PDF版

    Learn how to implement design patterns in Java: each pattern in Java Design Patterns is a complete implementation and the output is generated using Eclipse, making the code accessible to all....

    DesignPatterns_Factory

    理解Factory(工厂)设计模式的最通俗demo

    Head First Design Patterns

    第1章到第11章陆续介绍的设计模式为Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton、Command、Adapter、Facade、Templat Method、Iterator、Composite、 State、Proxy。最后三章比较...

    Learning Python Design Patterns(PACKT,2013)

    Then you will move on to learn about two creational design patterns which are Singleton and Factory, and two structural patterns which are Facade and Proxy. Finally, the book also explains three ...

    Programming.in.the.Large.with.Design.Patterns

    Title: Programming in the Large with Design Patterns Author: Eddie Burris Length: 156 pages Edition: 1 Language: English Publisher: Pretty Print Press Publication Date: 2012-07-04 ISBN-10: 0615662145 ...

    Design Patterns Elements of Reusable Object-Oriented Software

    • How Design Patterns Solve Design Problems • How to Select a Design Pattern • How to Use a Design Pattern A Case Study: Designing a Document Editor • Design Problems • Document Structure ...

    Head First Design Patterns 高清英文版

    第1章到第11章陆续介绍的设计模式为Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton,Command、Adapter、Facade、TemplateMethod、Iterator、Composite、State、Proxy。最后三章比较...

    Learning Python Design Patterns 2nd 2016第2版 无水印pdf 0分

    As you progress through the book, you will learn about Singleton patterns, Factory patterns, and Facade patterns in detail. After this, we'll look at how to control object access with proxy patterns....

    C++设计模式(Design Pattern)范例源代码

    23种设计模式(Design Pattern)的C++实现范例,包括下面列出的各种模式,代码包含较详细注释。另外附上“设计模式迷你手册.chm”供参考。 注:项目在 VS2008 下使用。 创建型: 抽象工厂模式(Abstract Factory) 生成...

    Android Design Patterns and Best Practice

    design patterns, the factory and abstract factory. Chapter 2 , Creational Patterns, covers material and interface design, exploring the design support library and the builder design pattern.

    Head First Design Patterns 英文原版

    of Design Patterns so that you can hold your own with your co-worker (and impress cocktail party guests) when he casually mentions his stunningly clever use of Command, Facade, Proxy, and Factory in ...

    设计模式(Design.Patterns.CHN)

    2.5.3 Abstract Factory模式 35 2.6 支持多种窗口系统 35 2.6.1 我们是否可以使用Abstract Factory 模式 35 2.6.2 封装实现依赖关系 35 2.6.3 Window和WindowImp 37 2.6.4 Bridge 模式 40 2.7 用户操作 40 2.7.1 ...

Global site tag (gtag.js) - Google Analytics