Reference Documentation:
1. Design mode-the basis of reusable object-oriented software
2.http://blog.csdn.net/hguisu/article/details/7505909 (design mode (i) Factory mode factory (created))
Factory mode (Factory method) (Object-creation mode)
Theoretical knowledge, you can refer to the reference document ...
Let's take a look at the structure of factory method:
Code implementation:
The case is an example of a maze built in the book.
Mazegame.java:
Package Com.rick.designpattern.factory_method;
/** * Created by MyPC on 2017/6/21.
*/public class Mazegame {public Maze Createmaze () {Maze amaze = new Maze ();
R1 = makeroom (1);
r2 = Makeroom (2);
Door Thedoor = Makedoor (r1, r2);
Amaze.addroom (R1);
Amaze.addroom (R2);
R1.setside (Room.Side.North, Makewall ());
R1.setside (Room.Side.East, Thedoor);
R1.setside (Room.Side.South, Makewall ());
R1.setside (Room.Side.West, Makewall ());
R2.setside (Room.Side.North, Makewall ());
R2.setside (Room.Side.East, Makewall ());
R2.setside (Room.Side.South, Makewall ());
R2.setside (Room.Side.West, Thedoor);
return amaze;
} public Maze Makemaze () {return new Maze ();
} public Makeroom (int n) {return new (n);
} public Wall Makewall () {return new Wall ();
} public Door Makedoor (guest r1, R2) {return new Door (R1, r2);
}
}
Bombedmazegame.java:
Package Com.rick.designpattern.factory_method;
/**
* Created by MyPC on 2017/6/21.
*
/public class Bombedmazegame extends Mazegame {
@Override public
Wall Makewall () {
return new Bombedwall ();
}
@Override public
makeroom (int n) {
return new Roomwithabomb (n);}
}
Enchantedmazegame.java:
Package Com.rick.designpattern.factory_method;
/**
* Created by MyPC on 2017/6/21.
*
/public class Enchantedmazegame extends Mazegame {
private Spell Castspell () {
return new Spell ();
}
@Override public
makeroom (int n) {
return new Enchantedroom (n, Castspell ());
}
@Override public
Door Makedoor (hostel r1, guest R2) {
return new Doorneedingspell (R1, r2);
}
}
Client.java:
Package Com.rick.designpattern.factory_method;
/**
* Created by MyPC on 2017/6/21.
*/Public
class Client {public
static void Main (string[] args) {
Maze Maze = new Mazegame (). Createmaze ();
maze maze1 = new Bombedmazegame (). Createmaze ();
Maze maze2 = new Enchantedmazegame (). Createmaze ();
}
}
The parent class of the Mazegame factory method above, the corresponding Bombedmazegame and Enchantedmazegame are subclasses. There are also client test classes.
Some of the following are the product classes:
Maze.java:
Package Com.rick.designpattern.factory_method;
Import java.util.ArrayList;
Import java.util.List;
/** *
Maze
* <p>
* Created by MyPC on 2017/6/21.
*
/public class Maze {
private list<room> rooms = new arraylist<room> ();
public void Addroom (guest) {
Rooms.add (guest);
}
}
Room.java:
Package Com.rick.designpattern.factory_method;
Import Java.util.HashMap;
Import Java.util.Map;
/**
* Created by MyPC on 2017/6/21.
*/Public
class
, private int n;
Private Map<side, object> walls = new Hashmap<side, object> ();
public (int n) {
THIS.N = n;
}
public void Setside (Side Side, Object o) {
walls.put (Side, O);
}
public enum Side {
n, East, South, West;
}
}
Door.java:
Package Com.rick.designpattern.factory_method;
/**
* Created by MyPC on 2017/6/21.
*/Public
class Door {
private r1, R2;
Public Door (R1, r2) {
this.r1 = r1;
THIS.R2 = r2;
}
}
Wall.java:
Package Com.rick.designpattern.factory_method;
/**
* Created by MyPC on 2017/6/21.
*/Public
class Wall {
}
Bombedwall.java:
Package Com.rick.designpattern.factory_method;
/**
* Created by MyPC on 2017/6/21.
*/Public
class Bombedwall extends Wall {
}
Doorneedingspell.java:
Package Com.rick.designpattern.factory_method;
/**
* Created by MyPC on 2017/6/21.
*/Public
class Doorneedingspell extends Door {public
Doorneedingspell (hostel r1, class R2) {
super (R1, r2);
}
}
Enchantedroom.java:
Package Com.rick.designpattern.factory_method;
/**
* Created by MyPC on 2017/6/21.
*/Public
class Enchantedroom extends (
private Spell Spell;
public enchantedroom (int n, Spell Spell) {
super (n);
This.spell = spell;
}
}
Roomwithabomb.java:
Package Com.rick.designpattern.factory_method;
/**
* Created by MyPC on 2017/6/21.
*
/public class Roomwithabomb extends. {Public
roomwithabomb (int n) {
super (n);
}
}
Spell.java:
Package Com.rick.designpattern.factory_method;
/**
* Created by MyPC on 2017/6/21.
*/Public
class Spell {
}