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

Detailed introduction to factory pattern in java

王林
Release: 2019-11-25 17:45:25
forward
2571 people have browsed it

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:

实例:面条工厂 实现工厂模式
···
Copy after login

The following shows some noodle code pieces

package am2;

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

}
Copy after login

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 ;
	}

}
Copy after login

The following shows some lasagna code pieces

package am2;

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

}
Copy after login

The following shows some instantiation code pieces

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

The following shows some spaghetti code pieces

package am2;

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

}
Copy after login

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();

	}

}
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template