js创建型设计模式--简单工厂模式

ES5

//定义: 工厂可以创建很多对象, 根据客户需求的不同, 返回不同的创建对象

    //es5
    //定义: 工厂可以创建很多对象, 根据客户需求的不同, 返回不同的创建对象
    //篮球
    var BasketBall=function () {
        this.intro="篮球盛行与美国";
    }
    BasketBall.prototype={
        getMemeber:function () {
            console.log("篮球需要5名队员");
        },
        getBallSize:function () {
            console.log("篮球很大");
        }
    }

    BasketBall.prototype={
        getMemeber:function () {
            console.log("篮球需要5名队员");
        },
        getBallSize:function () {
            console.log("篮球很大");
        }
    }

    //足球
    var FootBall=function () {
        this.intro="足球盛行与美国";
    }
    FootBall.prototype={
        getMemeber:function () {
            console.log("足球需要11名队员");
        },
        getBallSize:function () {
            console.log("足球很大");
        }
    }
    //网球
    var Tennis=function () {
        this.intro="网球盛行与美国";
    }
    Tennis.prototype={
        getMemeber:function () {
            console.log("网球需要1名队员");
        },
        getBallSize:function () {
            console.log("网球很大");
        }
    }
    //球工厂
    var SportsFactory=function (name) {
        switch (name){
            case "NBA":
                return new BasketBall();
            case "wordCup":
                return new FootBall();
            case "FrenchOpen":
                return new Tennis();
        }
    }

    var football=SportsFactory("wordCup");
    console.log(football);

ES6

    //es6
    //定义: 工厂可以创建很多对象, 根据客户需求的不同, 返回不同的创建对象
    //篮球
    class BasketBall2{
        constructor(){
            this.intro="篮球盛行与美国";
        }
        getMemeber () {
            console.log("篮球需要5名队员");
        }
        getBallSize () {
            console.log("篮球很大");
        }
    }
    //足球
    class FootBall2{
        constructor(){
            this.intro="足球盛行与美国";
        }
        getMemeber () {
            console.log("足球需要11名队员");
        }
        getBallSize () {
            console.log("足球很大");
        }
    }

    //网球
    class Tennis2{
        constructor(){
            this.intro="网球盛行与美国";
        }
        getMemeber () {
            console.log("网球需要1名队员");
        }
        getBallSize () {
            console.log("网球很大");
        }
    }

    //球工厂
    class SportsFactory2{
        constructor(name){
            this.name=name;
            return this.getBall();
        }
        getBall(){
            switch (this.name){
                case "NBA":
                    return new BasketBall();
                case "wordCup":
                    return new FootBall();
                case "FrenchOpen":
                    return new Tennis();
            }
        }
    }
    let football2= new SportsFactory2("wordCup");
    console.log(football);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值