C++ Buzz Phrase Answers
by Curtis Krauskopf
Buzz phrases show how well-read a C++ candidate is. The diversity of knowledge and wisdom embedded in any of these buzz phrases separates the advanced C++ programming candidates from the intermediate or beginner levels. Knowing when to apply these concepts, and equally important, knowing when to break the rules provided by these concepts, shows your ability to handle large C++ design and development projects.
A1) In C++, the Law of Demeter is a guideline for developing class hierarchies. The guideline states that a method inside of an object should only call the methods of:
- The object itself
- The objects passed as parameters to the method
- Objects instantiated within the method
- Objects that exist due to has-a relationships within the object
The purpose of the Law of Demeter is to reduce the dependencies between classes. Obeying the law requires the coder to create many wrapper methods that separate callees from callers.
A2) The Liskov Substitution Principle guarantees that code using a pointer to a base class doesn't break when a new class is added to the class hierarchy. One main principle provide this guarantee: the methods for subclasses are substitutable for the methods in the superclasses for instantiated objects.
Violating the principle requires subclasses (or modified superclasses) to check the object's state or the type of the subclass to determine how to dereference a request. Most commonly, this manifests itself in code with switch or if statements similar to:
switch (param.type) {
case Integer:
retval = param.toInt();
break;
case Double:
retval = param.toDouble();
...
A3) Dependency Inversion abstracts dependencies into subclasses so that the superclasses become so generic that they no longer have interdependencies.
See
Robert Martin's article for more details.
A4) The Visitor Pattern separates an algorithm from an object's structure. It can perform an operation (or groups of operations) on objects that belong to a structure. The objects are not required to have the same interfaces.
A5) The Gang of Four are Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides. They collectively became famous for their seminal book "Design patterns : elements of reusable object-oriented software", Addison-Wesley (ISBN 0201633612 -- no, I don't expect you to memorize the ISBN number -- I'm providing it here in case you want to get the book).
A6) A Bridge Pattern separates an abstraction from its implementation so that they can change independently. Some examples of when a Bridge Pattern can be used are:
- A reference counting mechanism that is invisible to the clients being counted.
- When changes to a class hierarchy cause an exponential increase in the number of subclasses or superclasses.
- When interfacing with a legacy system that can not be touched but must be extended.
|