js 面向对象设计模式--策列模式

24 篇文章 0 订阅
23 篇文章 0 订阅

策列模式将一组算法封装起来使其相互之间可以替换。封装的算法具有独立性,不会随客户端变化而变化。

// //价格策列对象
// let PriceState = function () {
//     //内部双算法对象
//     let stragtegy = {
//         //100返回30
//         return30:function (price) {
//             return price+parseInt(price/100)*30;
//         },
//         //100返50
//         return50:function (price) {
//             return price+parseInt(price/100)*50;
//         },
//         //打9折
//         percent90:function (price) {
//             //js 在处理乘法除有bug
//             return price*100*90/10000;
//         },
//         //打八折
//         percent80:function (price) {
//             return price*100*90/10000;
//         },
//         //5折
//         percent50:function (price) {
//             return price*100*50/10000;
//         }
//     }
//     return function (algo,price) {//构造函数
//         return stragtegy[algo]&&stragtegy[algo](price);
//     }
// }();
// let p = PriceState('return30',314.67);
// console.log(p);
let InputStrategy = function () {
    let strateg = {
        notNull:function (value) {
            return value+" 不能为空";
        }
    }
    return {
        check:function (type,value) {
            return strateg[type]?strateg[type](value):"该检测方法不存在";
        },
        addStrate:function (type,fn) {
            strateg[type] = fn;
        }
    }
}();
let str = InputStrategy.check('notNull',"hello");
console.log(str);
InputStrategy.addStrate('say',function (value) {
    return value + "说话";
});
let man = InputStrategy.check('say',"world");
console.log(man);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

weixin_42975115

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值