Skip to content

Instantly share code, notes, and snippets.

@RayLuxembourg
Created August 19, 2018 14:58
Show Gist options
  • Save RayLuxembourg/ed2a28788c76f3cc33a1907a49e0fd4d to your computer and use it in GitHub Desktop.
Save RayLuxembourg/ed2a28788c76f3cc33a1907a49e0fd4d to your computer and use it in GitHub Desktop.
dry-example.ts
interface BaseProduct {
name: string;
id: number;
price: number;
}
interface ProductsList {
top: BaseProduct[];
bottom: BaseProduct[];
}
class Slider {
data: ProductsList
next(): void { }
prev(): void { }
select(): void { }
}
abstract class AbstractSlider extends Slider {
//will handle this.data
abstract initData(): void;
init() {
this.initData();
//... rest of the flow
}
}
class SliderX extends AbstractSlider{
initData() {
this.data = this.getStandartProduct();
}
getStandartProduct(): ProductsList {
return { top: [], bottom: [] }
}
}
class SliderY extends AbstractSlider{
initData() {
const baseProduct = this.getBaseProduct();
const bottom = this.getRecommendedProducts(baseProduct.id);
this.data = {
top: [baseProduct],
bottom
}
}
getBaseProduct(): BaseProduct {
return {
name: 'someName',
id: 1,
price: 33
}
}
getRecommendedProducts(baseProductId: number): BaseProduct[] {
// get Recommended products from server using the baseProduct
return [{
name: 'someName',
id: 1,
price: 33
}];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment