目录
简单工厂模式
产品A/B分别实现同一个功能(同一个接口),然后由一个工厂类来实现创建对应产品的方法,客户端通过调用工程类的创建方式创景对应产品,然后使用该功能
类图:
以创建手机工程为例
手机产品接口
package com.anran.example.design;
public interface Phone {
/**
* 知道手机的通用方法
* @return 返回手机名称
*/
public String make();
}
华为手机
package com.anran.example.design;
public class HuaweiPhone implements Phone {
@Override
public String make() {
return "Huawei phone";
}
}
一加手机
package com.anran.example.design;
public class OneplusPhone implements Phone {
@Override
public String make() {
return "OnePlus phone";
}
}
手机工厂类
package com.anran.example.design;
public class PhoneFactory {
/**
* 获取手机信息
* @param type 类型
* @return 手机接口
*/
public static Phone getPhone(String type) {
switch (type) {
case "Huawei" :
return new HuaweiPhone();
case "Oneplus" :
return new OneplusPhone();
default:
return null;
}
}
}
客户端使用方式
package com.anran.example.design;
public class Client {
public static void main(String[] args) {
Phone huaweiPhone = PhoneFactory.getPhone("Huawei");
System.out.println(huaweiPhone.make());
Phone oneplusPhone = PhoneFactory.getPhone("Oneplus");
System.out.println(oneplusPhone.make());
}
}
执行结果
工厂方法模式
如果在添加一个产品C使用简单工厂模式时,此时需要对工厂类进行修改,也就是说后期存在运维成本,此时就应该使用工厂方法模式:前期提供一个工程的抽象类,每添加一个产品,自行实现该工厂类,并且产品实现产品的接口。
类图:
抽象工厂类
package com.anran.example.design;
public interface AbstractPhoneFactory {
public Phone getPhone();
}
华为工厂类
package com.anran.example.design;
public class HuaweiPhoneFactory implements AbstractPhoneFactory {
@Override
public Phone getPhone() {
return new HuaweiPhone();
}
}
一加工厂类
package com.anran.example.design;
public class OneplusPhoneFactory implements AbstractPhoneFactory {
@Override
public Phone getPhone() {
return new OneplusPhone();
}
}
客户端使用方式
package com.anran.example.design;
public class Client1 {
public static void main(String[] args) {
AbstractPhoneFactory factory = new HuaweiPhoneFactory();
Phone huaweiPhone = factory.getPhone();
System.out.println(huaweiPhone.make());
AbstractPhoneFactory factory1 = new OneplusPhoneFactory();
Phone onePlusPhone = factory1.getPhone();
System.out.println(onePlusPhone.make());
}
}
执行结果:
抽象工厂模式
如果又添加了不同的产品类型(不是手机,如手机壳),此时最简单的方式是将手机的工厂模式代码直接复制一份,但是也就意味着我们要完全复制和修改手机中的所有代码,显然这是一个笨办法,并不利于扩展和维护。因此需要使用抽象工厂模式:只需要在工程接口和实现中添加对应新产品类型的方法即可。
类图:
手机壳产品接口
package com.anran.example.design;
public interface Shell {
public String make();
}
华为手机壳产品
package com.anran.example.design;
public class HuaweiShell implements Shell{
@Override
public String make() {
return "Huawei shell";
}
}
一加手机壳产品
package com.anran.example.design;
public class OneplusShell implements Shell {
@Override
public String make() {
return "Oneplus shell";
}
}
华为手机工程类
package com.anran.example.design;
public class HuaweiPhoneFactory implements AbstractPhoneFactory {
@Override
public Phone getPhone() {
return new HuaweiPhone();
}
@Override
public Shell getShell() {
return new HuaweiShell();
}
}
一加手机工厂类
package com.anran.example.design;
public class OneplusPhoneFactory implements AbstractPhoneFactory {
@Override
public Phone getPhone() {
return new OneplusPhone();
}
@Override
public Shell getShell() {
return new OneplusShell();
}
}
客户端使用方式
package com.anran.example.design;
public class Client2 {
public static void main(String[] args) {
AbstractPhoneFactory factory = new HuaweiPhoneFactory();
Phone huaweiPhone = factory.getPhone();
System.out.println(huaweiPhone.make());
Shell huaweiShell = factory.getShell();
System.out.println(huaweiShell.make());
AbstractPhoneFactory factory1 = new OneplusPhoneFactory();
Phone onePlusPhone = factory1.getPhone();
System.out.println(onePlusPhone.make());
Shell onePlusShell = factory1.getShell();
System.out.println(onePlusShell.make());
}
}
执行结果: