- Mastering JavaScript Design Patterns
- Simon Timms
- 340字
- 2021-08-05 17:14:58
Chapter 3. Creational Patterns
In the last chapter, we took a long look at how to fashion a class. In this chapter, we'll look at how to create instances of classes. On the surface it seems like a simple concern, but how we create instances of a class can be of great importance.
We take great pains in creating our code such that it can be as decoupled as much aspossible. Ensuring that classes have minimal dependence on other classes is the key to building a system that can change fluently with the changing needs of those using the software. Allowing classes to be too closely related means that changes ripple through them.
One ripple isn't a huge problem but as you throw more and more changes into the mix, the ripples add up and create interference patterns. Soon the once placid surface is an unrecognizable mess of additive and destructive nodes. This problem also occurs in our applications: the changes magnify and interact in unexpected ways. One situation in which we tend to forget about coupling is when creating objects as can be seen in the following code:
var Westeros; (function (Westeros) { var Ruler = (function () { function Ruler() { this.house = new Westeros.Houses.Targaryen(); } return Ruler; })(); Westeros.Ruler = Ruler; })(Westeros || (Westeros = {}));
You can see in this class that the Ruler
variable's house is strongly coupled to the Targaryen
class. If this were ever to change, then this tight coupling would have to change in a great number of places. This chapter discusses a number of patterns, which were originally presented in the Gang of Four book, Design Patterns: Elements of Reusable Object-Oriented Software, Addison-Wesley. The goal of these patterns is to improve the degree of coupling in applications and increase the opportunities for code reuse.
The patterns are as follows:
- Abstract Factory
- Builder
- Factory Method
- Singleton
- Prototype
Of course not all of these are applicable to JavaScript, but we'll see all about that as we work through the creational patterns.
- 微服務(wù)與事件驅(qū)動架構(gòu)
- Windows系統(tǒng)管理與服務(wù)配置
- Spring Boot+Spring Cloud+Vue+Element項(xiàng)目實(shí)戰(zhàn):手把手教你開發(fā)權(quán)限管理系統(tǒng)
- C/C++常用算法手冊(第3版)
- Cassandra Data Modeling and Analysis
- Web全棧工程師的自我修養(yǎng)
- C語言程序設(shè)計(jì)
- Python爬蟲、數(shù)據(jù)分析與可視化:工具詳解與案例實(shí)戰(zhàn)
- Microsoft 365 Certified Fundamentals MS-900 Exam Guide
- HoloLens與混合現(xiàn)實(shí)開發(fā)
- 計(jì)算機(jī)應(yīng)用基礎(chǔ)項(xiàng)目化教程
- Simulation for Data Science with R
- 軟件測試分析與實(shí)踐
- ASP.NET開發(fā)寶典
- Java多線程并發(fā)體系實(shí)戰(zhàn)(微課視頻版)