Code Monkey home page Code Monkey logo

js-design-mode's Introduction

js-design-mode

js的设计模式

js-design-mode's People

Contributors

pleasestartyourperformance avatar

Stargazers

fanshanhong avatar Karl avatar Umbrella avatar 沈文杰 avatar  avatar ZhangMingzhe avatar chali avatar dujiang avatar  avatar ZhenYouLi avatar

Watchers

James Cloos avatar  avatar

js-design-mode's Issues

JS-设计模式之工厂模式

// #简单工厂模式第一种
/**
 * 足球类
 */
var FootBall = function  () {
    this.play = function () {
        console.log('我在踢足球');
    }
}

/**
 * 篮球类
 */
var BasketBall = function  () {
    this.play = function () {
        console.log('我在打篮球');
    }
}

var football = new FootBall();
football.play();
var basketball = new BasketBall();
basketball.play();

/**
 * 球类工厂
 */
var Ball = function(name) {
    switch (name) {
        case '足球':
            return new FootBall();
        break;
        case '篮球':
            return new BasketBall();
        break;
    }
}
var football =  Ball('足球');
football.play();
var basketball =  Ball('篮球');
basketball.play();

// #简单工厂模式第一种end

这段案例可以简单的这么去理解,假设我们需要多个球,我们希望在使用球

的时候,只需要告诉管理员我们需要的球的类型,不需要一个个去找对应的球

这个管理员就相对于工厂函数。

简单讲就是使用简单工厂模式,那么你就不需要关心它的具体实现,

你只需要知道你要使用的类型,那么工厂函数会自动帮你去做对应的事情

原文连接:https://www.jianshu.com/p/0eed7edc93c0
github:https://github.com/ToNiQian/js-design-pattern/blob/master/demo1/index.js

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.