Questions About Design Patterns Flashcards
Why are global and static objects evil? Can you show it with a code example?
Tell me about Inversion of Control and how it improves the design of code.
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 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 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, 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?
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?
Many state that, in Object-Oriented Programming, composition is often a better option than inheritance. What’s your opinion?
What is an Anti-corruption Layer?
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?
The ability to change implementation without affecting clients is called Data Abstraction. Produce an example violating this property, then fix it.
Write a snippet of code violating the Don’t Repeat Yourself (DRY) principle. Then, fix it.
How would you deal with Dependency Hell?
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?
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 re-worded as “be a tolerant reader and a careful writer”. Would you like to discuss the rationale of this principle?
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?