Home  >  Article  >  Java  >  Detailed introduction to factory pattern in java

Detailed introduction to factory pattern in java

王林
王林forward
2019-11-25 17:45:252522browse

Detailed introduction to factory pattern in java

Factory pattern classification:

1) Simple Factory pattern (Simple Factory)

2) Factory method pattern (Factory Method)

3) Abstract Factory Pattern (Abstract Factory)

Recommended related video tutorials: java learning

Simple Factory Pattern

Simple factory pattern is also called static factory method pattern. It can be seen from the renaming that this mode must be very simple. Its purpose is simple: to define an interface for creating objects.

1) Factory role: This is the core of this model and contains certain business logic and judgment logic. In java it is often implemented by a concrete class.

2) Abstract product role: It is generally the parent class inherited by a specific product or the interface implemented. It is implemented in java by interface or abstract class.

3) Specific product role: The object created by the factory class is an instance of this role. Implemented by a concrete class in java.

Abstract Factory Pattern:

The purpose of the Abstract Factory Pattern is to provide an interface for the client to create product objects in multiple product families

Moreover, the following conditions must be met to use the abstract factory pattern:

1) There are multiple product families in the system, and the system can only consume one family of products at a time.

2) Products belonging to the same product family should be used accordingly.

The various roles of the abstract factory pattern (the same as those of the factory method):

1) Abstract factory role: This is the core of the factory method pattern, which is related to the application Nothing to do. It is the interface that a specific factory role must implement or the parent class that must be inherited. In java it is implemented by abstract classes or interfaces.

Code presentation:

实例:面条工厂 实现工厂模式
···

The following shows some noodle code pieces

package am2;

public class Daoxiaomian extends Miantiao{
	
	public void show(){
		System.out.println("我生产刀削面");
	}

}

The following shows some factory pattern code pieces

package am2;
/**
 * 工厂模式
 * @author hadoop
 *
 */
public class Factory {

	public static final String XIMIANTIAO="ximiantiao";
	public static final String KUANMIANTIAO="kuanmiantiao";
	public static final String DAOXIAOMIAN="daoxiaomian";
	
	public static Miantiao getMiantiao(String name){
		Miantiao miantiao = new Miantiao();
		switch(name){
		case "ximiantiao":
			miantiao =  new Ximiantiao();
			break;
		case "kuanmiantiao":
			miantiao =  new Kuanmiantiao();
			break;
		case "daoxiaomian":
			miantiao =  new Daoxiaomian();
			break;
		}
		return miantiao ;
	}

}

The following shows some lasagna code pieces

package am2;

public class Kuanmiantiao extends Miantiao{
	
	public void show(){
		System.out.println("我生产宽面条");
	}

}

The following shows some instantiation code pieces

package am2;
public class Miantiao {
public  void show(){
}
}

The following shows some spaghetti code pieces

package am2;

public class Ximiantiao extends Miantiao{
	
	public void show(){
		System.out.println("我生产细面条");
	}

}

The following shows some factory mode test code pieces. Get various noodles from the factory Code piece

package am2;
/**
 * 工厂模式,我想从工厂获取到各种面条
 * 
 * @author hadoop
 *
 */
public class Test {

	public static void main(String[] args) {
		Miantiao miantiao = Factory.getMiantiao(Factory.XIMIANTIAO);
		miantiao.show();
		Miantiao miantiao1 = Factory.getMiantiao(Factory.KUANMIANTIAO);
		miantiao1.show();
		Miantiao miantiao2 = Factory.getMiantiao(Factory.DAOXIAOMIAN);
		miantiao2.show();

	}

}

Recommended related articles: Java language introduction

The above is the detailed content of Detailed introduction to factory pattern in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete