「More C++ Idioms/インタフェースクラス(Interface Class)」の版間の差分

削除された内容 追加された内容
編集の要約なし
1 行
=<center>Interface Class</center>=
=== Intent ===
* To separate an interface of a class from its implementation.
* Invoke implementation of an abstraction/class using runtime polymorphism.
=== Also Known As ===
 
=== Motivation ===
Separating an interface of a class from its implementation is fundamental to good quality object oriented software design/programming. For object oriented programming, the principle mechanism of separation is the Interface Class. However C++ (when compared to, say, Java) provides no exclusive mechanism for expressing such a separation. In Java, '''interface''' keyword is used to specify only the public methods that are supported by an abstraction. C++ does not have such a keyword but its functionality can be expressed closely using the Interface Class idiom. The idea is to express only the public methods of an abstraction and provide no implementation of any of them. Also, lack of implementation means no instances of the interface class should be allowed.
 
=== Solution and Sample Code ===
An interface class contains only a virtual destructor and pure virtual functions, thus providing a construct similar to the interface constructs of other languages (e.g. Java). An interface class is a class that specifies the polymorphic interface i.e. pure virtual function declarations into a base class. The programmer using a class hierarchy can then do so via a base class that communicates only the interface of classes in the hierarchy.
<source lang="cpp">
49 ⟶ 50行目:
* Dependency Inversion Principle (DIP) states that implementation classes should not depend on each other. Instead, they should depend on common abstraction represented using an interface class. DIP reduces coupling in a object-oriented system.
 
=== Known Uses ===
Nearly all good object-oriented software in C++!
 
=== Related Idioms ===
* [[More C++ Idioms/Capability Query|Capability Query]]
* [[More C++ Idioms/Inner Class|Inner Class]]
 
=== References ===
[http://www.twonine.co.uk/articles/CPPInterfaceClassesIntro.pdf C++ Interface Classes - An Introduction]
[[Category:{{BASEPAGENAME}}|{{SUBPAGENAME}}]]