Back

Technologies:

javascriptjavascript
avatar
Tolerim
a month ago

How can I execute a shared method in all child classes?

In my code setup, I have several classes that implement the Sender interface. These classes include Sender.js, UserInfoSender.js, and DocumentContentSender.js, among others, and they all have a sendData() function that needs to call a common function named cleanDataBeforeSend(). While I could manually call this function from each class, this is not ideal, as new child classes may forget to include the call to cleanDataBeforeSend(). What is the best approach to ensure that all child classes include the necessary call to cleanDataBeforeSend() without having to add it manually to each class?

Answers(2)
avatar
Vogi
a month ago
Verified Answer
One way to ensure that all child classes include the necessary call to cleanDataBeforeSend() without having to add it manually to each class is by using inheritance. You can create a parent class that includes the cleanDataBeforeSend() function and have all child classes inherit from this parent class. This way, all child classes will have access to the cleanDataBeforeSend() function without having to add it manually. Here is an example of how you can implement this:
class Sender:
    def sendData(self):
        self.cleanDataBeforeSend()
        # send data

    def cleanDataBeforeSend(self):
        raise NotImplementedError

class UserInfoSender(Sender):
    def __init__(self, userInfo):
        self.userInfo = userInfo

    def cleanDataBeforeSend(self):
        # clean data specific to UserInfoSender
        pass

class DocumentContentSender(Sender):
    def __init__(self, documentContent):
        self.documentContent = documentContent

    def cleanDataBeforeSend(self):
        # clean data specific to DocumentContentSender
        pass
In this example, Sender is the parent class that includes the sendData() function and the cleanDataBeforeSend() function. UserInfoSender and DocumentContentSender are child classes that inherit from Sender and implement their own version of the cleanDataBeforeSend() function. I hope this helps! Let me know if you have any other questions.

Sources

Call a child class method from a parent class objectWhat is use of Parent object instantiating with child classhow call function in class parent in child class? - CodeProjectRedefining methods through class inheritance in Python
avatar
Tolerim
a month ago
One way to achieve this is by using Composition over Inheritance, which means instead of inheriting from a parent class, we can create a separate class or object that holds the common functionality and include it in each child class. In this case, we can create a class called DataCleaner that contains the cleanDataBeforeSend() function and then include an instance of this class in each xxxSender class. Here's an example implementation:
class DataCleaner {
  cleanDataBeforeSend() {
    // implementation of the function here
  }
}

class UserInfoSender {
  constructor() {
    this.dataCleaner = new DataCleaner();
  }

  sendData(data) {
    this.dataCleaner.cleanDataBeforeSend();
    // implementation of the function here
  }
}

class DocumentContentSender {
  constructor() {
    this.dataCleaner = new DataCleaner();
  }

  sendData(data) {
    this.dataCleaner.cleanDataBeforeSend();
    // implementation of the function here
  }
}
In this example, we have created a separate DataCleaner class that contains the cleanDataBeforeSend() function. Then, in each child class (UserInfoSender and DocumentContentSender), we include an instance of the DataCleaner class in the constructor. Finally, in the sendData() function of each child class, we call this.dataCleaner.cleanDataBeforeSend() to ensure that the data is cleaned before it is sent. This way, we can ensure that any new child classes we create in the future will also have the cleanDataBeforeSend() function called without the need for them to remember to call it themselves.
;