Before saying the factory method pattern, first look at a simple factory pattern. Factory Method pattern is in fact a simple factory did a few enhancements.
Simple factory pattern: There is a special class to the production of other instances of the class, the production of these examples have a common parent class. This polymorphism with us a bit like.
Example:
If we have a mouse factory, the factory can produce both mouse Lenovo, Acer and can produce a mouse.
Code as follows:
1, factories mouse (the mouse can produce Lenovo and Acer mouse)
public class MouseFactory {
/**
* 生产一个鼠标
* @param i
* @return
*/
public Mouse createMouse(int i){
Mouse mouse = null;
switch (i){
case 1:
mouse = new LenovoMouse();
break;
case 2:
mouse = new AcerMouse();
break;
}
return mouse;
}
}
2, mouse interface (a getName () method)
public interface Mouse {
void getName();
}
3, Lenovo Mouse implementation class (implement mouse interface, the mouse can produce association)
public class LenovoMouse implements Mouse {
@Override
public void getName() {
System.out.println("这个是联想鼠标");
}
}
4, Acer implementation class mouse (mouse interface implemented, can produce Acer mouse)
public class AcerMouse implements Mouse {
@Override
public void getName() {
System.out.println("这个是宏碁鼠标");
}
}
5, the client (who needs a mouse, you can buy Lenovo mouse can also buy Acer mouse)
@SpringBootTest
class FactoryApplicationTests {
@Test
void contextLoads() {
MouseFactory factory = new MouseFactory();
Mouse mouse = factory.createMouse(2);
mouse.getName();
}
Even such a simple factory achieved, can pass 1 and 2, respectively, so that the plant generated a mouse Lenovo and Acer mouse.
Next we look at the factory method pattern on the basis of a simple plant.
Factory Method pattern: the definition of a special interface to the production of other class instance, let subclasses decide which class of specific instances of production.
Example:
There are a mouse factory opened two subsidiaries, a Lenovo mouse factory specializing in the production used to associate the mouse, the other is the Acer mouse factory for the production of specialized Acer mouse.
Code as follows:
1, mouse factory interface:
public interface MouseFactory {
Mouse createMouse();
}
2, Lenovo mouse factory (for the production of Lenovo Mouse)
public class LenovoMouseFactory implements MouseFactory {
@Override
public Mouse createMouse() {
return new LenovoMouse();
}
}
3, Acer mouse facility (Acer used to produce mouse)
public class AcerMouseFactory implements MouseFactory {
@Override
public Mouse createMouse() {
return new AcerMouse();
}
}
With simple mouse-type plants, as reproduced below:
4, mouse interface (there is a method getName ()) is
public interface Mouse {
void getName();
}
5, Lenovo Mouse implementation class (implement mouse interface, the mouse can produce association)
public class LenovoMouse implements Mouse {
@Override
public void getName() {
System.out.println("这个是联想鼠标");
}
}
6, Acer implementation class mouse (mouse interface implemented, can produce Acer mouse)
public class AcerMouse implements Mouse {
@Override
public void getName() {
System.out.println("这个是宏碁鼠标");
}
}
7, the client (who needs a mouse, you can buy Lenovo mouse can also buy Acer mouse)
@SpringBootTest
class FactoryApplicationTests {
@Test
void demo(){
LenovoMouseFactory lenovoMouseFactory = new LenovoMouseFactory();
Mouse lenovoMouse = lenovoMouseFactory.createMouse();
lenovoMouse.getName();
AcerMouseFactory acerMouseFactory = new AcerMouseFactory();
Mouse acerMouse = acerMouseFactory.createMouse();
acerMouse.getName();
}
}
We can get different instances in different factories, if we want to buy a mouse Lenovo, the Lenovo factory to buy. Acer to buy a mouse, then go buy the Acer plant.
Thus, we realized the factory method pattern, if we want an Asus mouse, you build a factory to produce ASUS ASUS mouse.
If the article helpful, please remember to focus on points like yo ~
Welcome to my public concern number <Love it>, pushing daily technical articles for them to learn.