4 ms·
Re the switch example, another possible solution is the command pattern, where you encapsulate the action in an object. e.g. (in C# because my JavaScript isn't
by Nition 7y ago
Re the switch example, another possible solution is the command pattern, where you encapsulate the action in an object.
e.g. (in C# because my JavaScript isn't good):
interface IContentType {
void CreateType();
}
class ContentPost : IContentType {
public void CreateType() {
return console.log("creating a post...");
}
}
class ContentVideo : IContentType {
public void CreateType() {
return console.log("creating a video...");
}
}
void CreateContentType(IContentType contentType) {
// Instead of a switch, you can now do:
contentType.CreateType();
}
Mainly useful if you want to do more than one thing depending on content type, of course. It's overkill if you'd only need a single switch anyway.