设计模式-工厂模式、抽象工厂模式

菜鸟教程:http://www.runoob.com/design-pattern/abstract-factory-pattern.html

 

一、工厂模式

1、概念:
    是 Java 中最常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。
3、优点:扩展性强,当需要增加一个产品时,只需要增加一个具体实体类,还有修改一下工厂方法就行了,这样使得调用者不关心具体的实现。
4、缺点:工厂模式它的优点,同时也是它的缺点,增加一个产品,就要增加一个具体类,然后修改工厂方法,这样使得代码越来越复杂。
5、使用场景:

  • 日志记录器:记录可能记录到本地硬盘、系统事件、远程服务器等,用户可以选择记录日志到什么地方。
  • 数据库访问,当用户不知道最后系统采用哪一类数据库,以及数据库可能有变化时。
  • 设计一个连接服务器的框架,需要三个协议,"POP3"、"IMAP"、"HTTP",可以把这三个作为产品类,共同实现一个接口。

6、注意事项:作为一种创建类模式,在任何需要生成复杂对象的地方,都可以使用工厂方法模式。有一点需要注意的地方就是复杂对象适合使用工厂模式,而简单对象,特别是只需要通过 new 就可以完成创建的对象,无需使用工厂模式。如果使用工厂模式,就需要引入一个工厂类,会增加系统的复杂度
>>>案例
汽车工厂,传入车的类型,工厂返回一个具体车对象

1、Car

package factory;

/**
 * Created by yanshao on 2019/1/4.
 */
public interface Car {
}

2、BmwCar

package factory;

/**
 * Created by yanshao on 2019/1/4.
 */
public class BmwCar implements Car{
}

3、JaguarCar

package factory;

/**
 * Created by yanshao on 2019/1/4.
 */
public class JaguarCar implements Car {
}

4、MercedesCar

package factory;

/**
 * Created by yanshao on 2019/1/4.
 */
public class MercedesCar  implements Car{
}

5、CarEnum

package factory;

/**
 * Created by yanshao on 2019/1/4.
 */
public enum CarEnum {
    bmw("BMW"),
    jaguar("Jaguar"),
    mercedes("Mercedes");
    String carType;
    String desc;

    CarEnum(String carType) {
        this.carType = carType;
    }

}

6、CarFactoryMain

package factory;

import static factory.CarEnum.jaguar;

/**
 * Created by yanshao on 2019/1/4.
 */
public class CarFactoryMain {

    public static void main(String[] args) {
        Car car = getInstance(jaguar);
        System.out.println("您的汽车>>>"+car.getClass().getName());
    }
    public static Car getInstance(CarEnum carType){
        Car car = null;
        if(carType == null){
            return car;
        }
        switch (carType){
            case bmw:
                car = new BmwCar();
                break;
            case jaguar:
                car = new JaguarCar();
                break;
            case mercedes:
                car = new MercedesCar();
                break;
            default:
                return null;
        }
        return car;
    }

}

7、执行结果

您的汽车>>>factory.JaguarCar

二、抽象工厂模式

1、概念:

       抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。

2、优点:当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象。

3:缺点:增加产品的相当麻烦,系统越来越复杂。

>>>>案例:

在前面汽车案例的基础上,再增加一个轮船产品,最后通过一个Factory方法,根据传入参数的不同,获取不同的对象

1、Ship

package factory;

/**
 * Created by yanshao on 2019/1/4.
 */
public interface Ship {
}

2、CruiseShip

package factory;

/**
 * Created by yanshao on 2019/1/4.
 */
public class CruiseShip implements Ship{
}

3、FreighterShip

package factory;

/**
 * Created by yanshao on 2019/1/4.
 */
public class FreighterShip implements Ship{
}

4、Factory

package factory;

/**
 * Created by yanshao on 2019/1/4.
 */
public interface Factory {
    Ship getInstance(ShipEnum shipEnum);
    Car  getInstance(CarEnum carEnum);
}

5、ShipEnum

package factory;

/**
 * Created by yanshao on 2019/1/4.
 */
public enum ShipEnum {
    freighter("Freighter"),
    cruise("Cruise");
    String carType;

    ShipEnum(String carType) {
        this.carType = carType;
    }
}

6、CarFactory

package factory;

/**
 * Created by yanshao on 2019/1/4.
 */
public class CarFactory implements Factory {

    @Override
    public Ship getInstance(ShipEnum shipEnum) {
        return null;
    }

    @Override
    public Car getInstance(CarEnum carType) {
        Car car = null;
        if(carType == null){
            return car;
        }
        switch (carType){
            case bmw:
                car = new BmwCar();
                break;
            case jaguar:
                car = new JaguarCar();
                break;
            case mercedes:
                car = new MercedesCar();
                break;
            default:
                return null;
        }
        return car;
    }
}

7、ShipFactory

package factory;

/**
 * Created by yanshao on 2019/1/4.
 */
public class ShipFactory implements Factory {
    @Override
    public Ship getInstance(ShipEnum shipType) {
        Ship ship = null;
        if(shipType == null){
            return ship;
        }
        switch (shipType){
            case cruise:
                ship = new CruiseShip();
                break;
            case freighter:
                ship = new FreighterShip();
                break;
            default:
                return null;
        }
        return ship;
    }

    @Override
    public Car getInstance(CarEnum carEnum) {
        return null;
    }
}

8、SuperFactory

package factory;

/**
 * Created by yanshao on 2019/1/4.
 */
public class SuperFactory {
    public Factory getInstance(SuperEnum superEnum){
        switch (superEnum){
            case ship:
                return new ShipFactory();
            case car:
                return new CarFactory();
            default:
                return null;
        }
    }
}

9、SuperEnum

package factory;

/**
 * Created by yanshao on 2019/1/4.
 */
public enum SuperEnum {
    ship,
    car;
}

10、FactoryMain

package factory;

/**
 * Created by yanshao on 2019/1/4.
 */
public class FactoryMain {
    public static void main(String[] args) {
        SuperFactory superFactory = new SuperFactory();
        Factory factory = superFactory.getInstance(SuperEnum.car);
        Car car = factory.getInstance(CarEnum.jaguar);
        System.out.println("您的车>>>"+car.getClass().getName());

        Factory factory1 = superFactory.getInstance(SuperEnum.ship);
        Ship ship = factory1.getInstance(ShipEnum.cruise);
        System.out.println("您的船>>>"+ship.getClass().getName());
    }
}

11、执行结果

您的车>>>factory.JaguarCar
您的川>>>factory.CruiseShip

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

燕少༒江湖

给我一份鼓励!谢谢!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值