Factory mode
explain
Factory Pattern is one of the most commonly used design patterns in Java. This type of design pattern belongs to creation pattern, which provides the best way to create objects.
effect
- Decoupling; For example, when we buy a computer, we don't need the factory model. We need a new computer, which is equivalent to building a computer ourselves. With the factory model, we just need to tell the factory what brand of computer we want, regardless of how the computer is created; There is nothing that can not be solved by adding a layer;
- High expansibility; If you want to add a product, just extend a factory class.
- Easy to maintain and reduce the amount of code; Mask the specific implementation of the product, and the caller only cares about the product interface.
classification
1. Simple factory mode (not belonging to 23 design modes, most of which are actually used)
- Create an interface
public interface Computer { void produce(); }
- Interface implementation class
DellComputer class
public class DellComputer implements Computer { public void produce() { System.out.println("Buy Dell Computers"); } }
LenovoComputer class
public class LenovoComputer implements Computer { public void produce() { System.out.println("Buy Lenovo computer"); } }
- Create a factory and create objects according to the passed parameters
public class ComputerFactory { public Computer buyComputer(String name){ if(name.equals("dell")){ return new DellComputer(); }else if(name.equals("lenovo")){ return new LenovoComputer(); }else{ return null; } } }
- Use the factory to get the corresponding object
import org.junit.Test; public class TestComputer { @Test public void getComputer(){ ComputerFactory computerFactory=new ComputerFactory(); Computer computer=computerFactory.buyComputer("dell"); computer.produce(); } }
- results of enforcement
shortcoming
If you want to buy ASUS now, you need to change the logic in the factory class, which is not in line with the opening and closing principle (open to expansion and close to modification) in the seven principles of oop, so the factory mode is introduced;
2. Factory mode
2. New product acer
AcerComputer class
public class AcerComputer implements Computer { @Override public void produce() { System.out.println("Buy Acer computer"); } }
- Declare factory interface
public interface Factory { public Computer createComputer(); }
- Factory class producing acer
public class AcerComputerFactor implements Factory { @Override public Computer createComputer() { return new AcerComputer(); } }
- Factory class producing lenovo
public class LenovoComputerFactor implements Factory { @Override public Computer createComputer() { return new LenovoComputer(); } }
- Factory class producing dell
public class DellComputerFactor implements Factory { @Override public Computer createComputer() { return new DellComputer(); } }
- Use the factory to get the corresponding object
import org.junit.Test; public class TestComputer { @Test public void getComputer(){ Factory factory; factory=new DellComputerFactor(); factory.createComputer().produce(); } }
- Operation results
shortcoming
No matter how many classes are added, we do not need to modify the code in the original class, but by adding factory classes. However, if there are too many classes, we have to create multiple factories, which is troublesome;
summary
Simple factory: a factory class and a product abstract class.
Factory method: multiple factory classes, one product abstract class.