java设计模式之工厂模式详解
作者:北极熊不在北极
本文介绍了三种常见的设计模式:简单厂模式、工厂方法模式和抽象工厂模式,简单厂模式通过一个工厂类来生产不同的产品实例,例如同时生产华为和小米手机,工厂方法模式则通过定义一个创建对象的接口,让子类决定实例化哪一个类
一、简单厂模式
一个工厂既代工生产华为手机也生产小米手机。
- 手机接口类
package com.liuyc.designpattern.factory;
public interface Phone {
void showInfo();
}- 华为手机实现类
package com.liuyc.designpattern.factory;
public class HuaWeiPhone implements Phone{
@Override
public void showInfo() {
System.out.println("华为手机");
}
}- 小米手机实现类
package com.liuyc.designpattern.factory;
public class XiaoMiPhone implements Phone{
@Override
public void showInfo() {
System.out.println("小米手机");
}
}- 生产手机工厂类
package com.liuyc.designpattern.factory.simplefactory;
import com.liuyc.designpattern.factory.Phone;
public class SimpleFactory {
public static Phone getPhone(String className){
Phone phone = null;
try {
Class<Phone> aClass = (Class<Phone>) Class.forName(className);
phone = aClass.newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return phone;
}
}- 具体使用类
package com.liuyc.designpattern.factory.simplefactory;
import com.liuyc.designpattern.factory.Phone;
public class SimpleFactoryMain {
public static void main(String[] args) {
Phone phone = SimpleFactory.getPhone("com.liuyc.designpattern.factory.XiaoMiPhone");
phone.showInfo();
}
}二、工厂方法模式
- 一个生产手机的工厂接口类
package com.liuyc.designpattern.factory.factorymethod;
import com.liuyc.designpattern.factory.Phone;
public interface PhoneFactory {
Phone getPhone();
}- 具体生产华为手机的工厂
package com.liuyc.designpattern.factory.factorymethod;
import com.liuyc.designpattern.factory.HuaWeiPhone;
import com.liuyc.designpattern.factory.Phone;
public class HuaWeiPhoneFactory implements PhoneFactory {
@Override
public Phone getPhone() {
return new HuaWeiPhone();
}
}- 具体生产小米手机的工厂
package com.liuyc.designpattern.factory.factorymethod;
import com.liuyc.designpattern.factory.Phone;
import com.liuyc.designpattern.factory.XiaoMiPhone;
public class XiaoMiPhoneFactory implements PhoneFactory {
@Override
public Phone getPhone() {
return new XiaoMiPhone();
}
}- 具体使用类
package com.liuyc.designpattern.factory.factorymethod;
import com.liuyc.designpattern.factory.Phone;
public class FactoryMethodMain {
public static void main(String[] args) {
PhoneFactory phoneFactory = new XiaoMiPhoneFactory();
Phone phone = phoneFactory.getPhone();
phone.showInfo();
}
}三、抽象工厂模式
现在,不管是华为也好,小米也好,他们已不满足于只生产手机了,他们也开始造车了,现在有一个“组”的概念了,或者说族。
- 小车的接口类
package com.liuyc.designpattern.factory;
public interface Car {
void showInfo();
}- 华为汽车
package com.liuyc.designpattern.factory;
public class HuaWeiCar implements Car {
@Override
public void showInfo() {
System.out.println("华为小汽车");
}
}- 小米汽车
package com.liuyc.designpattern.factory;
public class XiaoMiCar implements Car {
@Override
public void showInfo() {
System.out.println("小米汽车");
}
}- 抽象工厂,定义既能生产手机,也是生产汽车
package com.liuyc.designpattern.factory.abstactfactory;
import com.liuyc.designpattern.factory.Car;
import com.liuyc.designpattern.factory.Phone;
public interface AbstractFactory {
Phone getPhone();
Car getCar();
}- 华为工厂
package com.liuyc.designpattern.factory.abstactfactory;
import com.liuyc.designpattern.factory.Car;
import com.liuyc.designpattern.factory.HuaWeiCar;
import com.liuyc.designpattern.factory.HuaWeiPhone;
import com.liuyc.designpattern.factory.Phone;
public class HuaWeiFactory implements AbstractFactory{
@Override
public Phone getPhone() {
return new HuaWeiPhone();
}
@Override
public Car getCar() {
return new HuaWeiCar();
}
}- 小米工厂
package com.liuyc.designpattern.factory.abstactfactory;
import com.liuyc.designpattern.factory.*;
public class XiaoMiFactory implements AbstractFactory{
@Override
public Phone getPhone() {
return new XiaoMiPhone();
}
@Override
public Car getCar() {
return new XiaoMiCar();
}
}- 具体使用
package com.liuyc.designpattern.factory.abstactfactory;
import com.liuyc.designpattern.factory.Car;
import com.liuyc.designpattern.factory.Phone;
public class AbstractFactoryMain {
public static void main(String[] args) {
AbstractFactory abstractFactory = new XiaoMiFactory();
Car car = abstractFactory.getCar();
Phone phone = abstractFactory.getPhone();
car.showInfo();
phone.showInfo();
}
}总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
