Creational Patterns Part 1/5: Factory Pattern

原创 2016年05月30日 16:09:43

Creational Patterns Part 1/5: Factory Pattern

目录


Definition

Factory Pattern是Java中最常用的设计模式之一。这个模式属于Creational Patterns的原因就是它会Create一个对象。

何时使用?当希望使用统一的接口创建不同的实现类对象并且对用户隐藏对象的创建逻辑时。

使用频率:Frequency of use High


UML Class Diagram

Factory Pattern


Implementation

Shape接口,含有draw()方法。
创建Shape接口的实现类(Circle, Square和Rectangle)就可以交给ShapeFactory来实现。


// Shape.java
package designpatterns.creationalpatterns.factory;

public interface Shape {
    void draw();
}

// FactoryDemo.java
package designpatterns.creationalpatterns.factory;

public class FactoryDemo {
    public static void main(String[] args) {
        ShapeFactory factory = new ShapeFactory();

        Shape shape = factory.getShape("circle");
        shape.draw();

        shape = factory.getShape("square");
        shape.draw();

        shape = factory.getShape("rectangle");
        shape.draw();
    }
}

class ShapeFactory {

    Shape getShape(String shapeType) {
        if (shapeType == null)  return null;

        if (shapeType.equalsIgnoreCase("CIRCLE")) {
            return new Circle();
        } else if (shapeType.equalsIgnoreCase("SQUARE")) {
            return new Square();
        } else if (shapeType.equalsIgnoreCase("RECTANGLE")) {
            return new Rectangle();
        }
        return null;
    }
}

class Circle implements Shape {

    @Override
    public void draw() {
        System.out.println("Circle::draw");
    }
}

class Square implements Shape {

    @Override
    public void draw() {
        System.out.println("Square::draw");
    }
}

class Rectangle implements Shape {

    @Override
    public void draw() {
        System.out.println("Rectangle::draw");
    }
}

// output
Circle::draw
Square::draw
Rectangle::draw
版权声明:本文为博主原创文章,未经博主允许不得转载。

Creational Patterns Part 5/5: Prototype Pattern

Creational Patterns Part 5/5: Prototype Pattern目录 Creational Patterns Part 55 Prototype Pattern 目录 D...

Design Pattern - Creational Patterns - Factory Pattern

Section 1, Chapter 1 Factory Pattern Intr: Factories use a base class type to create a class ...

Design Pattern - Creational Patterns - Abstract Factory Pattern

2007 Section 2, Chapter 2 Abstract Factory Pattern Concept Since...

Behavioral Patterns Part 1/11: Chain Of Responsibility Pattern

Behavioral Patterns Part 1/11: Chain Of Responsibility Pattern目录 Behavioral Patterns Part 111 Chain ...

【C++ Desgin Pattern-Creational】Abstract Factory (抽象工厂)

------文章编辑参考机械工业出版社《设计模式 可复用面向对象软件的基础》 模型目的: 提供一个创建一系列相关或相互依赖的对象的接口,而无需指定其具体的类。 -----------------...
  • KG_Qt
  • KG_Qt
  • 2012年02月03日 20:45
  • 306

Design Pattern - Creational Patterns - Singleton Pattern

2007 Section 1, Chapter 1 Singleton Pattern Concept The Singleton pattern is a way t...

Design Pattern - Creational Patterns - Builder Pattern

2007 Section 3, Chapter 2 Builder Pattern Concept Builders usually deal with constru...

OOP Design (Head First Design Patterns) 学习笔记--05 The factory Pattern

When you see "new", think "concrete"! 1. Identify what varies 2. Encapsulate object creation Factori...

Head First Design Patterns 阅读笔记之四: Factory Pattern

new 的问题每当你使用 new 实例化一个具体的类时,那这就是一种实现而不是接口,而具体的实现会导致代码脆弱并且不灵活。 从技术上说,new 没有问题。问题在于需求不断变化,而这些变化又会影响 n...

设计模式(Design Pattern) - 创建型模式(Creational Pattern) - 简单工厂模式(Simple Factory Pattern) - Java实现

简单工厂模式(Simpale Factory Pattern):定义了一个工厂类,可以根据参数的不同返回不同类的实例,被创建的实例通常都具有共同的父类。 因为在简单工厂模式中用于创建实例的方法是静态...
  • achi010
  • achi010
  • 2017年07月19日 00:39
  • 135
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Creational Patterns Part 1/5: Factory Pattern
举报原因:
原因补充:

(最多只允许输入30个字)