模板设计模式的思路

var Coffee = function(){

    }
    Coffee.prototype.boilWater = function(){
        console.log('boilWater')
    }
    Coffee.prototype.brewCoffee = function(){
        console.log('brewCoffee')
    }
    Coffee.prototype.pourInCup = function(){
        console.log('pourInCup')
    }
    Coffee.prototype.addSugarAndMilk = function(){
        console.log('addSugarAndMilk')
    }
    Coffee.prototype.init = function(){
        this.boilWater();
        this.brewCoffee();
        this.pourInCup();
        this.addSugarAndMilk();

    }

    var Tea = function(){

    }
    Tea.prototype.boilWater = function(){
        console.log('boilWater')
    }
    Tea.prototype.steepTea = function(){
        console.log('steepTea')
    }
    Tea.prototype.pourInCup = function(){
        console.log('pourInCup')
    }
    Tea.prototype.addLemon = function(){
        console.log('boilWater')
    }
    Tea.prototype.init = function(){
        this.boilWater();
        this.steepTea();
        this.pourInCup();
        this.addLemon();
    }

如上图我们可以进行抽象:

    var Beverage = function(){

    };
    Beverage.prototype.boilWater = function(){//烧水
        console.log('boilWater'); //子对象不需要重写这个方法,通过原型链
    }
    Beverage.prototype.brew = function(){ //泡饮料
        throw new Error('must rewrite'); //子对象必须重写此方法否则报错
    }
    Beverage.prototype.pourInCup = function(){//倒水
        throw new Error('must rewrite');

    }
    Beverage.prototype.addCondiments = function(){//加调料
        throw new Error('must rewrite');

    }
    Beverage.prototype.init = function(){
        this.boilWater();
        this.brew();
        this.pourInCup();
        this.addCondiments();
    }


    var Coffee = function(){

    }
    Coffee.prototype.boilWater = function(){
        console.log('boilWater')
    }
    Coffee.prototype.brew = function(){
        console.log('brewCoffee');//冲泡
    }
    Coffee.prototype.pourInCup = function(){
        console.log('pourInCup');
    }
    Coffee.prototype.addCondiments = function(){
        console.log('addSugarAndMilk');
    }


    var Tea = function(){

    }
    Tea.prototype.boilWater = function(){
        console.log('boilWater');
    }
    Tea.prototype.steepTea = function(){
        console.log('steepTea');//浸泡
    }
    Tea.prototype.pourInCup = function(){
        console.log('pourInCup');
    }
    Tea.prototype.addCondiments = function(){
        console.log('addLemon')
    }

    Coffee.prototype = new Beverage();
    Tea.prototype = new Beverage();

    var coffee = new Coffee();
    var tea = new Tea();

    coffee.init();
    tea.init();

设计一些钩子函数

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Document</title>
</head>
<body>

</body>
</html>
<script type="text/javascript">
    var Beverage = function(){

    };
    Beverage.prototype.boilWater = function(){//烧水
        console.log('boilWater');
    }
    Beverage.prototype.brew = function(){ //泡饮料
        throw new Error('must rewrite');
    }
    Beverage.prototype.pourInCup = function(){//倒水
        throw new Error('must rewrite');

    }
    Beverage.prototype.addCondiments = function(){//加调料
        throw new Error('must rewrite');

    }
    Beverage.prototype.customerWantsCondiments = function(){
        return true;
    }
    Beverage.prototype.init = function(){
        this.boilWater();
        this.brew();
        this.pourInCup();
        if(this.customerWantsCondiments){
            this.addCondiments();
        }
    }


    var Coffee = function(){

    }
    Coffee.prototype.boilWater = function(){
        console.log('boilWater')
    }
    Coffee.prototype.brew = function(){
        console.log('brewCoffee');//冲泡
    }
    Coffee.prototype.pourInCup = function(){
        console.log('pourInCup');
    }
    Coffee.prototype.addCondiments = function(){
        console.log('addSugarAndMilk');
    }


    var Tea = function(){

    }
    Tea.prototype.boilWater = function(){
        console.log('boilWater');
    }
    Tea.prototype.steepTea = function(){
        console.log('steepTea');//浸泡
    }
    Tea.prototype.pourInCup = function(){
        console.log('pourInCup');
    }
    Tea.prototype.addCondiments = function(){
        console.log('addLemon');
    }
    Tea.prototype.customerWantsCondiments = function(){
        return window.confirm('wants condiments?');
    }

    Coffee.prototype = new Beverage();
    Tea.prototype = new Beverage();

    var coffee = new Coffee();
    var tea = new Tea();

    coffee.init();
    tea.init();



</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值