Factory method mode and abstract factory mode in Design Mode

Source: Internet
Author: User
Tags print print

In the previous blog on design patterns: http://www.cnblogs.com/shenliang123/archive/2012/05/10/2494412.html has already said the problem: that is, how to make the factory class better handle the returned results of different instances is to use another way of thinking to replace the logic judgment in a factory class to return different object instances;

The solution is to introduce the factory method mode and abstract factory mode.

The design of this design mode is to provide multiple factory classes and how many implementation classes are provided for the factory classes. According to the previous ideas, we need to write interfaces here;

Or the last print instance:

First, there are two implementation classes in the previous example: excelimpl and wordimpl. Therefore, we need to provide two factory classes for the design mode of the installation factory method;

According to interface-Oriented Programming: we need to first provide an interface for all factory classes:

Printfactoryinterface:

package xidian.sl.interfaces;public interface PrintFactoryInterface {    public Print getPrint();}

Then there are two implementation classes: excelfactory

package xidian.sl.impl;import xidian.sl.interfaces.Print;import xidian.sl.interfaces.PrintFactoryInterface;public class ExcelFactory implements PrintFactoryInterface{    @Override    public Print getPrint() {        // TODO Auto-generated method stub        return new ExcelImpl();    }}

Wordfactory:

package xidian.sl.impl;import xidian.sl.interfaces.Print;import xidian.sl.interfaces.PrintFactoryInterface;public class WordFactory implements PrintFactoryInterface {    @Override    public Print getPrint() {        // TODO Auto-generated method stub        return new WordImpl();    }}

Caller code:

Package xidian. SL. impl; import xidian. SL. interfaces. print; import xidian. SL. interfaces. printfactory; import xidian. SL. interfaces. printfactoryinterface; public class dataoutput {private print Print; Public dataoutput (Print print) {This. print = print;}/*** simulate export. Here, you need to call methods in other objects to implement **/Public void output () {print. outprint ();} public static void main (string [] ARGs) {/*** instantiate factory class ** // printfactory = new printfactory (); printfactoryinterface printfactory = new excelfactory ();/*** instantiate the called class and initialize the dataoutput object through the constructor **/dataoutput = new dataoutput (printfactory. getprint (); dataoutput. output ();}}

The difference between the above Code and the simple factory mode is that the caller needs to change the printfactoryinterface printfactory = new excelfactory () to instantiate the class.
The same factory class ensures that each factory class is only responsible for one instance class, but another problem arises: coupling between the factory class and the caller;

Abstract Factory mode is used to solve the coupling problem between factory classes and callers: first, let's look at the corresponding abstract factory class code:

Package xidian. SL. interfaces; import xidian. SL. impl. excelfactory; import xidian. SL. impl. wordfactory; public class printfactoryfactory {/*** only one method is defined below to return the corresponding factory class **/public static printfactoryinterface getfactory (string type) {// The case-insensitive if ("better ". repeated signorecase (type) {return New excelfactory () ;}else {return New wordfactory ();}}}

You may find that the abstract factory class is similar to the factory class mentioned in the previous section, but the returned objects are different. The abstract factory class is used to manage the factory class, the returned result is a factory instance, and the factory class manages the called instance.
Also called instances;

The call code is changed:

Package xidian. SL. impl; import xidian. SL. interfaces. print; import xidian. SL. interfaces. printfactory; import xidian. SL. interfaces. printfactoryfactory; import xidian. SL. interfaces. printfactoryinterface; public class dataoutput {private print Print; Public dataoutput (Print print) {This. print = print;}/*** simulate export. Here, you need to call methods in other objects to implement **/Public void output () {print. outprint ();} public static void main (string [] ARGs) {/*** instantiate factory class ** // printfactory = new printfactory (); // printfactoryinterface printfactory = new excelfactory (); printfactoryinterface printfactory = printfactoryfactory. getfactory ("better");/*** instantiate the called class and initialize the dataoutput object through the constructor **/dataoutput = new dataoutput (printfactory. getprint (); dataoutput. output ();}}

The factory mode is resolved here.

 

 

 

 

 

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.