Java~设计模式之工厂模式与抽象工厂模式

@Override

public String showCPU() {

return “HuaWei:” + this.cpu;

}

@Override

public String showRAM() {

return “HuaWei:” + this.ram;

}

@Override

public String showSystem() {

return “HuaWei:” + this.system;

}

@Override

public String showBattery() {

return “HuaWei:” + this.battery;

}

}

工厂

/**

  • Created with IntelliJ IDEA.

  • Description: If you don’t work hard, you will a loser.

  • User: Listen-Y.

  • Date: 2020-08-28

  • Time: 14:30

*/

public class PhoneFactory {

public static Phone makePhone(String cpu, String ram, String system, int battery) {

if (system != null && (system.contains(“ios”) || system.contains(“IOS”))) {

return new IPhone(cpu, ram, system, battery);

} else if (system != null && (system.contains(“Android”) || system.contains(“android”))) {

return new HuaWei(cpu, ram, system, battery);

}

return null;

}

}

演示

/**

  • Created with IntelliJ IDEA.

  • Description: If you don’t work hard, you will a loser.

  • User: Listen-Y.

  • Date: 2020-08-28

  • Time: 14:37

*/

public class TestPhoneFactory {

public static void main(String[] args) {

Phone phone = PhoneFactory.makePhone(“A14”, “4G”, “ios14”, 2500);

Phone phone1 = PhoneFactory.makePhone(“麒麟990”, “8G”, “Android7”, 4500);

if (phone != null) {

System.out.println(phone.showCPU());

System.out.println(phone.showRAM());

System.out.println(phone.showSystem());

System.out.println(phone.showBattery());

}

System.out.println(“========================”);

if (phone1 != null) {

System.out.println(phone1.showCPU());

System.out.println(phone1.showRAM());

System.out.println(phone1.showSystem());

System.out.println(phone1.showBattery());

}

}

}

IPhone:A14

IPhone:4G

IPhone:ios14

IPhone:2500

========================

HuaWei:麒麟990

HuaWei:8G

HuaWei:Android7

HuaWei:4500

工厂设计模式的优点

面向接口编程,体现了面向对象的思想;

将创建对象的工作转移到了工厂类;

JDK 中的工厂设计模式实例

java.util.Calendar, ResourceBundle and NumberFormat getInstance() 使用了工厂方法模式;

valueOf() 在包装类中,如Boolean, Integer 也使用了工厂方法模式;

抽象工厂模式


  • 上面说到的抽象模式只是针对一个产品 如果我要针对多个产品 比如现在要这个工厂还要生产computer, 这样的工厂就是抽象工厂

  • 抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

  • 在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。

Computer接口

/**

  • Created with IntelliJ IDEA.

  • Description: If you don’t work hard, you will a loser.

  • User: Listen-Y.

  • Date: 2020-08-28

  • Time: 15:19

*/

public interface Computer {

String showComputer();

}

实例1

/**

  • Created with IntelliJ IDEA.

  • Description: If you don’t work hard, you will a loser.

  • User: Listen-Y.

  • Date: 2020-08-28

  • Time: 14:19

*/

public class IPhone implements Phone, Computer {

private String cpu;

private String ram;

private String system;

private int battery;

public IPhone(String cpu, String ram, String system, int battery) {

this.cpu = cpu;

this.ram = ram;

this.system = system;

this.battery = battery;

}

public IPhone(){};

@Override

public String showCPU() {

return “IPhone:” + this.cpu;

}

@Override

public String showRAM() {

return “IPhone:” + this.ram;

}

@Override

public String showSystem() {

return “IPhone:” + this.system;

}

@Override

public String showBattery() {

return “IPhone:” + this.battery;

}

@Override

public String showComputer() {

return “Apple: Computer”;

}

}

实例2

/**

  • Created with IntelliJ IDEA.

  • Description: If you don’t work hard, you will a loser.

  • User: Listen-Y.

  • Date: 2020-08-28

  • Time: 14:27

*/

public class HuaWei implements Phone, Computer {

private String cpu;

private String ram;

private String system;

private int battery;

public HuaWei(String cpu, String ram, String system, int battery) {

this.cpu = cpu;

this.ram = ram;

this.system = system;

this.battery = battery;

}

public HuaWei(){};

@Override

public String showCPU() {

return “HuaWei:” + this.cpu;

}

@Override

public String showRAM() {

return “HuaWei:” + this.ram;

}

@Override

public String showSystem() {

return “HuaWei:” + this.system;

}

@Override

public String showBattery() {

return “HuaWei:” + this.battery;

}

@Override

public String showComputer() {

return “HuaWei: Computer”;

}

}

抽象工厂

/**

  • Created with IntelliJ IDEA.

  • Description: If you don’t work hard, you will a loser.

  • User: Listen-Y.

  • Date: 2020-08-28

  • Time: 15:19

*/

public abstract class AbstractFactory {

public abstract Phone makePhone(String cpu, String ram, String system, int battery);

public abstract Computer makeComputer(String type);

}

Computer工厂

/**

  • Created with IntelliJ IDEA.

  • Description: If you don’t work hard, you will a loser.

  • User: Listen-Y.

  • Date: 2020-08-28

  • Time: 15:24

*/

public class ComputerFactory extends AbstractFactory {

@Override

public Phone makePhone(String cpu, String ram, String system, int battery) {

return null;

}

@Override

public Computer makeComputer(String type) {

if (type.equalsIgnoreCase(“apple”)) {

return new IPhone();

} else if (type.equalsIgnoreCase(“HuaWei”)) {

return new HuaWei();

}

return null;

}

}

工厂生成器

/**

  • Created with IntelliJ IDEA.

  • Description: If you don’t work hard, you will a loser.

  • User: Listen-Y.

  • Date: 2020-08-28

  • Time: 15:39

*/

public class FactoryProducer {

public static AbstractFactory getFactory(String type) {

if (type.equalsIgnoreCase(“computer”)) {

return new ComputerFactory();

} else if (type.equalsIgnoreCase(“phone”)) {

return new PhoneFactory();

}

return null;

}

}

演示

/**

  • Created with IntelliJ IDEA.

  • Description: If you don’t work hard, you will a loser.

  • User: Listen-Y.

  • Date: 2020-08-28

  • Time: 14:37

*/

public class TestPhoneFactory {

public static void main(String[] args) {

AbstractFactory phoneFactory = FactoryProducer.getFactory(“phone”);

assert phoneFactory != null;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值