I want to use OCP rule in my "small" project. Actually i have a problem with implementing this one. Let me show you what have i wrote.
abstract class AnimationType {
public abstract createAnimation(timing: number): { in: string, out: string };
}
class FadingAnimation extends AnimationType {
public createAnimation(timing: number): { in: string, out: string } {
return { in: 'opacity: 0', out: 'opacity: 1' }
}
}
class TransformAnimation extends AnimationType {
public createAnimation(timing: number): { in: string, out: string } {
return { in: 'transform: translateX(-100%)', out: 'transform: translateX(100%)' }
}
}
class AnimationCreator {
private timing: number;
constructor(time: number) {
this.timing = time;
}
getAnimation(animation: AnimationType): { in: string, out: string } {
return animation.createAnimation(this.timing);
}
}
Do I write it good? If I do, then what actually i have gained? I need to send object of FadingAnimation class if i want to add this animation. If i want transform animation i need to send object of TransformAnimation class - so somewhere i have to use switch(this.animationType).