Core rules of object-oriented design patterns_javascript skills
1. 单一职责
就一个类而言,应该仅有一个引起它变化的原因。
如果一个类承担的职责过多,就等于把这些职责耦合在一起,一个职责的变化可能会削弱或者抑制这个类完成其他职责的能力。这种耦合会导致脆弱的设计,当变化发生时,设计会遭到意想不到的破坏。
软件设计真正要做的许多内容,就是发现职责并把那些职责互相分离。如果你多于一个动机去改变一个类,那么这个类就具有多于一个的职责。
2. 开放封闭
软件实体(类,模块,函数等)应该可以扩展,但是不可修改。也就是说,对于扩展是开放的,对于更改是封闭的。
如此设计,面对需求的改变可以保持相对的稳定,从而使系统可以在第一个版本以后不断的推出新的版本。
无论模块是多么的'封闭',都会存在一些无法对之封闭的变化。既然不可能完全封闭,设计人员必须对于他设计的模块应该对哪种变化封闭做出选择。他必须先猜测出最有可能发生的变化种类,然后构造抽象来隔离那些变化。
等到变化发生时立即采取行动。
在我们最初编写代码时,假设变化不会发生。当变化发生时,我们就创建抽象来隔离以后发生的同类变化。
面对需求,对程序的改动是通过增加新代码进行的,而不是更改现有的代码。
我们希望的是在开发工作展开不久就知道可能发生的变化。查明可能发生的变化所等待的时间越长,要创建正确的抽象就越困难。
开放-封闭原则是面向对象设计的核心所在。遵循这个原则可以带来面向对象技术所声称的巨大好处,也就是可维护、可扩展、可复用、灵活性好。开发人员应该仅对程序中呈现出频繁变化的那些部分做出抽象,然而,对于应用程序中的每个部分都可以的进行抽象同样不是一个好主意。拒绝不成熟的抽象和抽象本身一样重要。
3. 依赖倒转
高层模块不应该依赖底层模块。两个都应该依赖抽象。
抽象不应该依赖细节,细节应该依赖抽象。
抽象不应该依赖细节,细节应该依赖于抽象,针对接口编程,不要对实现编程。
依赖倒转其实可以说是面向对象设计的标志,用哪种语言来写程序并不重要,如果编写时考虑的都是如何针对抽象编程而不是针对细节编程, 即程序中所有的依赖关系都终止于抽象类或者接口,那就是面向对象的设计,反之那就是过程化的设计了。
4. 里氏代换
一个软件实体如果使用的是一个父类的话,那么一定适用于其子类,而且察觉不出父类对象与子类对象的区别。也就是说,在软件里面,把父类都替换成它的子类,程序的行为没有变化。
子类型必须能用替换掉它们的父类型。
只有当子类可以替换掉父类,软件单位的功能不受到影响时,父类才能真正被复用,而子类也能够在父类的基础上增加新的行为。
5. 合成/聚合复用
尽量使用合成/聚合,尽量不要使用类继承。
优先使用对象的合成/聚合将有助于你保持每个类被封装并被集中在单个任务上,这样累和类继承层次会保持较小的规模,并且不大可能增长为不可控制的庞然大物。
6. 迪米特法则
如果两个类不必彼此直接通信,那么着两个类就不应当发生直接的相互作用。如果其中一个类需要调用另一个类的某一个方法的话,可以通过第三者转发这个调用。
在类的结构设计上,每一个类都应当尽量降低成员的访问权限,也就是说,一个类包装好自己的private状态,不需要让别的类知道的字段或行为就不要公开。
迪米特法则其根本思想是强调了类之间的松耦合。
类之间的耦合越弱,越有利于复用,一个处在弱耦合的类被修改,不会对有关系的类造成波及。
辅助资料:
常用创建型设计模式(其他类型模式就不提了,自己看书)
创建型模式隐藏了这些类的实例是如何被创建和放在一起,整个系统关于这些对象所知道的是由抽象类所定义的接口。这样,创建型模式在创建了什么、谁创建它=它是怎么被创建的,以及何时创建这些方面提供了很大的灵活性。
1. 工厂方法模式(Factory Method)
Define an interface for creating objects and let subclasses decide which class to instantiate. The factory pattern delays the instantiation of a class to its subclasses.
Creational patterns abstract the process of instantiation. They help a system be independent of how its objects are created, composed, and represented. Creational patterns encapsulate information about which specific classes the system uses. Allows customers to configure a system with 'product' objects that vary widely in structure and functionality. Configuration can be static, that is, specified at compile time, or dynamic, that is, specified at runtime.
Usually the design should start from the factory method. When the designer finds that greater flexibility is needed, the design will evolve to other creational patterns. Understanding multiple creational patterns can give designers more options when making trade-offs between design criteria.
2. Abstract Factory pattern (Abstract Factory)
Provides an interface for creating a series of or related dependent objects without specifying their specific classes.
3. Builder mode (Builder)
Separate the construction of a complex object from its representation, so that the same construction process can create different representations.
Cohesion and Coupling Cohesion describes how closely the internal components of a routine are interconnected. Coupling describes how closely a routine is connected to other routines. The goal of software development should be to create routines that are internally complete, that is, highly cohesive, and whose connections to other routines are small, direct, visible, and flexible, that is, loosely coupled.
Separate the construction of a complex object from its representation, making it easy to change the internal representation of a product and keeping construction code separate from presentation code. In this way, customers do not need to care about the product creation process, but as long as they tell me what they need, I can use the same construction process to create different products for customers.
4. Prototype
Specify the types of objects to be created using instances of prototypes, and create new objects by copying these prototypes.
Creating a number of dependent prototypes and cloning them is often more convenient than manually instantiating the class with the appropriate state each time.
5. Singleton
Guarantee that a class has only one instance and provide a global access point to it.
For some classes, an instance is very important. A global variable enables an object to be accessed, but it does not prevent clients from instantiating multiple objects. The advantage of a singleton is that the class itself is responsible for saving its only instance. This class guarantees that no other instance can be created, and the singleton also provides a method to access the instance. This allows a unique instance to be tightly controlled on how and when clients access it.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.

JavaScript does not require installation because it is already built into modern browsers. You just need a text editor and a browser to get started. 1) In the browser environment, run it by embedding the HTML file through tags. 2) In the Node.js environment, after downloading and installing Node.js, run the JavaScript file through the command line.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.