`
DavyJones2010
  • 浏览: 151865 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

DesignPattern : Factory

阅读更多

1. Why should we use Factory Method?

    1) We want to control the way we create new instance of a certain class. We cannot merely use the new XXX() to create an instance.

    2) We want to keep our system robust and extensiable when we apply custom instaintiation of a certain class.

 

2. A simple example for explain Simple Factory Method:

    1) In a system we want to control the way we create a new vechicle.

        1) We want to apply permission check before we create new vehicle.

        2) We want to keep our vehicle number in a certain range. (Think of connection pool in JDBC)

        3) We want to keep our system extensiable and don't have to change code when we create a new kind of vehicle.

    2) The class diagram is as below.

        1) We extract the common attribute of Car and Plane as Movable.

            And create an interface Movable. Whenever we want to create another kind of Vehicle, we just implements Movable.

        2) We extract the common attribute of CarFactory and PlaneFactroy as VehicleFactory.

            And create an interface VehicleFactory. Whenever we want to apply a new kind of Vehicle generation process, we just implements VehicleFactory.

        3) We control the process of create a certain kind of Movable/Vehicle in its corrosponding VehicleFactory.

     3) Test case is as below. We can keep the code "new PlaneFactory()" in our config file.

package edu.xmu.designPattern.DesignPattern_Factory;

import org.junit.Test;

public class FactoryTest
{
	@Test
	public void testFactory()
	{
		VehicleFactory factory = new PlaneFactory();
		// VehicleFactory factory = new CarFactory();
		// VehicleFactory factory = new BroomFactory();
		Movable plane = factory.create();
		plane.move();
	}
}

  

 

3. How often Factory Method is used?

    1) We can take a look at JDK code.

        Whenever we encounter a method named getInstance(), then we can be sure that Factory Method is used to control the process of new instance generation.

        We cannot be sure whether a Singleton or a Multiton is used in the process. So we can be sure that Factory is more general aspect of instance generation than singleton.

        Singleton is only a typical kind of new instance generation. Factory method is the new instance generation process control.

    2) There are a lot of Factories in JDK.

        EG. KeyFactory, CertificateFactory... Because some kind of class is not appropriate to use the syntax of new XXX() to create an instance.

 

4. A simple for Abstract Factory Method

    1) What if a man is driving a car with a gun in his arm and an apple in his mouth. How can we create this man?

    2) Simply speaking, what if we want to create an instance of class that is associated with a series of other classes?

        What if we want to create a factory that can generate a series of instances of different classes?

    ---------- Example of creating without using Factory Method

package edu.xmu.designPattern.DesignPattern_Factory;

import org.junit.Test;

public class FactoryTest
{
	@Test
	public void testFactory()
	{
		Car car = new Car();
		AK47 ak47 = new AK47();
		Apple apple = new Apple();
		ak47.shoot();
		apple.printName();
		car.move();
	}
}

   -------- Example of creating with Factory Method

   1. Factory (Here we can control the process of new instance)

package edu.xmu.designPattern.DesignPattern_Factory;

public class DefaultFactory
{
	public Car createCar()
	{
		return new Car();
	}

	public AK47 createAK47()
	{
		return new AK47();
	}

	public Apple createApple()
	{
		return new Apple();
	}
}

   2. Test case

package edu.xmu.designPattern.DesignPattern_Factory;

import org.junit.Test;

public class FactoryTest
{
	@Test
	public void testFactory()
	{
		DefaultFactory defaultFactory = new DefaultFactory();
		Car car = defaultFactory.createCar();
		AK47 ak47 = defaultFactory.createAK47();
		Apple apple = defaultFactory.createApple();
		ak47.shoot();
		apple.printName();
		car.move();
	}
}

 

    But this factory is not extensiable/robust.

    What if we want to create a man that lives in magic world whose vehicle is broom instead of car, weapon is magic stick instead of AK47 and food is mushroom instead of apple?

    -------- Factory

package edu.xmu.designPattern.DesignPattern_Factory;

public class MagicFactory
{
	public Mushroom createMushroom()
	{
		return new Mushroom();
	}

	public MagicStick createMagicStick()
	{
		return new MagicStick();
	}
	
	public MagicBroom createMagicBroom()
	{
		return new MagicBroom();
	}
	
}

    -------- Test case (Here we have to totally change the previous code. This means the design is not extensiable)

package edu.xmu.designPattern.DesignPattern_Factory;

import org.junit.Test;

public class FactoryTest
{
	@Test
	public void testFactory()
	{
		// DefaultFactory defaultFactory = new DefaultFactory();
		// Car car = defaultFactory.createCar();
		// AK47 ak47 = defaultFactory.createAK47();
		// Apple apple = defaultFactory.createApple();
		// ak47.shoot();
		// apple.printName();
		// car.move();

		MagicFactory magicFactory = new MagicFactory();
		MagicBroom magicBroom = magicFactory.createMagicBroom();
		MagicStick magicStick = magicFactory.createMagicStick();
		Mushroom mushroom = magicFactory.createMushroom();

		// ...
	}
}

    How can we figure out a way that we don't have to change the code of using these factories?

    1) We can make the method name the same. So it is better we create an interface for these factories.

    2) We can make the way of generating object inside the factory the same name.

 

        3) Test case

package edu.xmu.designPattern.DesignPattern_Factory;

import org.junit.Test;

public class FactoryTest
{
	@Test
	public void testFactory()
	{
		AbstractFactory factory = new MagicFactory();
		// AbstractFactory factory = new DefaultFactory();

		Vehicle vehicle = factory.createVehicle();
		Weapon weapon = factory.createWeapon();
		Food food = factory.createFood();

		vehicle.run();
		weapon.shoot();
		food.printName();
	}
}

    Here we can easily switch the factory from real world to magic world.

    We can even move the code "new MagicFartory()" into config file. So now the code is simply extensiable.

    Whenever we want to make a new kind of world/factory, we just simply extends AbstractFactory. And the objects inside the world shold extends either Food, Weapon, Vehicle...

    And we merely change the config file. Then we can switch to the new world.

 

5. Comparision of two Factory

    1) We can think of Abstract Factory Method as change theme in our computer.

        When we change the theme, all the style of our button, window, text field controls have changed.

        It the same when we switch to another factory. All the products produced have changed accordingly.

       All we need to do when using the new created factory is to modify the config file.

    2) What's the advantage/disvantage of Simple Factory?

        --- 1) We can extends the kind of product, we merely need to create a corrosponding factory.

        --- 2) This would lead to Factory Flooding when we want to create a series of product.

    3) What's the advantage/disvantage of Abstract Factory?

        --- 1) We can easily create a whole new kind of factory that generate a series of product.

        --- 2) But we cannot extend the kind of product. For example, what if we want the the man is also with an armor?

                 We have to change the code of AbstractFactory as well as all the Factories that extends this AbstractFactory.

    4) How can we combine the advantages of the two kinds of factory together and discard the disadvantages?

        --- 1) There is a compromise solution in Spring framework called BeanFactory/BeanContainer/IOC.

        --- 2) Here BeanFactory is a factory created by Spring framework. When we using this, we don't have to create a factory for each of the beans defined in XML file.

        --- 3) But how can we control the process of creating new instance? What if we want to apply permission check before creation? Then it comes to AOP, another feature in Spring.

 

6. Summary

    1) The topic we disscussed is as below:

        1) What if there is only one car for the application?

            What if there is only 20 cars for the application?

            (Singleton/Multiton)

        2) What if we want to custom the process of creating vehicle and custom the type of the vehicle?

            (Simple Factory)

        3) What if we want to create a series of product like vehicle, weapon, food supply and armor?

            (Abstract Factory)

        4) What if we want to combine the benefits of Simple Factory with Abstract Factory?

            Spring Bean Factory -- Custom the process of generating production as well as the type of production.

                1) Custom the class according to a specific interface using IOC

                2) Custom the process of creating new instance using AOP.

 

  • 大小: 16.5 KB
  • 大小: 51.2 KB
分享到:
评论

相关推荐

    DesignPattern::pencil:设计模式_java实现以及详解

    本资源“DesignPattern::pencil:设计模式_java实现以及详解”提供了一套详细的学习材料,帮助开发者理解和应用设计模式。 该资源的作者是“养码青年-Style”,他通过这个项目记录了自己的设计模式学习过程。鼓励...

    DesignPattern:C#设计模式示例

    "DesignPattern:C#设计模式示例"这个资源很可能是包含多个C#实现的设计模式示例代码库。 设计模式通常分为三类:创建型、结构型和行为型。每种模式都解决了特定场景下的问题,并提供了良好的代码组织和扩展性。 ...

    designpattern:java的设计模式

    designpattern design pattern for java 描述: pattern.p001.factory :工厂模式。 pattern.p002.abstractfactory : 抽象工厂模式。 pattern.p003.singletonEH : 单例模式(饿汉式)。 pattern.p003.singletonLH : ...

    FOAD-DesignPattern:FOAD-DesignPattern

    1. 工厂模式(Factory Pattern): 工厂模式是一种创建型设计模式,它提供了一种创建对象的最佳方式。在C#中,工厂模式通过创建一个工厂类来负责生成特定类型的对象,而不是让客户端直接实例化。这样可以隐藏对象...

    factory mode ppt in design pattern

    工厂方法(Factory Method)是一种创建型设计模式,它的主要思想是定义一个创建对象的接口,但让子类决定实例化哪一个类。这样,工厂方法可以将对象的创建延迟到子类中进行,使得系统更具灵活性和扩展性。 **大图景...

    DesignPattern:设计模式小Demo

    设计模式是软件工程中的一种最佳实践,用于解决在软件设计中常见的...以上就是这个DesignPattern小Demo中可能会涵盖的设计模式,通过这些模式的实例,你可以更好地理解和应用它们到实际项目中,提升你的Java编程能力。

    DesignPattern:来自 http 的设计模式参考

    在"DesignPattern-master"这个压缩包中,可能包含了这些模式的Java实现示例,以及相关的解释和用法说明。通过学习和应用这些模式,开发者可以提高代码质量,降低维护成本,并提升软件系统的可扩展性。对于任何Java...

    DesignPattern:设计模式

    DesignPattern-master这个压缩包可能包含了一个关于设计模式的项目或者教程资源。 设计模式分为三类:创建型模式(Creational Patterns)、结构型模式(Structural Patterns)和行为型模式(Behavioral Patterns)...

    DesignPattern:有关设计模式的一些演示

    这个名为"DesignPattern:有关设计模式的一些演示"的项目,可能是为了帮助开发者理解和应用各种设计模式。 设计模式分为三大类:创建型、结构型和行为型。创建型模式关注对象的创建过程,如单例(Singleton)、工厂...

    DesignPattern:设计模式.net源代码

    本资源"DesignPattern:设计模式.net源代码"提供了一套基于.NET实现的设计模式示例,旨在帮助程序员更好地理解和应用这些模式。 在"DesignPattern-master"这个压缩包中,你可能找到的文件结构和内容包括: 1. **...

    designpattern:Head First 设计模式练习

    接着,工厂模式(Factory Method)用于创建对象,它提供了一个接口来创建对象,但让子类决定实例化哪一个类。Java中的抽象工厂类可能如下所示: ```java public abstract class ShapeFactory { public abstract ...

    Design Pattern英文版

    设计模式(Design Pattern)是软件工程中的一种经验总结,它是在特定上下文中为解决常见问题而提出的一套可复用的解决方案。设计模式并不直接实现为代码,而是提供了一种在面向对象设计中如何处理常见问题的指南。...

    designPattern:设计模式相关代码实现

    "designPattern:设计模式相关代码实现"这个项目,显然提供了不同设计模式在Java语言中的实际应用示例。 在Java世界里,设计模式主要分为三大类:创建型模式、结构型模式和行为型模式。每种模式都针对特定的编程问题...

    DesignPattern:设计模式演示程序

    这个名为"DesignPattern"的压缩包文件很可能包含了一个Java实现的各种设计模式的示例程序。 在这个"DesignPattern-master"目录中,我们可以期待找到一系列与设计模式相关的Java源代码文件(.java),每个文件或...

    Design*Pattern*Framework*4.5

    "Design*Pattern*Framework*4.5" 可能指的是一个基于 .NET Framework 4.5 的设计模式实现或教程集合。 设计模式是经验丰富的软件开发者的智慧结晶,它们被分为三类:创建型、结构型和行为型。创建型模式涉及对象的...

    designPattern:코딩사전님의정정정리

    "designPattern:코딩사전님의정정정리"可能是某位名为"정정전"的专家或教师对设计模式进行整理和修正后的资料集合,旨在帮助开发者更好地理解和应用这些模式。 首先,设计模式分为三大类:创建型、结构型和行为型...

    Design Pattern 简明手册

    ### Design Pattern 简明手册知识点详述 #### 一、接口型(interface)模式 **1. Adapter(适配器模式)** - **定义**:允许一个类接口与另一个不兼容的类接口协同工作。 - **分类**: - **继承型Adapter**:通过...

    design pattern tutorial

    教程内容涉及的标签是“python design pattern”,虽然教程是用Java语言编写,但设计模式的概念和应用是通用的,可以应用于任何面向对象的编程语言,包括Python。因此,无论是Java开发人员还是Python开发人员,都...

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

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

    DesignPattern:关于设计模式

    在这个“DesignPattern”仓库中,可能包含了对各种设计模式的详细解释、示例和应用。 设计模式分为三大类:创建型、结构型和行为型模式。创建型模式主要关注对象的创建过程,如单例模式(Singleton)、工厂模式...

Global site tag (gtag.js) - Google Analytics