我想学会设计模式之模板模式

122 阅读2分钟

我要学会设计模式之模板模式


这是我参与8月更文挑战的第9天,活动详情查看:8月更文挑战


什么是模板模式?

模板模式是:抽象父类定义了子类需要重写的相关方法。并且这些方法,仍然是通过父类方法调用的。

根据描述,父类提供了“模板”并决定是否调用,子类进行具体实现。

image-20210811232451412


优缺点

优点

 (1)具体细节步骤实现定义在子类中,子类定义详å细处理算法是不会改变算法整体结构。

 (2)代码复用的基本技术,在数据库设计中尤为重要。

 (3)存在一种反向的控制结构,通过一个父类调用其子类的操作,通过子类对父类进行扩展增加新的行为,符合“开闭原则”。

不足

每个不同的实现都需要定义一个子类,会导致类的个数增加,系统更加庞大。


应用场景

一些系统的架构或者算法骨架,由“BOSS”编写抽象方法,具体的实现,交给“小弟们”实现。

而用不用“小弟们”的方法,还是看“BOSS”的心情。


多语言实现

Animal是抽象类,DogCat分别具体实现了eat()sleep()方法。

DogCat实例可以通过live()方法调用eat()sleep()

注意CatDog实例会被自动添加 live()方法。不暴露live()是为了防止live()被子类重写,保证父类的控制权

class Animal {
    constructor() {
        // this 指向实例
        this.live = () => {
            this.eat();
            this.sleep();
        };
    }
    eat() {
        throw new Error("模板类方法必须被重写");
    }
    sleep() {
        throw new Error("模板类方法必须被重写");
    }
}
class Dog extends Animal {
    constructor(...args) {
        super(...args);
    }
    eat() {
        console.log("狗吃粮");
    }
    sleep() {
        console.log("狗睡觉");
    }
}
class Cat extends Animal {
    constructor(...args) {
        super(...args);
    }
    eat() {
        console.log("猫吃粮");
    }
    sleep() {
        console.log("猫睡觉");
    }
}
/********* 以下为测试代码 ********/
// 此时, Animal中的this指向dog
let dog = new Dog();
dog.live();
// 此时, Animal中的this指向cat
let cat = new Cat();
cat.live();
​
public class JDBCUtils {
    private static String url = "jdbc:mysql://localhost:3306/jdbcstudy";
    private static String user = "root";
    private static String password = "123";
​
    private JDBCUtils() {
    }
​
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new ExceptionInInitializerError(e);
        }
    }
​
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }
    
    public static void free(ResultSet rs, PreparedStatement ps, Connection conn){
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(ps != null){
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}