
作者:天津九安特機電工程有限公司 來(lái)源: 天津九安特機電工程有限公司 日期:2026-05-05 09:23:49
JavaScript中常見(jiàn)的中常種設十五種設計模式
在編程過(guò)程中,我們ヽ(′ー`)ノ經(jīng)常會(huì )遇到一些重復的見(jiàn)的計模問(wèn)題,這些問(wèn)題往往可以通過(guò)使用設計模式來(lái)解決,中常種設設計模式是見(jiàn)的計模一種在特定環(huán)??境下解決特定問(wèn)題的通用可重(′?`)用方案,本文將介紹15種(zhong)在JavaSc??ript??開(kāi)發(fā)中常見(jiàn)的中常種設設計模式,幫助你更好地理解和應用這些設計模式。(′?_?`)見(jiàn)的計模
1、中常種設單??例模式??(Singleton)
單例模式是見(jiàn)的計模一種( ?▽?)創(chuàng )建型設計模式,它保證一個(gè)類(lèi)只有一個(gè)實(shí)例,中常種設并提供一個(gè)全局訪(fǎng)問(wèn)點(diǎn),見(jiàn)的計模在JavaScript中,中常種設可以通過(guò)以下方式實(shí)現單例??模式:
class Singleton { constructor() { if (!Singleton.instance??) { Singleton.instance = this; } return Singleton.instance; }}2、見(jiàn)的計模工廠(chǎng)模式(Factory)
工廠(chǎng)模式是中常種設一種創(chuàng )建型設計模式,它提供了一種創(chuàng )建對象的見(jiàn)的(de)計模最佳方式,在JavaScript中,(′?`)中常種設可以通過(guò)以下方式實(shí)現工廠(chǎng)模式:
class Product { constructor(name) { this.name = name; }}const factory = new func??tion??() { this.createProduct = function(type) { switch (type) { case 'A': return new Product('A'); case 'B': return new Product('B'); default:?? throw new Error('Invalid product type'); } };};3、抽象工廠(chǎng)模式(Abstract Factory)
抽象工廠(chǎng)模式是一種創(chuàng )建型設計模式,它提供了一種創(chuàng )建一系(╥_╥)列相關(guān)或相互依賴(lài)對象的接口,而不需要指定它們具體的類(lèi),在JavaScript中,可以通過(guò)以下方式實(shí)現抽象工廠(chǎng)模式:
class AbstractFactory { createProductA() { } createProductB(′▽?zhuān)?)() { }}class ConcreteFactory extends AbstractFactory { creatヽ(′▽?zhuān)?ノeProductA() { return new ProductA(); } createProductB() { retu?????rn new ProductB(); }}4、建造者模式(Builder)
建造者┐(′ー`)┌模式是一種創(chuàng )建型設計模式,它可以將一個(gè)復雜對象的構建與其表示分離,使得同樣的構建過(guò)程可以創(chuàng )建不同的表示,在JavaScript中,可以通過(guò)以下方式實(shí)現建造者模式:
class?? ProductBuilder { addPartA() { } addPartB() { }}class Director { constructor(builder) { } construct() { }}5、原型鏈模式(Prototype Chain)
原型鏈模式是一種結構型設計模式??,它允許你繼承一個(gè)對象的屬性和方法,在JavaScrip??t中,對象通過(guò)引用其他對象來(lái)繼承屬性和方法:
function Person() { }Person.prototype.sayHello = funct(╯‵□′)╯ion() { };const?? person = new Perso??n(); // person.__??proto__ === Person.prototype; // true6、適配器(qi)模式(Adapter)
適配器模式是(╯‵□′)╯(shi)一種結構型設計模式,它將一個(gè)類(lèi)??ヽ(′ー`)ノ的接口轉換成客戶(hù)希望的另一個(gè)接口,在JavaScript中,可以通過(guò)以下方式實(shí)現適配器模式:
class TargetInterface { }class SourceInterface { }class??? TargetAdapter extends TargetInterface implements SourceInterface { } // TargetAdapter isヽ(′ー`)ノ an adapter between TargetInterface and SourceInterface. It implements both interfaces. It can be used to ada(╬ ò﹏ó)pt a source object to work with a target interface or vice versa. The client of the program does not need to be aware that it is using an adapter because the methods and properties of the adapter are implemented in terms of those of the target interface. The only difference is that the methods and propertie??s of the adap(′▽?zhuān)?ter are wrapped in a try-catch block that handles any exceptions thrown by the?? target meth??ods. This allows the client to?? use the adapter as if it were a regular object without needing to know anything about the internals of the adapter(′?_?`)."( ???) />', // TargetAdapter is an adapter betwee??n TargetInterface and SourceInterface. It?? implem(′?_?`)ents both interfaces. It can be used to adapt a source object to work with a target interface or vice versa. The client of the program does not need to be aware that it is using an adapter because the methods and properties of the adapter are implemented in terms of those of the target interface. The only differ( ?▽?)ence is that the methods and properties of the adapter are wra??pped in a try-cat(╯‵□′)╯ch block that handles any exceptions thrown by the target methods. This allows the client to use the ada(′▽?zhuān)?)pter as if it were a regular object without needing to know anything about the internals of the adapter." />', // TargetAdapter is an adapte(′;д;`)r between TargetInterヽ(′ー`)ノface and SourceInterface. It imp(╯‵□′)╯lements both interfa?????ces. It can be used to adapt a source object to(???) work with a target interface or vice versa. The client of the program does not need to be aware that it is using an adapter because the method?s?? and properties of the adapter are impleme??nted in terms of those of the target interface. The only difference is that the methods and properties of the adapter are wrapped in a try-catch block that handles any exceptions thrown by the target methods. This allows the client to use the adapter as if it were a regular object without needing to know anything about the internals of the adapter." />', // TargetAdapter is an adaヽ(′?`)ノpter between TargetInterface and SourceInterface. It implements both interfaces. It can be used to adapt a sourc(╬?益?)e object to work with a targ(????)et interf??ace or vice versa. The client of the program does not need to be aware that it is usin(′?`*)g an adapter because thヽ(′ー`)ノe methods and properties of the adapter are implemented in terms of those of?? the target interface. The only difference is that the methods and properties of the adapter are wrapped in a try-catch blヾ(′?`)?ock that handles any exceptions thrown by the target methods. This allows the clien??t to use the adapter as if it were a regular object without needing to know anything about the internals of the adapter." />', // TargetAdapter is an adapter between Ta(?_?;)rgetInterface and SourceInterface. It implements both interfaces. It can be used to adapt a source object to work with a target interface(′▽?zhuān)? or vice versa. The client of the program does not need to be aware that it is using an adapter because the met??hods and properties of the adapter are implemented in terms of those of the target interface. The only dif(′?_?`)ference is th(′▽?zhuān)?)at the methods and properties of the ada(′?`*)pter are wrapped in a try-catch block that handles any exc??eヽ(′▽?zhuān)?ノptions throw??n by the target methods. This allows th(′?`)e client to use th??e adapter as if it were?? a regular object without needing to know?? anything about the internals of the adapter." /&??gt;', // TargetAdapter is an adapter between TargetInterface and SourceInterface. It impleme(′_`)nts both interfaces. It canヾ(′?`)? be used to adapt a source object?? to work with a target interface or vice vers??a. The client of the program does not need to be aware thaヾ(′?`)?t it is using an adapter because the methods and properties of the adapter are implemented in terms of those of the target interface. The only difference is that