Factory design pattern, we call on the factory object is the creation method, and the factory object will generate an object of some kind of implementation of the interface. Theoretically, in this way, our code is completely separate from the implementation of the interface, which allows us to transparently replace one implementation with another.
For example, the use of factory methods to perform coin toss (Coin) and dice (Dice Class) function. The following 2 ways to implement the factory model.
The first type:
The game interface
Public interface Game {
int play ();
}
The Game Factory interface: creating objects
public interface Gamefactory () {
Game Getgame ();
}
The realization of the coin class
public class Coin implements game{
/**
* This method produces a random number
*/
public int Play () {
System.out.println ("Coin Play ()");
Random r = new Random ();
Return R.nextint (2);
}
}
The factory object of the coin produces a class: generates a Coin object
public class Coinfactory implements gamefactory{
Public Game Getgame () {
return new Coin ();
}
}
The implementation of the Dice class
public class Dice implements game{
/**
* This method produces a random number of 1~6
*/
public int Play () {
System.out.println ("Dice Play ()");
Random r = new Random ();
Return R.nextint (6);
}
}
The factory object of the dice produces a class: Generates a Dice object
public class Dicefactory implements gamefactory{
Public Game Getgame () {
return new Dice ();
}
}
Test Factory class
public class Testfactory {
public static int Game (Gamefactory gamefactory) {
Game game = Gamefactory.getgame ();
return Game.play ();
}
public static void Main (string[] args) {
Create a Coin object and invoke the Play method
Coinfactory coin = new Coinfactory ();
System.out.println (game (coin));
Create a Dice object
System.out.println (Game (New Dicefactory ()));
}
}
The above is a simple factory model, the following example uses anonymous inside to implement the Factory mode.
The second type:
The game interface
Public interface Game {
int play ();
}
The Game Factory interface: creating objects
public interface Gamefactory () {
Game Getgame ();
}
The realization of the coin class
public class Coin implements game{
Private Coin () {}
public int Play () {
System.out.println ("Coin Play ()");
return (int) (Math.random () *) +1;
}
public static Gamefactory factory = new Gamefactory () {
Public Game Getgame () {
return new Coin ();
}
};
}
The implementation of the Dice class
public class Dice implements game{
Private Dice () {}
public int Play () {
System.out.println ("Dice Play ()");
return (int) (Math.random () *6) +1;
}
public static Gamefactory factory = new Gamefactory () {
Public Game Getgame () {
return new Dice ();
}
};
}
Test class
public class Testfactory {
Public Testfactory () {}
public static void games (gamefactory fact) {
Game g = Fact.getgame ();
G.play ();
}
public static void Main (string[] args) {
Gamefactory coin = coin.factory;
Games (coin);
Games (dice.fact);
}
}
In this example, the constructors for both coin and dice can be private, and there is no need to create a named class as a factory. In addition, you often only need a single Factory object, in this case it is created in a static domain in the game implementation.
Reference: "Java Programming Ideas"
The Factory mode of Java design mode