Design Patterns-factory patterns and abstract factory patterns

Source: Internet
Author: User

Design Patterns-factory patterns and abstract factory patterns


Factory mode: Define an interface for creating an object, but let subclasses decide which class to instantiate. factory method lets a class defer instantiation to subclass. defines an interface used to create objects so that the subclass decides which class to instantiate. The factory method delays the instantiation of a class to the subclass.

 

The factory mode provides interfaces for object creation. The factory mode can be divided into three types:

Simple factory mode: it is also called static method mode. The simple factory mode is used to determine the class objects to be instantiated based on input parameters.

Factory method mode: The Factory method mode is a substitute for a new object, so it can be used wherever an object needs to be generated. It has good encapsulation and clear code structure. If a caller needs a specific product object, he or she only needs to know the Class Name of the product and does not need to know how the product is created, this reduces the coupling between modules. Excellent scalability. When adding product classes, you only need to modify the specific factory classes to adapt to the changes. The product class is blocked and how the product class is implemented does not need to be concerned by the caller. You only need to pay attention to the product interface.

Abstract Factory:

 

Package com. zz. factory;/*** Abstract factory ** @ author txxs **/public interface Creator {/*** factory Method * to create a product object, you can set the parameter type */public
 
  
T factory (Class
  
   
C );}
  
 

Abstract product:

 

Package com. zz. factory;/*** abstract Product * @ author txxs **/public interface Product {// public method interface public void method1 () provided by the Product class; public void method2 ();}
Two specific implementations:

 

 

Package com. zz. factory;/*** specific factory ** @ author txxs **/public class ConcreteCreator implements Creator {@ Overridepublic
 
  
T factory (Class
  
   
C) {Product product = null; try {product = (Product) Class. forName (c. getName ()). newInstance ();} catch (Exception e) {}// TODO Auto-generated method stubreturn (T) product ;}}
  
 

Package com. zz. factory; public class ConcreteProduct implements Product {@ Overridepublic void method1 () {// business logic processing code} @ Overridepublic void method2 () {// business logic processing code }}

 

 

Abstract Factory mode: its English definition is: Provide an interface for creating families of ralated or dependent objects without specifying their concrete classes. it means to provide interfaces for creating a group of related or mutually dependent objects without their specific implementation classes. The abstract factory mode is an upgraded version of the factory method. It targets a series of products. If there is only one product in the abstract factory method, the abstract factory mode degrades to the factory method mode.

Abstract Factory:

Package com. zz. abfactory;/*** Abstract Factory ** @ author txxs **/public interface AbstractFactory {// create Apublic ProductA factoryA (); // create product Bpublic ProductB factoryB ();}
Test class:

 

 

Package com. zz. abfactory;/*** test class * @ author txxs **/public class AbstractFactoryTest {public static void main (String [] args) {// define two factories: AbstractFactory abstractFactory1 = new ConcreteFactory1 (); AbstractFactory abstractFactory2 = new ConcreteFactory2 (); // AProductA productA1 = snapshot () for products with a production grade of 1 (); // AProductA productA2 = abstractFactory2.factoryA (); // product BProductB productB1 = abstractFactory1.factoryB () with the production level of 2 (); // product BProductB productB2 = abstractFactory2.factoryB (); // Service Processing productA1.method1 (); productA2.method1 (); productB1.method1 (); productB2.method1 ();}}

 

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.