Factory pattern of java design pattern (4)

background

The factory pattern belongs to the creational design pattern. It is a relatively simple design pattern, but it is also divided into several types. The following codes are directly written. Several writing methods range from easy to difficult. The more complex the business, the more complex the design pattern code to be used. , for reference only, if there is something wrong, please point it out in time.

Simple Factory Pattern

insert image description here

package com.example.factory.simple;

/**
 * 电脑接口类
 */
public interface ICompute {
    
    
    //生产电脑的方法
    void producer();
}

package com.example.factory.simple;

public class LenovoCompute  implements  ICompute{
    
    
    @Override
    public void producer() {
    
    
        System.out.println("开始联想电脑生产。。");
    }
}

package com.example.factory.simple;

/**
 * 华为电脑生产
 */
public class HuaweiCompute implements ICompute{
    
    
    @Override
    public void producer() {
    
    
        System.out.println("开始华为电脑生产。。。。");
    }
}

package com.example.factory.simple;

/**
 * 小作坊工厂,比如一个小的修电脑的店
 */
public class ComputeFactory {
    
    

    public static ICompute create(Class<? extends ICompute> clazz){
    
    
        if(clazz!=null){
    
    
            try {
    
    
                return clazz.newInstance();
            } catch (InstantiationException e) {
    
    
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
    
    
                throw new RuntimeException(e);
            }
        }
        return null;
    }
}

package com.example.factory.simple;

public class SimpleFactoryPatternTest {
    
    
    public static void main(String[] args) {
    
    
        //传class可以避免参数错误,以及只能传ICompute的实现类,单一原则
        ICompute iCompute = ComputeFactory.create(LenovoCompute.class);
        iCompute.producer();
    }
}

Simple Factory Method Pattern

insert image description here

package com.example.factory.method;

/**
 * 华为电脑生产
 */
public class HuaweiCompute implements ICompute {
    
    
    @Override
    public void producer() {
    
    
        System.out.println("开始华为电脑生产。。。。");
    }
}

package com.example.factory.method;




public class HuaweiComputeFactory implements IComputeFactory{
    
    

    @Override
    public ICompute create() {
    
    
        return new HuaweiCompute();
    }
}

package com.example.factory.method;

/**
 * 电脑接口类
 */
public interface ICompute {
    
    
    //生产电脑的方法
    void producer();
}

package com.example.factory.method;

public interface IComputeFactory {
    
    
    ICompute create();
}

package com.example.factory.method;

public class LenovoCompute  implements ICompute {
    
    
    @Override
    public void producer() {
    
    
        System.out.println("开始联想电脑生产。。");
    }
}

package com.example.factory.method;




public class LenovoComputeFactory implements IComputeFactory{
    
    
    
    @Override
    public ICompute create() {
    
    
        return new LenovoCompute();
    }
}

package com.example.factory.method;



//工厂方法模式测试类
public class MethodPatternTest {
    
    

    public static void main(String[] args) {
    
    
        IComputeFactory iComputeFactory=new HuaweiComputeFactory();
        ICompute iCompute= iComputeFactory.create();
        iCompute.producer();
    }
}

abstract factory pattern

insert image description here

package com.example.factory.abstracts;

public abstract class ComputeFactoryAbstract {
    
    
    private String brand; //品牌
    private String qualityLevel; //质量
    public void init(){
    
    
        System.out.println("初始化");
    }

    public abstract IKeyboard keyboard(); //生产键盘

    public abstract IWindow window(); //生产屏幕
}

package com.example.factory.abstracts;


//生产键盘
public interface IKeyboard {
    
    

    void producer();
}

package com.example.factory.abstracts;


//生产屏幕
public interface IWindow {
    
    

    void producer();
}

package com.example.factory.abstracts;


public class LenovoComputeFactory  extends ComputeFactoryAbstract {
    
    


    @Override
    public IKeyboard keyboard() {
    
    
        return new LenovoKeyboard();
    }

    @Override
    public IWindow window() {
    
    
        return new HuaweiWindow();
    }
}

package com.example.factory.abstracts;

public class LenovoKeyboard  implements IKeyboard {
    
    
    @Override
    public void producer() {
    
    
        System.out.println("开始联想电脑生产键盘。。");
    }
}

package com.example.factory.abstracts;

public class LenovoWindow implements IWindow {
    
    
    @Override
    public void producer() {
    
    
        System.out.println("开始联想电脑生产屏幕。。");
    }
}

package com.example.factory.abstracts;


public class HuaweiComputeFactory extends ComputeFactoryAbstract {
    
    


    @Override
    public IKeyboard keyboard() {
    
    
        return new HuaweiKeyboard();
    }

    @Override
    public IWindow window() {
    
    
        return new HuaweiWindow();
    }
}

package com.example.factory.abstracts;

/**
 * 华为电脑生产
 */
public class HuaweiKeyboard implements IKeyboard {
    
    
    @Override
    public void producer() {
    
    
        System.out.println("开始华为电脑生产键盘。。。。");
    }


}

package com.example.factory.abstracts;

/**
 * 华为电脑生产
 */
public class HuaweiWindow implements IWindow {
    
    
    @Override
    public void producer() {
    
    
        System.out.println("开始华为电脑生产屏幕。。。。");
    }


}

package com.example.factory.abstracts;

public class AbstractFactoryPatternTest {
    
    
    public static void main(String[] args) {
    
    
        HuaweiComputeFactory huaweiComputeFactory = new HuaweiComputeFactory();
        huaweiComputeFactory.keyboard().producer();
        huaweiComputeFactory.window().producer();
    }
}

Guess you like

Origin blog.csdn.net/qq_40351360/article/details/128450527