Design pattern--Factory mode and abstract factory pattern

Source: Internet
Author: User


The original English version of the Factory mode is: Define an interface for creating a object, but let subclasses decide class to which. Factory method lets a class defer instantiation to subclass. It means defining an interface for creating objects, letting subclasses decide which class to instantiate, and the factory method delays the instantiation of a class to subclasses.

The factory model mainly provides interfaces for creating objects, and the factory pattern can be divided into three categories:

Simple Factory pattern: Also known as the static method pattern, the simple factory pattern is the factory class that determines which class object to instantiate based on the parameters passed in.


Factory method Mode: The factory method pattern is a substitute for a new object, so it can be used wherever the object needs to be generated. It has good encapsulation, code structure is clear, if a caller needs a specific product object, as long as you know the product's class name, you do not need to know how the product creation process, reducing the coupling between the modules. Excellent scalability, in the case of increasing the product class, as long as the appropriate changes to the specific factory class can adapt to change. Shielding product class, the product class is how to achieve, the caller does not care, just focus on the product interface.


Abstract Factory:

Package com.zz.factory;

/**
 * Abstract Factory
 * @author TXXS
 * */public
interface Creator {
	/**
	 * Factory Method
	 * Create a Product object, The input parameter type can be set on its own
	 *
	/Public <t extends Product> T factory (class<t> c);



Abstract Products:

Package com.zz.factory;

/**
 * Abstract Products
 * @author txxs
 *
 *
	/public interface Product {//The Common method interface provided by the products class is publicly
	void Method1 ();
	public void Method2 ();

}
Two specific implementations:

Package com.zz.factory;
/** *
 Specific factory
 * @author TXXS * */Public
class Concretecreator implements Creator {

	@ Override public
	<t extends product> T factory (class<t> c) {
		product product = NULL;
		try{
			Product = (product) class.forname (C.getname ()). newinstance ();
		catch (Exception e) {
			
		}
		//TODO auto-generated Method Stub return
		(T) product;
	}



Package com.zz.factory;

public class Concreteproduct implements Product {

	@Override public
	void Method1 () {
		//Business logic processing code

	}< c34/> @Override public
	void Method2 () {
		//Business logic processing code

	}

}


Abstract Factory pattern: its English definition is: Provide an interface for creating families of ralated or dependent objects without specifying their TE classes. The meaning is to provide an interface for creating a set of interrelated or interdependent objects without their specific implementation class. Abstract Factory mode is an upgraded version of the factory method for a series of products, and if there is only one product in the abstract factory approach, the abstract factory pattern is degraded to the factory method pattern.


Abstract Factory:

Package com.zz.abfactory;
/**
 * Abstract Factory
 * @author TXXS
 *
 *
/public interface Abstractfactory {
	//Create product a public
	producta Factorya ();
	Create product B public
	PRODUCTB factoryb ();

}
Test class:

Package com.zz.abfactory;
/**
 * Test class
 * @author txxs
 * * *
/public class Abstractfactorytest {public

	static void main (String [] args) {
		
		//define two factory
		abstractfactory abstractFactory1 = new ConcreteFactory1 ();
		Abstractfactory AbstractFactory2 = new ConcreteFactory2 ();
		
		Production level of 1 products a
		producta productA1 = Abstractfactory1.factorya ();
		Production level of 2 products a
		producta productA2 = Abstractfactory2.factorya ();
		
		Production Grade 1 of the product b
		PRODUCTB productB1 = Abstractfactory1.factoryb ();
		Production Grade 2 of the product B
		PRODUCTB productB2 = Abstractfactory2.factoryb ();
		
		Business processing
		producta1.method1 ();
		Producta2.method1 ();
		Productb1.method1 ();
		Productb2.method1 ();
	}



Haha, is learning, source download

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.