Design Patterns (iii) AbstractFactory patterns-creation Models

Source: Internet
Author: User

Design Patterns (iii) AbstractFactory patterns-creation Models

Abstract Factory mode is the most abstract and general factory mode in all forms. Abstract Factory mode refers to a factory mode used when multiple abstract roles exist. Abstract Factory mode provides an interface to the client to create product objects in multiple product families without specifying the specific product. According to the LSP principle, any place that accepts the parent type should be able to accept the child type. Therefore, what the system actually needs is only some instances with the same type as those of abstract products, rather than the instances of these abstract products. In other words, it is an example of the concrete sub-classes of these abstract products. The factory class is responsible for creating instances of concrete subclasses of abstract products.

 

Differences between Abstract Factory mode and factory method mode

Abstract Factory mode is an upgraded version of the factory method mode. It creates a group of related or mutually dependent objects. The difference between the factory method model and the factory method model is that the factory method model is for a product level structure, while the abstract factory model is for multiple product level structures. In programming, a product structure is usually represented as an interface or abstract class. That is to say, all products provided by the factory method mode are derived from the same interface or abstract class, the abstract factory model provides products derived from different interfaces or abstract classes.

In the abstract factory model, there isProduct FamilyConcept: the so-called product family refersA family of products with functions associated with different product levels. Abstract A series of products provided by the factory model constitute a product family, and a series of products provided by the factory method are called a hierarchical structure. We still use the example of producing cars to illustrate the differences between them.

 

Abstract implementation principle of the factory Model

Implementation:

Add a semantic function to the factory Method

Translate. java

 

Package com. devin. simplefactory;/*** @ author E-mail: csu.ldw@csu.edu.cn * @ version Creation Time: april 24, 2015 12:38:57 * class description */public interface Translate {public String sayTxt (String text );}

 

ChineseTranslate. java

 

Package com. devin. simplefactory;/*** @ author E-mail: csu.ldw@csu.edu.cn * @ version Creation Time: april 25, 2015 12:28:45 * class description */public class ChineseTranslate implements Translate {@ Overridepublic String sayTxt (String txt) {return Hello + txt ;}}

 

EnglishTranslate. java

Package com. devin. simplefactory;/*** @ author E-mail: csu.ldw@csu.edu.cn * @ version Creation Time: april 24, 2015 1:18:24 * class description */public class EnglishTranslate implements Translate {@ Overridepublic String sayTxt (String txt) {return hello + txt ;}}

Interpret. java

Package com. devin. abstractfactory;/*** @ author E-mail: * @ version Creation Time: 1:31:45, January 1, April 24, 2015 * class description */public interface Interpret {public String doInterpret (String text );}

ChineseInterpret. java

Package com. devin. abstractfactory;/*** @ author E-mail: * @ version Creation Time: april 24, 2015 1:18:24 * class description */public class ChineseInterpret implements Interpret {@ Overridepublic String doInterpret (String txt) {return Chinese Semantics + txt ;}}

EnglishInterpret. java

Package com. devin. abstractfactory;/*** @ author E-mail: * @ version Creation Time: april 24, 2015 1:18:24 * class description */public class EnglishInterpret implements Interpret {@ Overridepublic String doInterpret (String txt) {return English interpret: + txt ;}}

AbstractFactoryMethod. java

 

Package com. devin. abstractfactory;/*** @ author E-mail: * @ version Creation Time: April 24, 2015 1:35:34 * class description */public abstract class AbstractFactoryMethod {protected abstract Translate factoryMethodTranslate (); protected abstract Interpret factoryMethodInterpret (); public String sayTxt (String txt) {Translate translate = factoryMethodTranslate (); Interpret interpret = factoryMethodInterpret (); return translate. sayTxt (txt) + --- + interpret. doInterpret (txt );}}

 

ChineseFactoryMethod. java

Package com. devin. abstractfactory;/*** @ author E-mail: * @ version Creation Time: 1:27:45, January 1, April 24, 2015 * class description */public class ChineseFactoryMethod extends AbstractFactoryMethod {@ Overrideprotected Translate factoryMethodTranslate () {return new ChineseTranslate () ;}@ Overrideprotected Interpret factoryMethodInterpret () {// TODO Auto-generated method stubreturn new ChineseInterpret ();}}

EnglishFactoryMethod. java

Package com. devin. abstractfactory;/*** @ author E-mail: * @ version Creation Time: 1:27:45, January 1, April 24, 2015 * class description */public class EnglishFactoryMethod extends AbstractFactoryMethod {@ Overrideprotected Translate factoryMethodTranslate () {return new EnglishTranslate () ;}@ Overrideprotected Interpret factoryMethodInterpret () {return new EnglishInterpret ();}}

Client. java

Package com. devin. abstractfactory;/*** @ author E-mail: * @ version Creation Time: april 24, 2015 1:28:57 * class description */public class Client {public static void main (String [] args) {AbstractFactoryMethod factoryMethod = new ChineseFactoryMethod (); System. out. println (factoryMethod. sayTxt (ddddfdsafs ));}}


Test results:

Helloddddfdsafs --- Chinese Semantics ddddddfdsaf previous http://www.bkjia.com/kf/201504/395058.html

Related Article

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.