結(jié)合?ES6?類編寫JavaScript?創(chuàng)建型模式
前言
什么是設(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)建公共對象的族或組,而不指定它們的具體類。
抽象工廠模式提供了一種方式,可以將一組具有同一主題的單獨(dú)的工廠封裝起來。—— 維基百科
實例
將使用飲料和飲料制造機(jī)的示例。
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è)計模式,它將類的實例化限制為一個“單一”實例。當(dāng)需要一個對象來協(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)文章
BootstrapTable refresh 方法使用實例簡單介紹
本文就bootstrapTable refresh 方法如何傳遞參數(shù)做簡單舉例說明,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-02-02給事件響應(yīng)函數(shù)傳參數(shù)的四種方式小結(jié)
這篇文章主要介紹了給事件響應(yīng)函數(shù)傳參數(shù)的四種方式。需要的朋友可以過來參考下,希望對大家有所幫助2013-12-12JavaScript中的動態(tài)?import()用法示例解析
這篇文章主要為大家介紹了JavaScript中的動態(tài)import()用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04詳解微信小程序-掃一掃 wx.scanCode() 掃碼大變身
這篇文章主要介紹了微信小程序-掃一掃wx.scanCode() 掃碼大變身,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04