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

削除された内容 追加された内容
Yak! (トーク | 投稿記録)
現状態を翻訳。
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.
オブジェクト指向プログラミングに対して、分離の原理的な機構がインタフェースクラス(Interface Class)である。
しかし、C++ では(例えば Java などと比較して)、そのような分離を表現する専用の機構を提供していない。
Java では、'''interface''' キーワードが、抽象が持つべき public なメソッドのみを定めるために使われる。
C++ はそのようなキーワードを持たないが、インタフェースクラス(Interface Class)イディオムを使うことで、その機能性を大体表現することができる。
抽象の public なメソッドのみを表現し、実装を一切提供しないという発想である。
実装の欠如は、インタフェースクラスのインスタンスが存在できないということも意味する。
 
=== 解法とサンプルコード ===
インタフェースクラスは、仮想デストラクタと、純粋仮想関数のみを含む。
これにより、他の言語(例えば Java)の interface と似た構成を提供する。
インタフェースクラスは、例えば基本クラス中の純粋仮想関数宣言のような多態的なインタフェースを定めるクラスである。
クラス階層を使うプログラマは、基本クラスを通じて、階層中のクラスのインタフェースのみとやりとりすることでクラス階層を使うことができる。
<source lang="cpp">
class shape // An interface classインタフェースクラス
{
public:
36 ⟶ 46行目:
{
std::vector<shape *> shapes;
// Fillどうにかして upshape shapes vector somehow.を埋める。
for (vector<shape *>::iterator iter (shapes.begin());
iter != shapes.end();
45 ⟶ 55行目:
}
</source>
全てのインタフェースクラスは仮想デストラクタを持つべきである。
Every interface class should have a virtual destructor. Virtual destructor makes sure that when a shape is deleted polymorphically, correct destructor of the derived class is invoked. Otherwise there is a good chance of resource leak. Benefit of expressing design using interface classes are many:
仮想デストラクタにより、shape が多態的に delete されたときに、派生クラスの正しいデストラクタが呼び出されることが保証される。
* New shape abstractions can be added without changing the code that depends only on shape interface. For example, Square can inherit from shape and implement the interface methods in its own way. The function main() needs no changes at all.
さもなくば、リソースリーク(解放漏れ)の好機となる。
* Separation of interface from implementation prevents recompilation of parts of the program that depend only on the interface classes.
インタフェースクラスを使って設計を表現することによる利益はたくさんある。
* 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.
* 新しい shape の抽象を、shape インタフェースのみに依存しているコードを変更することなく追加できる。例えば、square を shape から継承して、独自の方法でインタフェースのメソッドを実装することができる。main() 関数は全く変更する必要がない。
* 実装からインタフェースを分離することで、インタフェースクラスにのみ依存しているプログラムの部分を再コンパイルしなくてすむ。
* 実装クラスは互いに依存してはならないと依存関係逆転の原則(Dependency Inversion Principle(DIP))は主張する。その代わり、インタフェースクラスによって表現される共通の抽象に依存するべきである。DIP はオブジェクト指向システムの結合(coupling)を削減する。
 
=== Known Uses既知の利用 ===
ほとんど全ての良い C++ オブジェクト指向ソフトウェア
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]
<noinclude>
[[Category:{{BASEPAGENAME}}|{{SUBPAGENAME}}]]
[[en:More C++ Idioms/Interface Class]]
</noinclude>
[[Category:{{BASEPAGENAME}}|{{SUBPAGENAME}}いんたふえーすくらす]]