设计模式-工厂方法模式

定义:

定义一个创建一类对象的接口,让实现这个接口的子类决定生产哪个对象。
类型:创建者类型

应用场景

  1. 客户端对生产对象的细节并不关心,只关注传入参数,拿到对象。
  2. 创建对象需要大量的代码。

优点

  1. 客户端无需关系对象创建的细节
  2. 符合开闭原则

缺点

  1. 类的数量过多(每一个类型对象,就需要一个对应的工厂类)
  2. 理解逻辑难度增加

源码应用的场景

  1. Connection定义了返回Statement的抽象。子类去实现定义的实体类(mysql、oracle等有自己的实现)
public interface Connection  extends Wrapper, AutoCloseable {

    /**
     * Creates a <code>Statement</code> object for sending
     * SQL statements to the database.
     * SQL statements without parameters are normally
     * executed using <code>Statement</code> objects. If the same SQL statement
     * is executed many times, it may be more efficient to use a
     * <code>PreparedStatement</code> object.
     * <P>
     * Result sets created using the returned <code>Statement</code>
     * object will by default be type <code>TYPE_FORWARD_ONLY</code>
     * and have a concurrency level of <code>CONCUR_READ_ONLY</code>.
     * The holdability of the created result sets can be determined by
     * calling {@link #getHoldability}.
     *
     * @return a new default <code>Statement</code> object
     * @exception SQLException if a database access error occurs
     * or this method is called on a closed connection
     */
    Statement createStatement() throws SQLException;

}

demo

生产产品的父类

package creational.factorymethod;

public class Video {
    private  String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Video{" +
                "name='" + name + '\'' +
                '}';
    }
}

产品①

package creational.factorymethod;

public class JavaVideo extends Video {

    public JavaVideo(){
        super.setName("JavaVideo");
    }

}

产品②

package creational.factorymethod;

public class MySqlVideo  extends Video{

    public  MySqlVideo(){
        super.setName("MySqlVideo");
    }


}

工厂类的接口(抽象层)

package creational.factorymethod;
public interface VideoFactoryI {
    Video createVideo();
}

具体的工厂① 生成产品①的工厂

package creational.factorymethod;

public class JavaVideoFactory implements  VideoFactoryI {
    @Override
    public Video createVideo() {
        System.out.println("JavaVideoFactory create JavaVideo");
        return new JavaVideo();
    }
}

具体的工厂②生成产品②的工厂

package creational.factorymethod;

public class MySqlVideoFactory implements  VideoFactoryI {
    @Override
    public Video createVideo() {
        System.out.println("MySqlVideoFactory create MySqlVideo");
        return new MySqlVideo();
    }
}

测试类

package creational.factorymethod;

public class Test {

    public static void main(String[] args) {
        VideoFactoryI videoFactory = new JavaVideoFactory();
        Video video = videoFactory.createVideo();
        System.out.println(video);
        videoFactory = new MySqlVideoFactory();
        video = videoFactory.createVideo();
        System.out.println(video);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值