web观察者模式如何实现_Web开发应了解的4种设计模式:观察者,单例,策略和装饰器

web观察者模式如何实现

Have you ever been on a team where you need to start a project from scratch? That's usually the case in many start-ups and other small companies.

您是否曾经在一个团队中需要从头开始一个项目? 在许多初创公司和其他小型公司中通常就是这种情况。

There are so many different programming languages, architectures, and other concerns that it can be difficult to figure out where to start. That's where design patterns come in.

有太多不同的编程语言,体系结构和其他问题,以至于很难从哪里开始。 那就是设计模式出现的地方。

A design pattern is like a template for your project. It uses certain conventions and you can expect a specific kind of behavior from it. These patterns were made up of many developers' experiences so they are really like different sets of best practices.

设计模式就像您的项目的模板。 它使用某些约定,您可以从中期望一种特定的行为。 这些模式由许多开发人员的经验组成,因此它们实际上就像是不同的最佳实践集。

And you and your team get to decide which set of best practices is the most useful for your project. Based on the design pattern you choose, you all will start to have expectations for what the code should be doing and what vocabulary you all will be using.

您和您的团队可以决定哪种最佳实践对您的项目最有用。 根据您选择的设计模式,所有人都会开始对代码应该做什么以及所有人将使用的词汇有期望。

Programming design patterns can be used across all programming languages and can be used to fit any project because they only give you a general outline of a solution.

编程设计模式可以在所有编程语言中使用,并且可以用于任何项目,因为它们仅为您提供解决方案的一般概述。

There are 23 official patterns from the book Design Patterns - Elements of Reusable Object-Oriented Software, which is considered one of the most influential books on object-oriented theory and software development.

设计模式-可重用的面向对象软件的元素 》一书中有23种正式模式,它们被认为是有关面向对象理论和软件开发的最具影响力的书之一。

In this article, I'm going to cover four of those design patterns just to give you some insight to what a few of the patterns are and when you would use them.

在本文中,我将介绍其中的四种设计模式,以使您对其中的几种模式以及何时使用它们有一些了解。

单例设计模式 (The Singleton Design Pattern)

The singleton pattern only allows a class or object to have a single instance and it uses a global variable to store that instance. You can use lazy loading to make sure that there is only one instance of the class because it will only create the class when you need it.

单例模式仅允许类或对象具有单个实例,并且它使用全局变量来存储该实例。 您可以使用延迟加载来确保该类只有一个实例,因为它只会在需要时创建该类。

That prevents multiple instances from being active at the same time which could cause weird bugs. Most of the time this gets implemented in the constructor. The goal of the singleton pattern is typically to regulate the global state of an application.

这样可以防止同时激活多个实例,这可能会导致奇怪的错误。 大多数情况下,这是在构造函数中实现的。 单例模式的目标通常是调节应用程序的全局状态。

An example of a singleton that you probably use all the time is your logger.

您可能一直使用的单例示例是记录器。

If you work with some of the front-end frameworks like React or Angular, you know all about how tricky it can be to handle logs coming from multiple components. This is a great example of singletons in action because you never want more than one instance of a logger object, especially if you're using some kind of error tracking tool.

如果您使用诸如React或Angular之类的某些前端框架,那么您将了解处理来自多个组件的日志有多么棘手。 这是一个很好的单例实例,因为您永远不需要一个记录器对象的实例,尤其是在使用某种错误跟踪工具的情况下。

Now you don't have to worry about losing logs from multiple instances because you only have one in your project. So when you want to log the food that has been ordered, you can use the same FoodLogger instance across multiple files or components.

现在,您不必担心会丢失多个实例中的日志,因为您的项目中只有一个实例。 因此,当您要记录已订购的食物时,可以在多个文件或组件中使用相同的FoodLogger实例。

With this singleton pattern in place, you don't have to worry about just getting the logs from the main application file. You can get them from anywhere in your code base and they will all go to the exact same instance of the logger, which means none of your logs should get lost due to new instances.

有了这种单例模式,您不必担心仅从主应用程序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值