Design pattern Learning--Factory mode

Source: Internet
Author: User

the design pattern for today's study is Factory mode. So why call it Factory mode? Factory model, as the name implies is a factory to produce a variety of things, the factory model is how to reflect it?

First of all, the factory model has a simple Factory mode and abstract Factory mode two, we first use a hair factory to demonstrate the Simple Factory mode:

A hairstyle interface: Factory mode is interface-oriented programming, which makes it easier to extend the program

Package com.sunny.project;/** * Hairstyle interface * @author Administrator * */public interface Hairinterface {/** * draw a hairstyle */public void Draw ();}
Write two hairstyles to implement this interface:

Package com.sunny.project;/** * Left-biased * @author Administrator * */public class Lefthair implements Hairinterface {@Overrid Epublic void Draw () {//TODO auto-generated method StubSystem.out.println ("-----------------left-biased distribution-------------------" );}} Package com.sunny.project;/** * Right-biased distribution * @author Administrator * */public class Righthair implements Hairinterface {@Overri Depublic void Draw () {//TODO auto-generated method StubSystem.out.println ("-----------------right-biased distribution------------------- ");}}
If we do not use the factory model, then we are generally new these two objects to do our operation, but this is very unfavorable to our program management, because these hairstyles are belong to the implementation of the hairstyle interface, we can be a general class to the series of hair generation example? This time is the application of the factory model, we write a special production of hair type factory class:

Package Com.sunny.project;import java.util.map;/** * Hairstyle Factory * @author Administrator * */public class Hairfactory {/** * by type To create the object * @param key * @return */public hairinterface gethair (String key) {if ("left". Equals (Key)) {return new Lefthair ()} else if ("right". Equals (Key)) {return new Righthair ();} return null;} /** * Production Object according to the name of the class * @param className * @return */public hairinterface gethairbyclass (String className) {try {Hairinterface Hair = (hairinterface) class.forname (className). newinstance (); return hair;} catch (Instantiationexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (Illegalaccessexception  e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (ClassNotFoundException e) {//Todo auto-generated catch Blocke.printstacktrace ();} return null;} /** * Production Object according to the name of the class * @param className * @return */public hairinterface gethairbyclasskey (String key) {try {map<string, St ring> map = Propertiesreader.getinstance (). GetProperties (); Hairinterface hair = (hairinteRface) Class.forName (Map.get (Key)). Newinstance (); return hair;} catch (Instantiationexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (Illegalaccessexception  e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (ClassNotFoundException e) {//Todo auto-generated catch Blocke.printstacktrace ();} return null;}}
Here we provide different methods of production, in our Java program, there are many classes of implementation we do not need to care about the details, only need to provide the method to create their own (the last to use the configuration file read)

The configuration file is as follows Type.properties:

Left=com.sunny.project.lefthairright=com.sunny.project.righthairin=com.sunny.project.inhair
In this way, our hairstyle is produced by this factory, we just need to focus on what we want to do, this is the test class:

Package Com.sunny.project;public class Sunnytest {public static void main (string[] args) {//hairinterface left = new Leftha IR ();//left.draw (); Hairfactory factory = new Hairfactory (); Hairinterface right =  Factory.gethair ("right"); Right.draw (); Hairinterface left = Factory.gethairbyclass ("Com.sunny.project.LeftHair"); Left.draw (); Hairinterface hair = Factory.gethairbyclasskey ("left"); Hair.draw ();}}
This is the simple factory model.

What about the abstract factory model?

In fact, the difference between the abstract factory model and the simple factory model is that the abstract factory can produce multiple product families, our simple factory is a factory class, our abstract factory is an interface, the benefit of abstract factory is that we can allow our programs to achieve more product families in different extensions, in order to achieve better expansion.


So under what circumstances do we need to apply our factory model?

1. A system should not be created with different products when the details of the composition and several dependencies, the system should and the creation of these instances separate let us focus on the business.


2. This system has at least one product series


3. The series of the same product is set to be used together


4. Different products appear in a series of interfaces, so that the system does not depend on the details of the implementation of the interface.


In our frequently used Java framework, our JDBC is using the factory model, JDBC only provides a factory for production data manipulation objects, different databases only need to implement the JDBC Unified data interface operation, Our clients use only the factory provided by JDBC to obtain an instance of the operational database for uniform and flexible use.

Similarly, our spring beanfactory is the same, all JavaBean are produced through a factory, we just need to use a Get plus his key name to get an example, very easy to use.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Design pattern Learning--Factory mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.