L - Liskov Substitution (LSP)
About
Easy practical example
interface HttpClient {
get(url: string): Promise<any>;
post(url: string, data: any): Promise<any>;
}
// Returns `response.data`
class AxiosAdapter implements HttpClient {
get(url: string): Promise<any> {
return axios.get(url);
}
post(url: string, data: any): Promise<any> {
return axios.post(url, data);
}
}
// Returns `response.body`
class FetchAdapter implements HttpClient {
async get(url: string): Promise<any> {
const response = await fetch(url);
return response.json();
}
async post(url: string, data: any): Promise<any> {
const response = await fetch(url, {
method: 'post',
headers: { "content-type": "application/json" },
body: JSON.stringify(data)
});
return response.json();
}
}Parameters and Returns
Example Base
Contravariance of method parameters
Covariance of method return types
Exceptions thrown
Pre & Post conditions
Preconditions
Postconditions
Invariance (Internal State)
Last updated