I can't figure out why you're so cruel. Let's use the factory method model today.
First, what is the factory method mode?
The so-called factory method model is the upgraded version of the Simple factory model.
The factory method pattern and the simple factory model are the same except for the factory class.
Second, with the simple factory model why still use factory method mode?
The simple factory model seems very easy. Because of this. His factory class violates the principle of open closure in design mode. Take the calculator in the first sentence for example. Change the switch branch in the factory class each time you add a new operation.
This violates the extension of the object-oriented design, not the change-it is also the open and closed principle of previous learning. How to solve the problem? We are able to use the dependency reversal principle to be intact.
Three, Code demonstration sample (on the basis of the first words calculator changes)
First change:
Change the previous Operationfactory.java such as the following
Public interface Operationfactory {
Public Operation CreateOperation ();
}
A second change:
Join the new factory class: Addfactory.java
public class Addfactory implements Operationfactory {
Public Operation CreateOperation () {
TODO auto-generated Method Stub
return new Addoperation ();
}
}
Instantiating operationfactory using Addfactory
Operationfactory factory = new Addfactory ();
Operation operation = Factory.creaoperation ();
Note: This image is from "Big Talk design mode"
Iv. Summary
This mode is very easy because of the basics of the previous simple factory model and the closed open principle. Learn together, come on.
Eighth session-factory method mode