Factory Method
- Definition: The definition of a created object's interface, but let Chinese interface implementation class to decide which class to instantiate
- Factory method to instantiate the class allow deferred to subclasses were. - This is the core idea
- Type: Creating type
Applicable scene
- Creating an object requires a lot of repetitive code
- The client does not depend on how the instance of the class is to create products and achieve other details
- A class by class to develop its child which object is created
in principle
- java polymorphism and Richter substitution principle
- When the program is running, an object subclass object overrides the parent class making it easier to expand the system.
advantage
- Users only need to plant the desired product corresponding relationship need not create a relationship details
- Add a new product line with the principle of opening and closing, scalability
Shortcoming
- The number of classes is easy too, adds complexity
- Add abstraction of the system and the difficulty of understanding
And the difference between simple plant
- Simple factory implementation object created in the factory
- Factory method call the object's parent class in the factory method to create specific creation process is implemented by subclasses.
- If the junior partner of simple plant can not very clear reference design patterns - simple factory
Roles
- Abstract Factory
- Abstract factory implementation
- Abstract objects
- Concrete objects
Coding
-
We make changes on this basis design patterns - simple factory .
-
Production of computer
public interface Computer {
/**
* 生产电脑
*/
void produce();
}
- The achievement of specific computer
public class AppleComputer implements Computer {
public void produce() {
System.out.println("苹果电脑");
}
}
public class LenovoComputer implements Computer {
public void produce() {
System.out.println("联想电脑");
}
}
- Computer production plant
public abstract class ComputerFactory {
/**
* 获取电脑的抽象方法
* @return
*/
abstract Computer getComputer();
}
- Specific production of a computer factory
public class AppleComputerFoctory extends ComputerFactory{
@Override
Computer getComputer() {
return new AppleComputer();
}
}
public class LenovoComputerFoctory extends ComputerFactory{
@Override
Computer getComputer() {
return new LenovoComputer();
}
}
- use
public class FactoryMethodTest {
public static void main(String[] args) {
//工厂方法
ComputerFactory computerFactory = new AppleComputerFoctory();
Computer computer = computerFactory.getComputer();
computer.produce();
ComputerFactory computerFactory1 = new LenovoComputerFoctory();
Computer computer1 = computerFactory1.getComputer();
computer1.produce();
}
}
- UML 图
- From the figure we see concrete to create the parent class is not in the factory to achieve, but to the individual products factory class implementation.
- If we need a new Dell computer. We need to define a Dell product, and Dell factory class can
Source
- Other design patterns source address in here, oh! Source Address
important point
- Factory method uses a product family, in other words, the same type of product. If you want to add a side refrigerators on the method just does not apply, but we will next abstract factory.