Interface Application and factory method mode, interface factory Mode
interface Printer{public void open();public void close();public void print(String s);}
Class HP implements Printer {public void open () {System. out. println ("HP printer Boot");} public void print (String s) {System. out. println (s);} private void clean () {System. out. println ("clean");} public void close () {this. clean (); System. out. println ("HP printer shutdown ");}}
Class Canon implements Printer {public void open () {System. out. println ("canon printer Boot");} public void print (String s) {System. out. println (s);} public void close () {System. out. println ("canon printer shutdown ");}}
Class PrintFactory {public static Printer getPrinter (int flag) {// generate the corresponding Printer object based on the selected Printer and convert it to printer type Printer = null; if (flag = 1) {printer = new HP () ;}else {printer = new Canon () ;}return printer ;}}
class test{public static void main(String args[]){int flag = 0;Printer p = PrintFactory.getPrinter(flag);p.open();p.print("test");p.close();}}
Printer is an interface. The HP class and Canon class are used to implement the interface. The code for generating objects of HP and Canon classes is encapsulated in the getPrinter method of PrintFactory. Call the getPrinter method of PrintFactory to generate a printer object. The advantage is that, when generating objects in a large system and using the printer function, duplicate code is reduced. users do not need to know the printer type, and it is also convenient for developers to modify the printer type.
JAVA's questions about the factory method mode and the use of multiple interfaces
However, you need to implement many interfaces for each specific class .. Small programs cannot see the benefits of the factory model ..
How to apply the factory Model
It is recommended that LZ study the simple factory, factory method and abstract factory together. Haha, my teacher asked me to study the differences between the three models, so I may be more familiar with learning together.
The simple factory mode is also called the static factory method mode. After renaming, we can see that this mode must be very simple. Its purpose is to define an interface for creating objects.
Let's take a look at its composition:
1) Factory roles: this is the core of this model and contains some commercial logic and judgment logic. In java, it is often implemented by a specific class.
2) Abstract Product role: it is generally the parent class or implemented interface inherited by a specific product. It is implemented by interfaces or abstract classes in java.
3) specific product role: the object created by the factory class is the instance of this role. It is implemented by a specific class in java.
The factory method mode removes the static attribute of the factory method in the simple factory mode so that it can be inherited by the quilt class. In this way, the pressure on the factory method in the simple factory mode can be shared by different factory subclass in the factory method mode.
Take a look at its composition:
1) Abstract Factory role: this is the core of the factory method model and has nothing to do with the application. It is an interface that must be implemented by a specific factory role or a parent class that must be inherited. In java, it is implemented by abstract classes or interfaces.
2) specific factory role: it contains Code related to the specific business logic. An application is called to create the objects of a specific product.
3) Abstract Product role: it is the parent class or implemented interface inherited by a specific product. In java, abstract classes or interfaces are generally used for implementation.
4) specific product role: the object created by the specific factory role is the instance of this role. It is implemented by specific classes in java.