You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Lecture 2 The Power of APIs and Introduction to API Design
The Power of APIs
Exponential growth in the power of APIs
'00s - Data structures, higher-level abstractions, Web APIs: social media, cloud infrastructure
Enabled code reuse on a grand scale
Increased the level of abstraction dramatically
Most of the functionality came from libraries
APIs confer superpowers
Characteristics of a good API
Easy to learn
Easy to use, even if you take away the documentation
Hard to misuse
Easy to read and maintain code that uses it
Sufficiently powerful to satisfy requirements
Handles edge and corner cases gracefully
Easy to evolve
Appropriate to audience
A bit of APIs
The way to learn API design is to design APIs
Problem:
Can you write me an API for a thermometer?
It has to support Fahrenheit and Celsius because we'll have users in the US and Eurepe
Design Choices
Store temperature
Temperature unit
Conversion
Changes/Updates
Representation
Temperature precision / range
// My Thermometer DesignpublicenumTemperatureUnit {
Celsius,
Fahrenheit;
}
publicclassThermometer {
// Constructor.Thermometer(doubletemperature, TemperatureUnitunit);
// Convert current temperature into Fahrenheit scale. No operation would be applied if the current scale is Fahrenheit.publicvoidconvertToFahrenheit();
// Convert current temperature into Celsius scale. No operation would be applied if the current scale is Celsius.publicvoidconvertToCelsius();
// Returns current temperature in given unit.publicdoublegetTemperature(TemperatureUnitunit);
// Returns current temperature in default unit.publicdoublegetTemperature();
// Returns current temperature unit.publicTemperatureUnitgetTemperatureUnit();
// Sets the new temperature according to given temperature and unit (scale).publicbooleanchangeTemperature(doublenewTemperature, TemperatureUnitunit);
}
// Thermometer Design 1publicclassThermometer {
publicstaticvoidsetScaleFahrenheit(booleanfahrenheit);
publicstaticdoubletemperature();
}
// Thermometer Design 1cpublicclassThermometer {
publicenumScale { FAHRENHEIT, CELSIUS }
publicstaticvoidsetScale(Scalescale);
publicstaticdoubletemperature();
}
// Thermometer Design 1dpublicclassThermometer {
publicenumScale { FAHRENHEIT, CELSIUS }
publicstaticdoubletemperature(Scalescale);
}
// it's a pseudo-singleton// can't test without sensor hardware to mock// Thermometer Design 2publicclassThermometer {
publicenumScale { FAHRENHEIT, CELSIUS }
publicThermometer();
publicdoubletemperature(Scalescale);
}
// Thermometer Design 3publicinterfaceThermometer {
enumScale { FAHRENHEIT, CELSIUS }
doubletemperature(Scalescale);
}
publicclassThermometer {
// Returns "standard thermometer." Doc must say what that is!publicThermometergetThermometer(); // Static factory
}
// Thermometer Design 3bpublicinterfaceThermometer {
enumScale { FAHRENHEIT, CELSIUS }
doubletemperature();
doubletemperature(Scalescale);
ScaledefaultScale();
}
// To invert control, use the Observer patternpublicinterfaceThermometer {
enumScale { FAHRENHEIT, CELSIUS }
interfaceTemperatureChangeListener {
voidtemperatureChange(doublenewTemp, Scalescale);
}
voidaddListener(TemperatureChangeListenerlistener);
// Returns true if the listener was represent.booleanremoveListener(TemperatureChangeListenerlistener);
doubletemperature();
doubletemperature(Scalescale);
ScaledefaultScale();
}
Combinatorial explosion of possible designs
Shape: Utility class vs. true singleton vs. final concrete class vs. non-final concrete class vs. abstract class vs. interface
Scale specification: boolean vs. method clevage. vs. enum
setScale method vs. passing scale in to temperature