Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
roth_stephan_clean_c20_sustainable_software_development_patt-1.pdf
Скачиваний:
25
Добавлен:
27.03.2023
Размер:
7.26 Mб
Скачать

Chapter 5 Advanced Concepts of Modern C++

Interface Design

“Since changing interfaces breaks clients, you should consider them as immutable once you’ve published them.”

—Erich Gamma, Design Principles from Design Patterns, 2005

In our daily work as software craftspeople, we are constantly confronted with interfaces, either because we have to use them (e.g., from a library), or because we have to design them (e.g., when creating a class or a module). Probably one of the most demanding tasks in software design is to design good interfaces and APIs. But what makes a “good interface”?

Well, in previous chapters, you learned some principles and practices that can help you create well-designed interfaces:

•\ Easy to use, even without documentation. Think about the KISS principle from Chapter 3. An interface should not be too complicated. Furthermore, a good and expressive naming is

important; if an interface is hard to name, that’s generally a bad sign. Good names also make it easier to learn an interface. The API can quickly be memorized by developers who work with it constantly.

•\ Users of an interface/API should not be surprised by unexpected behavior. Avoid unexpected side effects! Think about the principle of least astonishment, discussed in Chapter 3.

•\ An interface should be as small as possible. Do not offer more services than necessary. You won’t be able to please everyone anyway. You can always add something, but you can never remove it! If something has to be added, it should be done in a way that existing parts of the interface are not changed.

•\ A well-designed interface/API hides the implementation.

Changes in the implementation of a software module should not be propagated outside via its interface. Think about the information hiding principle from Chapter 3. Make classes and their members as private as possible, because it fosters loose coupling.

209