The Factory mode of Java design mode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.