concept
Factory Pattern (Factory Pattern) is one of the most commonly used design patterns in Java. This type of design pattern is a creational pattern, which provides the best way to create objects.
In the factory mode, we do not expose the creation logic to the client when creating an object, and we use a common interface to point to the newly created object.
Simple factory pattern
The Simple Factory Pattern refers to an instance class in which a factory object determines which product is created, but it does not belong to GOF23 design patterns.
1. Create an interface
public interface ICourse {
public void record();
}
2. Create an interface implementation class
public class JavaCourse implements ICourse{
@Override
public void record() {
System.out.println("Java 语言");
}
}
public class PythonCourse implements ICourse {
@Override
public void record() {
System.out.println("pythod语言");
}
}
3. Create a factory class
public class SimpleFactory {
public ICourse getCourse(String name){
if("java".equals(name)){
return new JavaCourse();
}
if("python".equals(name)){
return new PythonCourse();
}else{
return null;
}
}
}
4. Create a test class
public class FactoryTest {
public static void main(String[] args) {
SimpleFactory simpleFactory = new SimpleFactory();
ICourse course = simpleFactory.getCourse("java");
course.record();
}
}
Operation result:
Simple factory is suitable for scenarios where the factory class is responsible for creating fewer objects, and the client only needs to pass in the parameters of the factory class, and does not need to care about the logic of how to create objects
Disadvantage
- The responsibilities of the factory class are relatively heavy, and it is not easy to extend the overly complex product structure
Factory method pattern
Factory method pattern (Factory Method Pattern) refers to the definition of an interface to create objects, but let the class that implements this interface decide which class to instantiate, and the factory method delays the instantiation of the class to the subclass.
1. Create a factory interface
public interface ICourseFactory {
public ICourse create();
}
2. Create a factory class that implements the interface
public class JavaCourseFactory implements ICourseFactory{
@Override
public ICourse create() {
return new JavaCourse();
}
}
public class PythonCourseFactory implements ICourseFactory{
@Override
public ICourse create() {
return new PythonCourse();
}
}
3. Test class
public class FactoryMethodTest {
public static void main(String[] args) {
ICourseFactory iCourseFactory=new JavaCourseFactory();
ICourse icourse=iCourseFactory.create();
icourse.record();
}
}
Operation result:
In the factory method mode, the user only needs to care about the factory corresponding to the required product, not the creation details
Disadvantage
- The number of classes is easy to be too much, increasing complexity
- Increased the abstraction and difficulty of understanding the system
Abstract factory pattern
Abstract Factory Pattern (Abstract Factory Pattern) is to create other factories around a super factory. This super factory is also called the factory of other factories. This type of design pattern is a creational pattern, which provides the best way to create objects.
In the abstract factory pattern, the interface is responsible for creating a factory for related objects, without explicitly specifying their classes. Each generated factory can provide objects according to the factory pattern.
1. Create a product interface
public interface IAirConditioning {
public void cold();
}
public interface IPhone {
public void call();
}
2. Create a product
public class GreePhone implements IPhone {
@Override
public void call() {
System.out.println("格力电话");
}
}
public class GreeAirConditioning implements IAirConditioning {
@Override
public void cold() {
System.out.println("好空调,格力造");
}
}
public class MideaPhone implements IPhone{
@Override
public void call() {
System.out.println("美的电话");
}
}
public class MideaAirConditioning implements IAirConditioning {
@Override
public void cold() {
System.out.println("美的空调-精彩生活由我创造");
}
}
3. Create an abstract factory
public abstract class AbstractFactory {
public void init(){
System.out.println("开始。。。");
}
protected abstract IPhone createPhone();
protected abstract IAirConditioning createAirConditioning();
}
4. Create a specific factory
public class MideaFactory extends AbstractFactory{
@Override
protected IPhone createPhone() {
return new MideaPhone();
}
@Override
protected IAirConditioning createAirConditioning() {
return new MideaAirConditioning();
}
}
public class GreeFactory extends AbstractFactory {
@Override
protected IPhone createPhone() {
return new GreePhone();
}
@Override
protected IAirConditioning createAirConditioning() {
return new GreeAirConditioning();
}
}
4. Test
public class AbstractFactoryTest {
public static void main(String[] args) {
GreeFactory greeFactory=new GreeFactory();
greeFactory.createAirConditioning().cold();
}
}
operation result:
Disadvantage
- It specifies all the product sets that may be created. If the product family changes, then adjustments will occur from the abstract factory to the specific factory, which does not conform to the principle of opening and closing
- Increased the abstraction and difficulty of understanding the system
The principle of opening and closing: a software entity such as class, module and function should be open for extension and closed for modification.