arialdomartiniUpdated Feb 18, 2024

Questions about Design Patterns: 关于设计模式的问题:

Globals Are Evil 全局变量是邪恶的

Why are global and static objects evil? Can you show it with a code example?

Inversion of Control 控制反转

Tell me about Inversion of Control and how it improves the design of code.
请谈谈控制反转以及它如何改进代码设计。Resources 资源

Law of Demeter 得墨忒耳定律

The Law of Demeter (the Principle of Least Knowledge) states that each unit should have only limited knowledge about other units and it should only talk to its immediate friends (sometimes stated as "don't talk to strangers").
Would you write code violating this principle, show why it is a bad design and then fix it?
你会写违反这个原则的代码,说明为什么这是一个糟糕的设计,然后修复它吗?Resources 资源

Active-Record 活动记录

Active-Record is the design pattern that promotes objects to include functions such as Insert, Update, and Delete, and properties that correspond to the columns in some underlying database table. In your opinion and experience, which are the limits and pitfalls of the this pattern?
Active-Record 是一种设计模式,它提升对象包括“插入”、“更新”和“删除”等函数,以及与某些基础数据库表中的列相对应的属性。根据您的观点和经验,这种模式的局限性和陷阱是什么?

Data-Mapper 数据映射器

Data-Mapper is a design pattern that promotes the use of a layer of Mappers that moves data between objects and a database while keeping them independent of each other and the mapper itself. On the contrary, in Active-Record objects directly incorporate operations for persisting themselves to a database, and properties corresponding to the underlying database tables. Do you have an opinion on those patterns? When would you use one instead of the other?
Data-Mapper 是一种设计模式,它促进了映射层的使用,该层在对象和数据库之间移动数据,同时使它们彼此独立,并保持映射器本身的独立性。相反,在 Active-Record 中,对象直接包含用于将自身持久化到数据库的操作,以及与基础数据库表相对应的属性。你对这些模式有什么看法吗?你什么时候会使用一个而不是另一个?

Billion Dollar Mistake 十亿美元错误

Tony Hoare who invented the null reference once said "I call it my billion-dollar mistake" since it led to "innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years".
发明空引用的托尼·霍尔(Tony Hoare)曾经说过“我称之为我的十亿美元错误”,因为它导致了“无数的错误、漏洞和系统崩溃,这些错误、漏洞和系统崩溃在过去四十年中可能造成了十亿美元的痛苦和损害”。
Would you discuss the techniques to avoid it, such as the Null Object Pattern introduced by the GOF book, or Option types?
您能否讨论一下避免这种情况的技术,例如 GOF 书中引入的 Null 对象模式或 Option 类型?

Inheritance vs Composition 继承与组合

Many state that, in Object-Oriented Programming, composition is often a better option than inheritance. What's you opinion?许多人指出,在面向对象编程中,组合通常是比继承更好的选择。你怎么看?

Anti-Corruption Layer 防腐层

What is an Anti-corruption Layer?

Singleton 单例

Singleton is a design pattern that restricts the instantiation of a class to one single object. Writing a Thread-Safe Singleton class is not so obvious. Would you try?

Data Abstraction 数据抽象

The ability to change implementation without affecting clients is called Data Abstraction. Produce an example violating this property, then fix it.在不影响客户端的情况下更改实现的能力称为数据抽象。生成一个违反此属性的示例,然后修复它。

Don't Repeat Yourself 不要重复自己

Write a snippet of code violating the Don't Repeat Yourself (DRY) principle. Then, fix it.
编写一段违反“不要重复自己”(DRY) 原则的代码片段。然后,修复它。

Dependency Hell 依赖地狱

How would you deal with Dependency Hell?

Goto is Evil Goto是邪恶的

Is goto evil? You may have heard of the famous paper "Go To Statement Considered Harmful" by Edsger Dijkstra, in which he criticized the use of the goto statement and advocated structured programming instead. The use of goto has always been controversial, so much that even Dijkstra's letter was criticized with articles such as "'GOTO Considered Harmful' Considered Harmful". What's your opinion on the use of goto?
goto是邪恶的吗?你可能听说过 Edsger Dijkstra 的著名论文“Go To Statement Considered Harmful”,他在论文中批评了该 goto 语句的使用,并提倡结构化编程。的使用 goto 一直存在争议,以至于甚至 Dijkstra 的信也受到诸如“'GOTO 被认为是有害的'被认为是有害的”之类的文章的批评。您对 的用途有什么意见 goto ?

Robustness Principle 鲁棒性原则

The robustness principle is a general design guideline for software that recommends "be conservative in what you send, be liberal in what you accept". It is often reworded as "be a tolerant reader and a careful writer". Would you like to discuss the rationale of this principle?

Separation of Concerns 关注点分离

Separation of Concerns is a design principle for separating a computer program into distinct areas, each one addressing a separate concern. There are a lot of different mechanisms for achieving Separation of Concerns (use of objects, functions, modules, or patterns such as MVC and the like). Would you discuss this topic?
关注点分离是一种设计原则,用于将计算机程序划分为不同的区域,每个区域解决一个单独的关注点。有许多不同的机制可以实现关注点分离(使用对象、函数、模块或模式,如 MVC 等)。你会讨论这个话题吗?
LeetCode 刷题记录使用 HWID2 激活 Windows 系统