Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.
For the best experience please use the latest Chrome, Safari or Firefox browser.
Software Component
- Locus of computation and state in a system
- Reusable unit of composition
Component Interface
- How to reuse (compose) a component?
Provided Interface
- Specify and document the externally visible features (or the public API) offered by the component
- Data Types
- Define the component data model
- Operations
- Call functionality, modify state
- Properties
- Read visible state attributes
- Events
- “Call-backs” – Notify about state changes
Required Interface
- A component can be reused only if its required interface is satisfied
- The platform is compatible:
- Runtime Libraries/Framework
- Operating System/Device Drivers
- Hardware
- The environment is setup correctly:
- Databases
- Configuration Files
- File System Directories
Information Hiding
Which part is easier to change?
Information Hiding
D. Parnas
- Always keep the implementation details secret (invisible from the outside) and only access them through the public interface
- Makes it possible to replace components (as long as they share the same interface)
- Ensures component decoupling
Effective Encapsulation
G. Fairbanks and D. Parnas
- Reduce the cognitive burden for using a component
- Well designed interfaces are simple to understand
- Every interface should hide one secret (avoid leakage: do not reveal unnecessary details)
- Changes to the implementation hidden behind an interface do not affect clients
- Difficult to hide the impact of all changes
- How to choose what should be hidden?
Example
- Pre-condition: unsorted content
- Post-conditon: sorted content
- Hidden details:
- What sorting algorithm is used?
- Is the sorting algorithm stable?
- What if the content is already sorted?
- What data structure stores the content?
- Can I provide my own comparator strategy?
What details are hidden behind this interface?
Describing Interfaces
Interface Description Languages
IDL Example: Java/RMI
import java.rmi.*;
public interface Adder extends Remote
{
public int sum(int x, int y) throws RemoteException;
}
IDL Example: C/RPC
interface adder {
const long ARRAY_SIZE = 10;
typedef long array_long[ARRAY_SIZE];
long sum([in] long a, [in] long b);
void array_sum([in] array_long a,
[in] array_long b,
[out] array_long c);
}
Parameter direction: [in]
Input, [out]
Output,
[in, out]
Input and Output
Working With IDL
Usability vs. Reusability
Ian Sommerville
- A general interface helps to reuse a component in many architectures
- A general interface is more complex and difficult to use in a single architecture
Performance vs. Reusability
Ian Sommerville
- A general reusable component may be less optimized and provide worse performance than a specific, non-reusable one
- For the same interface, there can be multiple implementation components optimized for different hardware platforms/execution environments
API
What is an API?
Is it really API?
Rule of Threes (Will Tracz)
123
- You know you have designed a reusable API only
after at least three clients have been built for it
- A component interface with only one known client does not qualify as a reusable API
Many Applications
API Examples
API Design
Where to start?
- The API should be designed from the perspective of the application(s) developers using it
- Do not leak into the design of the API details from the existing component implementation
Explicit Interfaces Principle
Bertrand Meyer
- Components always communicate through interfaces
- No Shared State
- No Out-Of-Band Communication
Small Interfaces Principle
Bertrand Meyer
- If two components communicate, they exchange as little information as possible
Few Interfaces Principle
Bertrand Meyer
- Every component communicates with as few others as possible
DRY
Do not Repeat Yourself
Clear Interfaces Principle
Bertrand Meyer
- Do not publish useless or unused functionality
- Design interfaces that are needed and actually used by other components
YAGNI
You ain't gonna need it
Design Advice
- Keep it simple
- Do One Thing and do it well
- Do not surprise clients
- Keep it as small as possible but not smaller
- When in doubt leave it out
- You can always add more later
- Maximize information hiding
- API First
- Avoid leakage: implementation should not impact interface
- Names Matter
- Avoid cryptic acronyms
- Use names consistently
- Keep it consistent
- Naming Conventions
- Argument Ordering
- Return values
- Error Handling
- Follow the conventions of the underlying platform
- Document Everything
- Classes, Methods, Parameters
- Include Correct Usage Examples
- Quality of Documentation critical for success
- Make it easy to learn and easy to use
- without having to read too much documentation
API Evolution
Only one chance...
...to get the design right:
- Application Programming Interface
- Programming Language (and Standard Library)
- External Data Model:
- File/Document Format
- Database Schema
- Wire/Message Representation Format
- Once the API becomes public, it is out of your hands and it will be very expensive to change!
API Evolution
- Once in the API, keep it there forever
- Never add something you do not intend to keep forever
- Easier to add than to remove
- Keep changes backwards and forwards compatible
- Make sure you explicit version all changes (including documentation) pointing out incompatibilities
- Publish 0.x versions of an API before freezing it to get early feedback from users
- Rename a component if its API has changed too much so you can start the redesign from scratch without breaking old clients
API Compatibility
- Backwards compatibility:
new version of API compatible with old client
- Forwards compatibility:
old version of API compatible with new client
1435mm Standard Gauge
1668mm Iberian Gauge
1524mm Russian Gauge
1000mm RhB (Swiss Alps)
Compatible Interfaces
- To be connected, component interfaces need to match perfectly
Adapter
- Adapters help to connect mismatching interfaces
- Adapters deal with transforming the interaction provided by one to the interaction required by the other interface
- Warning: if data or functionality are missing the adaptation may be impossible
Wrapper
- The mismatching end of the adapter is hidden inside a wrapper component
- The adaptation logic is encapsulated within the wrapper and made reusable
References
- William Brown, Raphael Malveau, Hays McCormik III, Thomas Mowbray, Anti-Patterns, Refactoring Software, Architectures, and Projects in Crisis, Wiley, 1998
- Joshua Bloch, How to Design a Good API and Why it Matters,
Google Tech Talk Slides Video
- Michi Henning, API: Design Matters, ACM Queue, Vol 5, No 4, May/June 2007
- Will Tracz, Confessions of a Used Program Salesman, Addison-Wesley, 1995
- Jaroslav Tulach, Practical API Design: Confessions of a Java Framework Architect, APress, 2008, ISBN 1-4302-0973-9
Use a spacebar or arrow keys to navigate