一、工厂模式分类
- 简单工厂模式
- 工厂方法模式
- 抽象工厂模式
二、简单工厂模式
简单工厂就是讲需要new对象的那段代码单独封装成一个对象(方法)
demo(汽车4s店):
汽车接口
public interface Car {
public String getName();
}
汽车实现类:
public class BYD implements Car {
String name;
public BYD(String name) {
this.name=name;
}
@Override
public String getName() {
// TODO 自动生成的方法存根
return name;
}
}
public class BMW implements Car {
String name;
public BMW(String name) {
this.name=name;
}
@Override
public String getName() {
// TODO 自动生成的方法存根
return name;
}
}
汽车工厂
public class CarFactory {
public static Car create(String name) {
Car car=null;
if(name.equals("BYD")) {
car=new BYD(name);
}else if(name.equals("BMW")) {
car=new BMW(name);
}
return car;
}
}
汽车4s店
public class CarStore {
public static final String BMW="BMW";
public static final String BYD="BYD";
public Car buy(String name) {
Car car=CarFactory.create(name);
return car;
}
}
测试代码:
public class Test {
public static void main(String[] args) {
CarStore store=new CarStore();
Car car=store.buy(CarStore.BMW);
System.out.println(car.getName());
}
}
运行结果:
三、工厂方法模式
工厂方法模式就是将工厂交由子类处理不同子类的工厂制造不同种类的对象
demo(汽车4s店):
汽车接口:
public interface Car {
public String getType();
public String getName();
}
汽车实现类:
class HightBYD implements Car {
private String type="Height";
private String name="BYD";
@Override
public String getType() {
// TODO 自动生成的方法存根
return type;
}
@Override
public String getName() {
// TODO 自动生成的方法存根
return name;
}
}
class LowBYD implements Car {
private String type="Low";
private String name="BYD";
@Override
public String getType() {
// TODO 自动生成的方法存根
return type;
}
@Override
public String getName() {
// TODO 自动生成的方法存根
return name;
}
}
class HightBMW implements Car {
private String type="Hight";
private String name="BMW";
@Override
public String getType() {
// TODO 自动生成的方法存根
return type;
}
@Override
public String getName() {
// TODO 自动生成的方法存根
return name;
}
}
class LowBMW implements Car {
private String type="Low";
private String name="BMW";
@Override
public String getType() {
// TODO 自动生成的方法存根
return type;
}
@Override
public String getName() {
// TODO 自动生成的方法存根
return name;
}
}
汽车4s店接口:
public abstract class CarStore {
public static final String HIGHT="Hight";
public static final String LOW="Low";
public Car Buy(String type) {
Car car=create(type);
return car;
}
public abstract Car create(String type);
}
汽车4s店子类并实现工厂方法:
class BYDStore extends CarStore {
@Override
public Car create(String type) {
// TODO 自动生成的方法存根
Car car=null;
if(type.equals(CarStore.HIGHT)) {
car=new HightBYD();
}else if(type.equals(CarStore.LOW)) {
car=new LowBYD();
}
return car;
}
}
class BMWStore extends CarStore {
@Override
public Car create(String type) {
// TODO 自动生成的方法存根
Car car=null;
if(type.equals(CarStore.HIGHT)) {
car=new HightBMW();
}else if(type.equals(CarStore.LOW)) {
car=new LowBMW();
}
return car;
}
}
测试代码:
public class Test {
public static void main(String[] args) {
BMWStore bmwStore=new BMWStore();
Car car1=bmwStore.Buy(CarStore.HIGHT);
System.out.println(car1.getType()+car1.getName());
}
}
运行结果:
四、抽象工厂模式
抽象工厂模式不再生产单一对象,当有多个接口时形成产品族时使用。
demo
汽车4s店类
public class CarStore {
public static final String HIGHT="Hight";
public static final String LOW="Low";
public Car buy(String type) {
Car car=null;
if(type.equals(CarStore.HIGHT)) {
car=new Car(new HightDeviceFactory());
}else if(type.equals(CarStore.LOW)) {
car=new Car(new LowDeviceFactory());
}
return car;
}
}
汽车类
class Car {
DeviceFactory deviceFactory;
Tyre tyre;
Wheel wheel;
public Car(DeviceFactory deviceFactory) {
this.deviceFactory=deviceFactory;
}
public void create() {
// TODO 自动生成的方法存根
tyre=deviceFactory.getTyre();
wheel=deviceFactory.getWheel();
}
}
部件接口与实现类
interface Tyre {
}
class HightTyre implements Tyre{
public HightTyre() {
System.out.print("HightTyre");
}
}
class LowTyre implements Tyre{
public LowTyre() {
System.out.print("LowTyre");
}
}
interface Wheel {
}
class HightWheel implements Wheel{
public HightWheel() {
System.out.print("HightWheel");
}
}
class LowWheel implements Wheel{
public LowWheel() {
System.out.print("LowWheel");
}
}
部件工厂接口
public interface DeviceFactory {
public Tyre getTyre();
public Wheel getWheel();
}
部件工厂实现类
class HightDeviceFactory implements DeviceFactory{
@Override
public Tyre getTyre() {
// TODO 自动生成的方法存根
return new HightTyre();
}
@Override
public Wheel getWheel() {
// TODO 自动生成的方法存根
return new HightWheel();
}
}
class LowDeviceFactory implements DeviceFactory{
@Override
public Tyre getTyre() {
// TODO 自动生成的方法存根
return new LowTyre();
}
@Override
public Wheel getWheel() {
// TODO 自动生成的方法存根
return new LowWheel();
}
}
测试代码
public class Test {
public static void main(String[] args) {
CarStore carStore=new CarStore();
Car car=carStore.buy(CarStore.LOW);
car.create();
}
}
运行结果: