最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

結(jié)合?ES6?類編寫JavaScript?創(chuàng)建型模式

 更新時間:2022年07月21日 09:07:15   作者:??天行無忌?  
這篇文章主要介紹了結(jié)合ES6類編寫JavaScript創(chuàng)建型模式,本文開始系統(tǒng)性的對20多種JavaScript?設(shè)計模式進行簡單概述,然后結(jié)合ES6類的方式來編寫實例代碼展示其使用方式,需要的朋友可以參考一下

前言

什么是設(shè)計模式?

設(shè)計模式是軟件設(shè)計中常見問題的解決方案,這些模式很容易重復(fù)使用并且富有表現(xiàn)力。

在軟件工程中,設(shè)計模式(design pattern)是對軟件設(shè)計中普遍存在(反復(fù)出現(xiàn))的各種問題,所提出的解決方案。它并不直接用來完成代碼的編寫,而是描述在各種不同情況下,要怎么解決問題的一種方案。面向?qū)ο笤O(shè)計模式通常以類別或?qū)ο髞砻枋銎渲械年P(guān)系和相互作用,但不涉及用來完成應(yīng)用程序的特定類別或?qū)ο蟆?mdash;— 維基百科

有三種模式:創(chuàng)建型模式,結(jié)構(gòu)型模式、行為型模式。

  • 創(chuàng)建型模式:解決與創(chuàng)建對象相關(guān)的問題。
  • 結(jié)構(gòu)型模式:處理實體之間的關(guān)系,以及它們?nèi)绾喂餐M成一個更大的結(jié)構(gòu)。
  • 行為型模式:處理對象如何相互通信和交互。

創(chuàng)建型設(shè)計模式

創(chuàng)建設(shè)計模式將創(chuàng)建對象,而不是直接實例化對象。

在軟件工程中,創(chuàng)建型模式是處理對象創(chuàng)建的設(shè)計模式,試圖根據(jù)實際情況使用合適的方式創(chuàng)建對象,因為基本的對象創(chuàng)建方式可能會導(dǎo)致設(shè)計上的問題,或增加設(shè)計的復(fù)雜度。創(chuàng)建型模式的關(guān)注點是如何創(chuàng)建對象,其核心思想是要把對象的創(chuàng)建和使用相分離。—— 維基百科

  • 工廠模式
  • 抽象工廠
  • 構(gòu)建器模式
  • 原型模式
  • 單例模式

1. 工廠模式

工廠模式定義了一個用于創(chuàng)建單個對象的接口,并讓子類決定要實例化類。

工廠方法模式(英語:Factory method pattern)是一種實現(xiàn)了“工廠”概念的面向?qū)ο笤O(shè)計模式。就像其他創(chuàng)建型模式一樣,它也是處理在不指定對象具體類型的情況下創(chuàng)建對象的問題。工廠方法模式的實質(zhì)是“定義一個創(chuàng)建對象的接口,但讓實現(xiàn)這個接口的類來決定實例化哪個類。—— 維基百科

實例

以一個點為例,有一個 Point 類,必須創(chuàng)建一個笛卡爾點和一個極點。將定義一個 Point 工廠來完成這項工作。

CoordinateSystem = {
    CARTESIAN:0,
    POLAR:1
};

class Point {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }

    static get factory() {
        return new PointFactory();
    }
}

現(xiàn)在將創(chuàng)建 Point 工廠,現(xiàn)在將使用工廠:

class Point {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }
    static get factory() {
        return new PointFactory();
    }
}
class PointFactory {
    static newCartesianPoint(x, y) {
        return new Point(x, y);
    }

    static newPolarPoint(rho, theta) {
        return new Point(rho * Math.cos(theta), rho * Math.sin(theta));
    }
}
const point = PointFactory.newPolarPoint(5, Math.PI / 2);
const point2 = PointFactory.newCartesianPoint(5, 6);
console.log(point);
console.log(point2);

2. 抽象工廠

抽象工廠創(chuàng)建公共對象的族或組,而不指定它們的具體類。

抽象工廠模式提供了一種方式,可以將一組具有同一主題的單獨的工廠封裝起來。—— 維基百科

實例

將使用飲料和飲料制造機的示例。

class Drink {
    consume() {}
}
class Tea extends Drink {
    consume() {
        console.log("This is tea");
    }
}
class Coffee extends Drink {
    consume() {
        console.log("This is coffee");
    }
}
class DrinkFactory {
    prepare(amount) {}
}
class TeaFactory extends DrinkFactory {
    makeTea() {
        console.log("Tea Created");
        return new Tea();
    }
}
class CoffeeFactory extends DrinkFactory {
    makeCoffee() {
        console.log("Coffee Created");
        return new Coffee();
    }
}
const teaDrinkFactory = new TeaFactory();
const tea = teaDrinkFactory.makeTea();
tea.consume();
const coffeeDrinkFactory = new CoffeeFactory();
const coffee = coffeeDrinkFactory.makeCoffee();
coffee.consume();

3. 構(gòu)建器模式

構(gòu)建器模式從簡單對象構(gòu)造復(fù)雜對象。

又名:建造模式,是一種對象構(gòu)建模式。它可以將復(fù)雜對象的建造過程抽象出來(抽象類別),使這個抽象過程的不同實現(xiàn)方法可以構(gòu)造出不同表現(xiàn)(屬性)的對象。—— 維基百科

實例

將使用存儲 Person 信息的 person 類的 ab 示例。

class Person {
    constructor() {
        this.streetAddress = this.postcode = this.city = "";
        this.companyName = this.position = "";
        this.annualIncome = 0;
    }

    toString() {
        return `個人生活在 ${this.streetAddress},${this.city},${this.postcode} ,工作在 ${this.companyName} 作為一名 ${this.position} 收入 ${this.annualIncome}`;
    }
}

現(xiàn)在將創(chuàng)建 Person Builder、Person Job Builder 和 Person Address Builder。

class PersonBuilder {
    constructor(person = new Person()) {
        this.person = person;
    }

    get lives() {
        return new PersonAddressBuilder(this.person);
    }

    get works() {
        return new PersonJobBuilder(this.person);
    }
    build() {
        return this.person;
    }
}
class PersonJobBuilder extends PersonBuilder {
    constructor(person) {
        super(person);
    }
    at(companyName) {
        this.person.companyName = companyName;
        return this;
    }
    asA(position) {
        this.person.position = position;
        return this;
    }
    earning(annualIncome) {
        this.person.annualIncome = annualIncome;
        return this;
    }
}

class PersonAddressBuilder extends PersonBuilder {
    constructor(person) {
        super(person);
    }

    at(streetAddress) {
        this.person.streetAddress = streetAddress;
        return this;
    }

    withPostcode(postcode) {
        this.person.postcode = postcode;
        return this;
    }

    in(city) {
        this.person.city = city;
        return this;
    }
}

現(xiàn)在將使用上面定義的構(gòu)建器:

const personBuilder = new PersonBuilder();
const person = personBuilder.lives
    .at("高新南九道")
    .in("深圳")
    .withPostcode("518029")
    .works.at("字節(jié)跳動")
    .asA("工程師")
    .earning(10000)
    .build();
console.log(person.toString()); // 個人生活在 高新南九道,深圳,518029 ,工作在 字節(jié)跳動 作為一名 工程師 收入 10000

4. 原型模式

原型模式從現(xiàn)有對象創(chuàng)建新對象。

其特點在于通過“復(fù)制”一個已經(jīng)存在的實例來返回新的實例,而不是新建實例。被復(fù)制的實例就是我們所稱的“原型”,這個原型是可定制的。—— 維基百科

實例

使用汽車的例子:

class Car {
    constructor(name, model) {
        this.name = name;
        this.model = model;
    }
    setName(name) {
        console.log(name);
        this.name = name;
    }
    clone() {
        return new Car(this.name, this.model);
    }
}
const car = new Car();
car.setName("閃電");

const car2 = car.clone();
car2.setName("麥昆");

5. 單例模式

單例模式確保只為特定類創(chuàng)建一個對象。

在軟件工程中,單例模式是一種軟件設(shè)計模式,它將類的實例化限制為一個“單一”實例。當需要一個對象來協(xié)調(diào)整個系統(tǒng)的動作時,這很有用。 —— 維基百科

實例

創(chuàng)建一個單例類:

class Singleton {
    constructor() {
        const instance = this.constructor.instance;
        if (instance) {
            return instance;
        }
        this.constructor.instance = this;
    }

    say() {
        console.log("Saying……");
    }
}
const s1 = new Singleton();
const s2 = new Singleton();
console.log("Are they same?" + (s1 === s2));
s1.say();

到此這篇關(guān)于結(jié)合 ES6 類編寫JavaScript 創(chuàng)建型模式的文章就介紹到這了,更多相關(guān)JS 創(chuàng)建型模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

五常市| 德清县| 荔波县| 棋牌| 泰州市| 常德市| 海淀区| 武穴市| 新蔡县| 阿坝| 安陆市| 昆明市| 十堰市| 虎林市| 兴安盟| 六盘水市| 精河县| 本溪市| 蕉岭县| 迁西县| 西乌珠穆沁旗| 阳山县| 洛川县| 肇东市| 东台市| 庆云县| 都兰县| 维西| 镇原县| 渝北区| 白朗县| 阿克陶县| 若尔盖县| 马鞍山市| 南雄市| 雷州市| 彰武县| 固原市| 自治县| 休宁县| 拜城县|