工厂模式
发布日期:2021-05-08 09:46:38 浏览次数:0 分类:技术文章

工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。
详情请参考—

新建一个Phone接口

public interface Phone {
   		String getName();	}

新建手机实体类

public class Huawei implements Phone {
   		public String getName() {
   		return "huawei";	}	}
public class Mi implements Phone {
   	public String getName() {
   		return "mi";	}}
public class Oppo implements Phone {
   	public String getName() {
   		return "oppo";	}}
public class Vivo implements Phone {
   	public String getName() {
   		return "vivo";	}	}

简单工厂实现

public class SimpleFactory {
   		public Phone getPhone(String name) {
   		if("huawei".equalsIgnoreCase(name)) {
   			return new Huawei();		}else if("mi".equalsIgnoreCase(name)) {
   			return new Mi();		}else if("oppo".equalsIgnoreCase(name)) {
   			return new Oppo();		}else if("vivo".equalsIgnoreCase(name)) {
   			return new Vivo();		}else {
   			throw new RuntimeException("没有该手机");		}	}}

调用测试

public class SimpleFactoryTest {
   	public static void main(String[] args) {
   		Phone huawei = new SimpleFactory().getPhone("huawei");		Phone mi = new SimpleFactory().getPhone("mi");		Phone oppo = new SimpleFactory().getPhone("oppo");		Phone vivo = new SimpleFactory().getPhone("vivo");		System.out.println(huawei.getName());		System.out.println(mi.getName());		System.out.println(oppo.getName());		System.out.println(vivo.getName());	}	}

在这里插入图片描述

工厂方法实现

新建Factory 接口

public interface Factory {
   		Phone getPhone();	}

创建各手机厂商工厂

public class HuaweiFactory implements Factory {
   	public Phone getPhone() {
   		return new Huawei();	}}
public class MiFactory implements Factory {
   	public Phone getPhone() {
   		return new Mi();	}}
public class OppoFactory implements Factory {
   	public Phone getPhone() {
   		return new Oppo();	}}
public class VivoFactory implements Factory {
   	public Phone getPhone() {
   		return new Vivo();	}}

工厂方法调用测试

public class FactoryTest {
   		public static void main(String[] args) {
   		Factory huaweiFactory = new HuaweiFactory();		System.out.println(huaweiFactory.getPhone().getName());				Factory miFactory = new MiFactory();		System.out.println(miFactory.getPhone().getName());				Factory oppoFactory = new OppoFactory();		System.out.println(oppoFactory.getPhone().getName());				Factory vivoFactory = new VivoFactory();		System.out.println(vivoFactory.getPhone().getName());	}}

在这里插入图片描述

抽象工厂实现

抽象工厂父类

public abstract class AbstractFactory {
   	protected abstract Phone getPhone();		public Phone getPhone(String name) {
   		if("huawei".equalsIgnoreCase(name)) {
   			return new Huawei();		}else if("mi".equalsIgnoreCase(name)) {
   			return new Mi();		}else if("oppo".equalsIgnoreCase(name)) {
   			return new Oppo();		}else if("vivo".equalsIgnoreCase(name)) {
   			return new Vivo();		}else {
   			throw new RuntimeException("没有该手机");		}	}	}

各手机厂商工厂

public class HuaweiFactory extends AbstractFactory {
   	@Override	public Phone getPhone() {
   		return new Huawei();	}}
public class MiFactory extends AbstractFactory {
   	@Override	public Phone getPhone() {
   		return new Mi();	}}
public class OppoFactory extends AbstractFactory {
   	@Override	public Phone getPhone() {
   		return new Oppo();	}}
public class VivoFactory extends AbstractFactory {
   	@Override	public Phone getPhone() {
   		return new Vivo();	}}

默认工厂

public class DefaultFactory extends AbstractFactory {
   	HuaweiFactory defaultFactory = new HuaweiFactory();		@Override	protected Phone getPhone() {
   		return defaultFactory.getPhone();	}}

抽象工厂调用

public class AbstractFactoryTest {
   		public static void main(String[] args) {
   		DefaultFactory defaultFactory = new DefaultFactory();		System.out.println(defaultFactory.getPhone().getName());		System.out.println(defaultFactory.getPhone("mi").getName());	}}

结果打印
在这里插入图片描述
总结工厂模式特点:
隐藏复杂的逻辑处理过程,只关心执行结果

上一篇:SpingBoot跨域问题
下一篇:线程安全之Synchronized