I have a class in my code and I'm not sure if the design I've implemented is correct or if there is a better way to do it. The class I have created have important useful methods that is used by each instance of that class. However, the problem is that the instance itself is never used. The class handles everything that needs to be done without using the object itself. So, once I call the constructor, I never use the object again. The internals of the class handle everything. Here's an example of what I mean. It's written in JS but can apply to pretty much any lang.
class MyHTMLElement {
constructor(htmlElement) {
this.htmlElement = htmlElement;
this.changeColorOnClick();
this.changeBorderOnInput();
}
changeColorOnClick() {
this.htmlElement.onclick = () => {
this.htmlElement.style.color = "blue";
}
}
changeBorderOnInput {
this.htmlElement.addEventListener("input", () => {
this.htmlElement.style.border = "2px solid green";
}
}
Now once I've created this, all I need to do is call new MyHTMLElement("element");
for each instance I need and then never use the functionalities of the class ever again. I will still use the instance htmlElement
attribute, but will never use any of the methods. This seems like it's bad deign to me. Is there a better way to do this that is also very clean. Thanks for your help.