比较:
- 简单工厂:定义一个工厂类,根据应用端传入的不同参数创建不同的业务实例。也可以理解为统一的静态方法工厂,该工厂类负责所有不同业务类型的封装。
- 工厂方法:将简单工厂进行优化,定义多种业务类,并定义每种业务相对应的工厂类,即不同的工厂类各自封装对应的业务,由应用端按需创建工厂及调用工厂封装的业务方法。
- 抽象工厂:将工厂方法进行抽象及优化,定义多种业务类,定义多个工厂类,工厂类可以封装多种业务类型,由应用端按需创建工厂及调用工厂封装的多种不同的业务类型。
代码分析:
- 简单工厂的代码
//Phone抽象类
public abstract class Phone {
abstract void produce();
}
//ApplePhone类
public class ApplePhone extends Phone {
@Override
void produce() {
System.out.println("生产苹果手机");
}
}
//HuaweiPhone类
public class HuaweiPhone extends Phone {
@Override
void produce() {
System.out.println("生产华为手机");
}
}
//简单工厂类
public class PhoneFactory {
Phone getPhone(String type){
switch (type.toLowerCase()){
case "apple":
return new ApplePhone();
case "huawei":
return new HuaweiPhone();
default:
return null;
}
}
}
//测试类
public class Test {
public static void main(String[] args) {
PhoneFactory phoneFactory = new PhoneFactory();
Phone phone=phoneFactory.getPhone("apple");
phone.produce();
PhoneFactory phoneFactory1 = new PhoneFactory();
Phone phone1=phoneFactory.getPhone("huawei");
phone1.produce();
}
}
- 工厂方法的代码
//Phone抽象类
public abstract class Phone {
abstract void produce();
}
//PhoneFactory接口
public interface PhoneFactory {
Phone getPhone();
}
//通过继承抽象类定义ApplePhone
public class ApplePhone extends Phone {
@Override
void produce() {
System.out.println("生产苹果手机");
}
}
//通过实现接口定义ApplePhoneFactory
public class ApplePhoneFactory implements PhoneFactory {
@Override
public Phone getPhone() {
return new ApplePhone();
}
}
//通过继承抽象类定义HuaweiPhone
public class HuaweiPhone extends Phone {
@Override
void produce() {
System.out.println("生产华为手机");
}
}
//通过实现接口定义HuaweiPhoneFactory
public class HuaweiPhoneFactory implements PhoneFactory {
@Override
public Phone getPhone() {
return new HuaweiPhone();
}
}
//Test测试类
public class Test {
public static void main(String[] args) {
PhoneFactory phoneFactory= new ApplePhoneFactory();
phoneFactory.getPhone().produce();
PhoneFactory phoneFactory1 = new HuaweiPhoneFactory();
phoneFactory1.getPhone().produce();
}
}
- 抽象工厂的代码
//Phone抽象类,可以用接口方式代替
public abstract class Phone {
abstract void producePhone();
}
//NoteBook抽象类,可以用接口方式代替
public abstract class NoteBook {
abstract void produceNoteBook();
}
//抽象工厂
public interface Factory {
Phone getPhone();
NoteBook getNoteBook();
}
//Apple Phone类
public class ApplePhone extends Phone {
@Override
void producePhone() {
System.out.println("生产苹果手机");
}
}
//Apple MacBook类
public class AppleMacBook extends NoteBook {
@Override
void produceNoteBook() {
System.out.println("生产苹果MacBook");
}
}
//Apple工厂类
public class AppleFactory implements Factory {
@Override
public Phone getPhone() {
return new ApplePhone();
}
@Override
public NoteBook getNoteBook() {
return new AppleMacBook();
}
}
//Huawei Phone类
public class HuaweiPhone extends Phone {
@Override
void producePhone() {
System.out.println("生产华为手机");
}
}
//Huawei NoteBook类
public class HuaweiNoteBook extends NoteBook {
@Override
void produceNoteBook() {
System.out.println("生产华为笔记本电脑");
}
}
//Huawei工厂类
public class HuaweiFactory implements Factory {
@Override
public Phone getPhone() {
return new HuaweiPhone();
}
@Override
public NoteBook getNoteBook() {
return new HuaweiNoteBook();
}
}
//测试类
public class Test {
public static void main(String[] args) {
Factory appleFactory = new AppleFactory();
appleFactory.getPhone().producePhone();
appleFactory.getNoteBook().produceNoteBook();
Factory huaweiFactory = new HuaweiFactory();
huaweiFactory.getPhone().producePhone();
huaweiFactory.getNoteBook().produceNoteBook();
}
}