「More C++ Idioms/多態的例外(Polymorphic Exception)」の版間の差分

削除された内容 追加された内容
編集の要約なし
7 行
 
=== Motivation ===
[http://en.wikipedia.org/wiki/Dependency_inversion_principle Dependency Inversion Principle (DIP)], a popular object-oriented software design guideline states that higher level modules should not depend directly on lower level modules. Instead, both should depend on common abstractions (captured in the form of well-defined interfaces). For example, an object of type ''Person'' (say John) should not create and use an object of type ''HondaCivic'' but instead John should simply commit to a ''Car'' class, which is a base class of ''HondaCivic''. This allows John to upgrade to a ''Corvette'' easily in future without any changes to the class ''Person''. John can be "configured" with a concrete instance of a car (any car) using [http://en.wikipedia.org/wiki/Dependency_injection dependency injection] technique. Use of DIP leads to flexible and extensible modules that are easy to unit test. Unit testing is simplified using DIP because real objects can be easily replaced with mock objects using dependency injection.
 
However, there are several places where DIP is violated: (1) using the Singleton pattern and (2) throwing exceptions! Singleton pattern breaks DIP because it forces the use of the concrete class name while accessing the static ''instance()'' function. A singleton should be passed as a parameter while calling a function or a constructor. A similar situation arises while dealing with exceptions in C++. The ''throw'' clause in C++ requires a concrete type name (class) to raise an exception. For example,