背景
工厂模式属于创建型设计模式,算是比较简单的设计模式,但是也分为几种,下面直接上代码,几种写法是有易到难,业务越复杂,要用到的设计模式代码也更复杂,仅供参考,如有说的不对的地方,请大家及时指出。
简单工厂模式
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();
}
}
简单工厂方法模式
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();
}
}
抽象工厂模式
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();
}
}