(04)工厂模式
概念
工厂类主要用来代替new对象,当需要一个对象时,直接从工厂中获取即可
实现方式
简单工厂
专门定义一个工厂类创建不同产品。可以根据参数的不同,创建不同产品对象。被创建的产品通常都具有共同的父类。
例如创建一个手机工厂,该工厂可以生产华为手机,也可以生产苹果手机,用户只需要传入不同的参数,即可获得不同的手机。
// 所有手机的父类
public abstract class BasePhone {
public abstract void call();
}
// 华为手机
public class HuaweiPhone extends BasePhone {
public void call() {
System.out.println("华为手机打电话");
}
}
// 苹果手机
public class IPhone extends BasePhone {
public void call() {
System.out.println("苹果手机打电话");
}
}
/**
* 简单工厂
*/
public class Demo {
public static void main(String[] args) {
BasePhone iphone = PhoneFactory.createPhone(PhoneFactory.PG);
iphone.call();
BasePhone huawei = PhoneFactory.createPhone(PhoneFactory.HW);
huawei.call();
}
}
// 手机工厂
class PhoneFactory {
public static final String PG = "iphone";
public static final String HW = "huawei";
public static BasePhone createPhone(String brand) {
if ("iphone".equals(brand)) {
return new IPhone();
} else if ("huawei".equals(brand)) {
return new HuaweiPhone();
}
return null;
}
}
苹果手机打电话
华为手机打电话
工厂方法
根据简单工厂改变而成。现实是生活中,不可能一个手机工厂既生产华为手机,又生产苹果手机。
应该华为有华为的工厂,苹果有苹果的工厂,华为工厂和苹果工厂都有一个共同的父类工厂,父类工厂用来规范所有的子工厂。
// 所有手机的父类
public abstract class BasePhone {
public abstract void call();
}
// 华为手机
public class HuaweiPhone extends BasePhone {
public void call() {
System.out.println("华为手机打电话");
}
}
// 苹果手机
public class IPhone extends BasePhone {
public void call() {
System.out.println("苹果手机打电话");
}
}
/**
* 工厂方法
*/
public class Demo {
public static void main(String[] args) {
PhoneFactory iphoneFactory = new IphoneFactory();
BasePhone iphone = iphoneFactory.createPhone();
iphone.call();
PhoneFactory huaweiFactory = new HuaweiFactory();
BasePhone huaweiPhone = huaweiFactory.createPhone();
huaweiPhone.call();
}
}
interface PhoneFactory {
BasePhone createPhone();
}
class IphoneFactory implements PhoneFactory {
public BasePhone createPhone() {
return new IPhone();
}
}
class HuaweiFactory implements PhoneFactory {
public BasePhone createPhone() {
return new HuaweiPhone();
}
}
抽象工厂
根据工厂方法改变而成。华为工厂和苹果工厂虽然可以生产手机,但是也可以生产笔记本、台式机等。当工厂方法中不再只生产一种类型产品时,就会称为成抽象工厂
// 所有电脑的父类
public abstract class BaseComputer {
public abstract void start();
}
// 华为电脑
public class HuaweiComputer extends BaseComputer {
public void start() {
System.out.println("华为电脑开机");
}
}
// 苹果电脑
public class ImacComputer extends BaseComputer {
public void start() {
System.out.println("苹果电脑开机");
}
}
// 所有手机的父类
public abstract class BasePhone {
public abstract void call();
}
// 华为手机
public class HuaweiPhone extends BasePhone {
public void call() {
System.out.println("华为手机打电话");
}
}
// 苹果手机
public class IPhone extends BasePhone {
public void call() {
System.out.println("苹果手机打电话");
}
}
/**
* 抽象工厂
*/
public class Demo {
public static void main(String[] args) {
PhoneFactory iphoneFactory = new IphoneFactory();
BasePhone iphone = iphoneFactory.createPhone();
BaseComputer ImacComputer = iphoneFactory.createComputer();
iphone.call();
ImacComputer.start();
}
}
interface PhoneFactory {
BasePhone createPhone();
BaseComputer createComputer();
}
class IphoneFactory implements PhoneFactory {
public BasePhone createPhone() {
return new IPhone();
}
public BaseComputer createComputer() {
return new ImacComputer();
}
}
class HuaweiFactory implements PhoneFactory {
public BasePhone createPhone() {
return new HuaweiPhone();
}
public BaseComputer createComputer() {
return new HuaweiComputer();
}
}
苹果手机打电话
苹果电脑开机