什么时候在对象中使用工厂方法而不是factory类是一个好主意?


当前回答

工厂类更重量级,但也有一定的优势。当您需要从多个原始数据源构建对象时,它们允许您在一个地方只封装构建逻辑(可能还包括数据的聚合)。在那里可以进行抽象的测试,而不需要考虑对象接口。

我发现这是一种有用的模式,特别是当我无法替换ORM且ORM不足,并且希望有效地实例化来自DB表连接或存储过程的许多对象时。

其他回答

我认为这取决于你想要给你的代码带来的松耦合程度。

工厂方法解耦得很好,但是工厂类不行。

换句话说,使用工厂方法比使用简单的工厂(称为工厂类)更容易更改内容。

看看这个例子:https://connected2know.com/programming/java-factory-pattern/。现在,想象一下你想要带来一个新的动物。在Factory类中,您需要更改Factory,但在Factory方法中,不需要,您只需要添加一个新的子类。

UML从

Product:它定义Factory方法创建的对象的接口。

ConcreteProduct:实现Product接口

创建者:声明Factory方法

ConcreateCreator:实现Factory方法以返回ConcreteProduct的实例

问题陈述:使用定义游戏界面的工厂方法创建游戏工厂。

代码片段:

import java.util.HashMap;


/* Product interface as per UML diagram */
interface Game{
    /* createGame is a complex method, which executes a sequence of game steps */
    public void createGame();
}

/* ConcreteProduct implementation as per UML diagram */
class Chess implements Game{
    public Chess(){

    }
    public void createGame(){
        System.out.println("---------------------------------------");
        System.out.println("Create Chess game");
        System.out.println("Opponents:2");
        System.out.println("Define 64 blocks");
        System.out.println("Place 16 pieces for White opponent");
        System.out.println("Place 16 pieces for Black opponent");
        System.out.println("Start Chess game");
        System.out.println("---------------------------------------");
    }
}
class Checkers implements Game{
    public Checkers(){

    }
    public void createGame(){
        System.out.println("---------------------------------------");
        System.out.println("Create Checkers game");
        System.out.println("Opponents:2 or 3 or 4 or 6");
        System.out.println("For each opponent, place 10 coins");
        System.out.println("Start Checkers game");
        System.out.println("---------------------------------------");
    }
}
class Ludo implements Game{
    public Ludo(){

    }
    public void createGame(){
        System.out.println("---------------------------------------");
        System.out.println("Create Ludo game");
        System.out.println("Opponents:2 or 3 or 4");
        System.out.println("For each opponent, place 4 coins");
        System.out.println("Create two dices with numbers from 1-6");
        System.out.println("Start Ludo game");
        System.out.println("---------------------------------------");
    }
}

/* Creator interface as per UML diagram */
interface IGameFactory {
    public Game getGame(String gameName);
}

/* ConcreteCreator implementation as per UML diagram */
class GameFactory implements IGameFactory {

     HashMap<String,Game> games = new HashMap<String,Game>();
    /*  
        Since Game Creation is complex process, we don't want to create game using new operator every time.
        Instead we create Game only once and store it in Factory. When client request a specific game, 
        Game object is returned from Factory instead of creating new Game on the fly, which is time consuming
    */

    public GameFactory(){

        games.put(Chess.class.getName(),new Chess());
        games.put(Checkers.class.getName(),new Checkers());
        games.put(Ludo.class.getName(),new Ludo());        
    }
    public Game getGame(String gameName){
        return games.get(gameName);
    }
}

public class NonStaticFactoryDemo{
    public static void main(String args[]){
        if ( args.length < 1){
            System.out.println("Usage: java FactoryDemo gameName");
            return;
        }

        GameFactory factory = new GameFactory();
        Game game = factory.getGame(args[0]);
        if ( game != null ){                    
            game.createGame();
            System.out.println("Game="+game.getClass().getName());
        }else{
            System.out.println(args[0]+  " Game does not exists in factory");
        }           
    }
}

输出:

java NonStaticFactoryDemo Chess
---------------------------------------
Create Chess game
Opponents:2
Define 64 blocks
Place 16 pieces for White opponent
Place 16 pieces for Black opponent
Start Chess game
---------------------------------------
Game=Chess

这个例子通过实现FactoryMethod展示了一个Factory类。

Game is the interface for all type of games. It defines complex method: createGame() Chess, Ludo, Checkers are different variants of games, which provide implementation to createGame() public Game getGame(String gameName) is FactoryMethod in IGameFactory class GameFactory pre-creates different type of games in constructor. It implements IGameFactory factory method. game Name is passed as command line argument to NotStaticFactoryDemo getGame in GameFactory accepts a game name and returns corresponding Game object.

工厂:

创建对象而不向客户端公开实例化逻辑。

FactoryMethod

定义一个用于创建对象的接口,但是让子类来决定实例化哪个类。Factory方法允许类延迟实例化到子类

用例:

何时使用:客户端不知道在运行时需要创建什么具体的类,而只是想获得一个可以完成这项工作的类。

当您需要几个具有相同参数类型但具有不同行为的“构造函数”时,它们也很有用。

在对象中使用工厂方法是个好主意:

对象的类不知道它必须创建什么确切的子类 Object的类被设计成它创建的对象由子类指定 对象的类将其职责委托给辅助子类,并且不知道确切的类将承担这些职责

在以下情况下使用抽象工厂类是个好主意:

对象不应该依赖于其内部对象是如何创建和设计的 一组链接的对象应该一起使用,您需要满足这个约束 对象应该由几个可能的链接对象家族中的一个来配置,这些对象将成为父对象的一部分 它需要共享只显示接口而不显示实现的子对象

工厂类更重量级,但也有一定的优势。当您需要从多个原始数据源构建对象时,它们允许您在一个地方只封装构建逻辑(可能还包括数据的聚合)。在那里可以进行抽象的测试,而不需要考虑对象接口。

我发现这是一种有用的模式,特别是当我无法替换ORM且ORM不足,并且希望有效地实例化来自DB表连接或存储过程的许多对象时。