Skip to main content
Commonmark migration
Source Link

##Separate Static class, and interface

Separate Static class, and interface

##Separate Static class, and interface

Separate Static class, and interface

Source Link
rolfl
  • 98.1k
  • 17
  • 220
  • 419

The code you present is neat, well structured, etc. (unfortunately it is dangerously close to being off-topic since it is hypothetical code, and it does not work - where does the Type come from in the interface's public abstract GameObject createGameObject(Type type); ? ).

The neat code is really useful because it makes it easy to home in on the critical deviation you have made from the 'idiomatic' way of doing the Factory pattern in Java.

This is (often) wrong:

public interface GameObjectFactory { public abstract GameObject createGameObject(Type type); } 

Your narrative is closer-to-home: "... seems un-necessary unless instead of an Interface it was some Abstract class"

The factory is often a combination of static methods, and an interface. Sometimes the interface and the static methods are combined on to a single abstract class, and sometimes they are separate. What you are missing is the static methods for creating the Factory instance.

Note: many factory-pattern implementations in Java do not use the word Factory in the name....

So, there are factories that separate the static methods in a different class from the interface, and factories that have the static methods and the interface methods in the same abstract class.

Abstract Class way

The Abstract class is the most recognizable way of doing this in Java. It is often most easily identified by having static methods like newInstance(). The Abstract class has abstract methods which are the factory methods, but it also has factory-factory methods for creating the factory!!!! (yes, really). The factory-factory methods are normally implemented as static concrete methods. In your example code, the GameObjectFactory and parts of the GameObjectFactoryImpl would be merged as:

public abstract class GameObjectFactory { // A helper enum that clients will use to create game objects // Note that this enum is declared as part of the 'interface' for the factory, // not as part of one of the actual fctory implementations. public enum Type { PLAYER, ENEMY; } // factory factory method here ('default' factory implementation): public static final GameObjectFactory newInstance() { // look for an implementation of the factory in some known/default place String implName = ....; // default implementation... return newInstance(implName); } // factory factory method here ('specific' factory implementation): public static final GameObjectFactory newInstance(String specificImpl) { // fancy code to create a new instance .... perhaps reflection? GameObjectFactory ret = ....... return ret; } // factory method here.... public abstract GameObject createGameObject(Type type); } 

The concrete implementation of the Abstract GameObjectFactory is somewhere else (often un-documented).

Examples of this type of factory pattern are:

##Separate Static class, and interface

In this case, there is normally a class with only static methods, and an interface that is returned from these methods. For your code, it would require three classs (the enum would be separate)....

// A helper enum that clients will use to create game objects // Note that this enum is declared as part of the 'interface' for the factory, // not as part of one of the actual fctory implementations. public enum Type { PLAYER, ENEMY; } public interface GameObjectFactory { // factory method here.... public GameObject createGameObject(Type type); } public final class GameObjectFactoryFactory { // factory factory method here ('default' factory implementation): public static final GameObjectFactory newInstance() { // look for an implementation of the factory in some known/default place String implName = ....; // default implementation... return newInstance(implName); } // factory factory method here ('specific' factory implementation): public static final GameObjectFactory newInstance(String specificImpl) { // fancy code to create a new instance .... perhaps reflection? GameObjectFactory ret = ....... return ret; } } 

Examples of this type of factory are: