1 Factory mode simple Introduction
the definition of a factory pattern: in short, to instantiate an object, instead of New operation.
The factory model is dedicated to instantiating a large number of classes with common interfaces. The mode of operation dynamically determines which class to instantiate. Don't know which class to instantiate each time.
The factory model can be divided into three different forms:
Simple Factory (Simple Factory) mode: Also known as static Factory mode ( staticfactory ).
Factory Method (Factroy Method) mode: Also multiprocessing-state Factory mode ( polymorphic Factory ).
Abstract Factory (Abstract Factroy) mode: Also known as Toolbox mode ( Kit ).
1.1 Simple Factory mode (static Factory mode):
The simple factory model has three roles: 1. Abstract product Interface 2. Detailed Product Class 3. Factory class .
1.2 Implementation of the Simple factory model (build a simplefactory package. All programs are placed under this package):
(1) First, an abstract product interface (Computer.java) is built.
Package Simplefactory;public interface Computer {public void run ();}
(2) build three detailed product classes ( Dell.java , Lenovo.java , Asus.java ), you need to implement the computer interface.
Package Simplefactory;public class Dell implements computer {public void run () {System.out.println ("Dell Computer Executed");}} Package Simplefactory;public class Lenovo implements computer {public void run () {System.out.println ("Lenovo Computer Executed");}} Package Simplefactory;public class Asus implements computer {public void run () {System.out.println ("ASUS computer executed");}}
(3) Build a factory class (Computerfactory.java).
Package Simplefactory;public class Computerfactory {public static computer Makecomputer (String ComputerName) Throws Exception {if (Computername.equals ("Dell")) { return new Dell ();} else if (computername.equals ("Lenovo")) { return new Lenovo (); } else if (computername.equals ("ASUS")) { return new Lenovo (); } else{ throw new Exception ();}}
(4) Build a client program (Testsimplefactory.java) test.
Package Simplefactory;public class Testsimplefactory {public static void main (String []args) throws Exception{computer Computer=computerfactory.makecomputer ("Dell"); Computer.run ();}}
Execution output:
The Dell Computer performed
As can be seen from the output, the factory class generates different objects depending on the number of references. Do not personally new objects.
This is the simple factory model. The simple Factory mode allows the client not to be responsible for creating objects, but is only responsible for using objects. In such a model, the product is in accordance with the opening and closing principle (open to the extension, the change is closed). But for the factory part does not conform to this principle, each add a brand of computer will have to change the factory class code. And this factory class has done all the things. is a god class, when it is out of the question, all things can not work, because it provides a static method, cannot inherit, so can not provide the inheritance hierarchy of the factory role, and can not be associated with the hierarchical structure of the product, so we want to improve the factory part, is the next analysis of the factory method pattern.
(7) Finally. Let's take a look at the UML diagram of the Simple factory model.
Figure 1.1 Simple Factory mode
Recommended articles:
A brief analysis of the factory model of Java design pattern (II.)
http://blog.csdn.net/minidrupal/article/details/38323457
Author:vicky
Introduction: Educators
Sign : Antecedent, Future
A brief analysis of the factory model of Java design pattern (i)