Introduction of three factory models

Simple factory pattern

definition

Simple Factory Pattern (Simple Factory Pattern) refers to a factory object to determine which type of product instance is created

It belongs to the creation mode, but it does not belong to the GOF23 design modes

Applicable scene

The factory class is responsible for creating fewer objects. 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 the object.

advantage

You only need to pass in a correct parameter to get the object you need without knowing the details of its creation

Disadvantage

  • The responsibilities of the factory category are relatively heavy, and the judgment logic of the factory category needs to be modified when adding new products, which violates the principle of opening and closing

  • Not easy to expand overly complex product structure

Class Diagram

Insert picture description here

Related code

ICourse.java

package simpleFactory;

/**
 * Created with IntelliJ IDEA.
 * User: 轩辕龙儿
 * Date: 2021/3/14
 * Time: 23:26
 * Description: 课程接口类
 */
public interface ICourse {
    
    
    /**
     * 录制视频
     */
    void record();
}

JavaCourse.java

package simpleFactory;

/**
 * Created with IntelliJ IDEA.
 * User: 轩辕龙儿
 * Date: 2021/3/14
 * Time: 23:27
 * Description: No Description
 */
public class JavaCourse implements ICourse {
    
    
    public void record() {
    
    
        System.out.println("录制Java课程");
    }
}

PythonCourse.java

package simpleFactory;

/**
 * Created with IntelliJ IDEA.
 * User: 轩辕龙儿
 * Date: 2021/3/14
 * Time: 23:29
 * Description: No Description
 */
public class PythonCourse implements ICourse{
    
    
    public void record() {
    
    
        System.out.println("录制Python课程");
    }
}

CourseFactory.java

package simpleFactory;

/**
 * Created with IntelliJ IDEA.
 * User: 轩辕龙儿
 * Date: 2021/3/14
 * Time: 23:31
 * Description: No Description
 */
public class CourseFactory {
    
    
    public ICourse create(Class<? extends ICourse> clazz) {
    
    
        try {
    
    
            if (null != clazz) {
    
    
                return clazz.newInstance();
            }
        } catch (IllegalAccessException e) {
    
    
            e.printStackTrace();
        } catch (InstantiationException e) {
    
    
            e.printStackTrace();
        }
        return null;
    }
}

SimpleFactoryMain.java

package simpleFactory;

/**
 * Created with IntelliJ IDEA.
 * User: 轩辕龙儿
 * Date: 2021/3/14
 * Time: 23:30
 * Description: No Description
 */
public class SimpleFactoryMain {
    
    
    public static void main(String[] args) {
    
    
        CourseFactory factory = new CourseFactory();
        ICourse javaCourse = factory.create(JavaCourse.class);
        javaCourse.record();
        ICourse pythonCourse = factory.create(PythonCourse.class);
        pythonCourse.record();
    }
}

Factory method pattern

definition

Factory method pattern (Factory Method Pattern) refers to the definition of an interface to create objects, but the class that implements this interface determines which class to instantiate, and the factory method delays the instantiation of the class to subclasses.

Belongs to the creative design model

Applicable scene

  • Creating objects requires a lot of repetitive code
  • The client (application layer) does not depend on the details of how the product class instance is created and implemented
  • A class specifies which object to create through its subclasses

advantage

  • The user only needs to care about the factory corresponding to the required product, and does not need to care about the creation details

  • The addition of new products does not comply with the principle of opening and closing, which improves the scalability of the system

Disadvantage

  • The number of classes is easy to be too much, which increases the complexity of the code structure
  • Increased the abstraction and difficulty of understanding the system

Class Diagram

Insert picture description hereInsert picture description here

Related code

IPay.java

package task;

public interface IPay {
    
    
    void pay();
}

IIPayOut.java

package task;

public interface IIPayOut extends IPay {
    
    
}

IIPayIn.java

package task;

public interface IIPayIn extends IPay {
    
    
}

OtherIPay.java

package task;

public class OtherIPay implements IIPayOut {
    
    

    public void pay() {
    
    
        System.out.println("其他国外支付");
    }
}

AppleIPay.java

package task;

public class AppleIPay implements IIPayOut {
    
    
    public void pay() {
    
    
        System.out.println("苹果支付");
    }
}

WeIPay.java

package task;

public class WeIPay implements IIPayIn {
    
    
    public void pay() {
    
    
        System.out.println("微信支付");
    }
}

UniIPay.java

package task;

public class UniIPay implements IIPayIn {
    
    
    public void pay() {
    
    
        System.out.println("银联支付");
    }
}

AliIPay.java

package task;

public class AliIPay implements IIPayIn {
    
    
    public void pay() {
    
    
        System.out.println("支付宝支付!");
    }
}

IPayFactory.java

package task;

public interface IPayFactory {
    
    
    IPay create();
}

IPayOutFactory.java

package task;

public interface IPayOutFactory extends IPayFactory{
    
    
}

IPayInFactory.java

package task;

public interface IPayInFactory extends IPayFactory{
    
    
}

OtherPayFactory.java

package task;

public class OtherPayFactory implements IPayOutFactory{
    
    
    public IIPayOut create() {
    
    
        return new OtherIPay();
    }
}

ApplePayFactory.java

package task;

public class ApplePayFactory implements IPayOutFactory{
    
    
    public IIPayOut create() {
    
    
        return new AppleIPay();
    }
}

WePayFactory.java

package task;

public class WePayFactory implements IPayInFactory{
    
    
    public IIPayIn create() {
    
    
        return new WeIPay();
    }
}

AliPayFactory.java

package task;

public class AliPayFactory implements IPayInFactory{
    
    
    public IIPayIn create() {
    
    
        return new AliIPay();
    }
}

UniPayFactory.java

package task;

public class UniPayFactory implements IPayInFactory{
    
    
    public IIPayIn create() {
    
    
        return new UniIPay();
    }
}

TaskMain.java

package task;

public class TaskMain {
    
    
    public static void main(String[] args) {
    
    
        IPayInFactory factoryIn = new AliPayFactory();
        IPay payIn = factoryIn.create();
        payIn.pay();

        IPayOutFactory factoryOut = new ApplePayFactory();
        IPay payOut = factoryOut.create();
        payOut.pay();
    }
}

Abstract factory pattern

definition

The abstract factory pattern (Abastract Factory Pattern) is to provide an interface to create a series of related or interdependent objects without specifying their specific classes

Created design pattern

Applicable scene

  • The client (application layer) does not depend on the details of how the product class instance is created and implemented
  • Emphasize that a series of related product objects (belonging to the same product family) are used together to create objects that require a lot of repetitive code
  • Provide a product class library, all products appear with the same interface, so that the client does not depend on the specific implementation

advantage

  • Specific products are isolated in the application layer code, no need to care about the creation details
  • Unify a series of product families together to create

Disadvantage

  • It specifies all the product sets that may be created. It is difficult to extend new products in the product family, and it is necessary to modify the interface of the abstract factory
  • Increased the abstraction and difficulty of understanding the system

Class Diagram

Insert picture description here
Insert picture description here

Related code

IVideo.java

package abstractFactory;

/**
 * Created with IntelliJ IDEA.
 * User: 轩辕龙儿
 * Date: 2021/3/17
 * Time: 14:13
 * Description: No Description
 */
public interface IVideo {
    
    
    void record();
}

JavaVideo.java

package abstractFactory;

/**
 * Created with IntelliJ IDEA.
 * User: 轩辕龙儿
 * Date: 2021/3/17
 * Time: 14:27
 * Description: No Description
 */
public class JavaVideo implements IVideo{
    
    
    @Override
    public void record() {
    
    
        System.out.println("录制Java视频");
    }
}

PythonVideo.java

package abstractFactory;

/**
 * Created with IntelliJ IDEA.
 * User: 轩辕龙儿
 * Date: 2021/3/17
 * Time: 14:28
 * Description: No Description
 */
public class PythonVideo implements IVideo{
    
    
    @Override
    public void record() {
    
    
        System.out.println("录制python视频");
    }
}

INote.java

package abstractFactory;

/**
 * Created with IntelliJ IDEA.
 * User: 轩辕龙儿
 * Date: 2021/3/17
 * Time: 14:12
 * Description: No Description
 */
public interface INote {
    
    
    void edit();
}

JavaNote.java

package abstractFactory;

/**
 * Created with IntelliJ IDEA.
 * User: 轩辕龙儿
 * Date: 2021/3/17
 * Time: 14:14
 * Description: No Description
 */
public class JavaNote implements INote {
    
    

    @Override
    public void edit() {
    
    
        System.out.println("记录Java笔记");
    }
}

PythonNote.java

package abstractFactory;

/**
 * Created with IntelliJ IDEA.
 * User: 轩辕龙儿
 * Date: 2021/3/17
 * Time: 14:26
 * Description: No Description
 */
public class PythonNote implements INote {
    
    

    @Override
    public void edit() {
    
    
        System.out.println("记录python笔记");
    }
}

CourseFactory.java

package abstractFactory;

/**
 * Created with IntelliJ IDEA.
 * User: 轩辕龙儿
 * Date: 2021/3/17
 * Time: 14:29
 * Description: No Description
 */
public abstract class CourseFactory {
    
    

    public void init(){
    
    
        System.out.println("初始化");
    }

    protected abstract INote createNote();

    protected abstract IVideo createVideo();
}

JavaCourseFactory.java

package abstractFactory;

/**
 * Created with IntelliJ IDEA.
 * User: 轩辕龙儿
 * Date: 2021/3/17
 * Time: 15:21
 * Description: No Description
 */
public class JavaCourseFactory extends CourseFactory {
    
    

    public INote createNote() {
    
    
        super.init();
        return new JavaNote();
    }

    public IVideo createVideo() {
    
    
        super.init();
        return new JavaVideo();
    }
}

PythonCourseFactory.java

package abstractFactory;

/**
 * Created with IntelliJ IDEA.
 * User: 轩辕龙儿
 * Date: 2021/3/17
 * Time: 15:21
 * Description: No Description
 */
public class PythonCourseFactory extends CourseFactory {
    
    

    public INote createNote() {
    
    
        super.init();
        return new PythonNote();
    }


    public IVideo createVideo() {
    
    
        super.init();
        return new PythonVideo();
    }
}

AbstractFactoryMain.java

package abstractFactory;

/**
 * Created with IntelliJ IDEA.
 * User: 轩辕龙儿
 * Date: 2021/3/17
 * Time: 15:22
 * Description: No Description
 */
public class AbstractFactoryMain {
    
    
    public static void main(String[] args) {
    
    

        JavaCourseFactory factory = new JavaCourseFactory();
        factory.createNote().edit();
        factory.createVideo().record();

    }
}

Guess you like

Origin blog.csdn.net/huangge1199/article/details/114933036