Design Patterns Flashcards
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.
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?
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 this pattern?
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?
Billion Dollar Mistake:
Why is it often said that the introduction of null is a “billion-dollar mistake”? Would you discuss the techniques to avoid it, such as the Null Object Pattern introduced by the GOF book, or Option types?
Inheritance vs Composition:
Many states that, in Object-Oriented Programming, the composition is often a better option than inheritance. What’s your 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 the 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.
Dependency Hell:
How would you deal with Dependency Hell?
Goto is Evil:
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?
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?