1

Suppose i have a class ObjectUtills and all the methods which are as part of the class has some common things to do before start and after the end of the core functionality of the function like:

func1() {

  some common things

  core functionality

  some common things
}

So my question is how would you achieve this by OOPS concept. Suppose tomorrow somebody is going to add a new function then he should just add the core functionality not the some common thingsand the client who calls this function automatically get those common functionality. Thanks.

Please don't give answers like put in a function like preProcess postProcess and call those inside all the functions. But thats the same thing.

Trying
  • 14,004
  • 9
  • 70
  • 110

1 Answers1

0

You can define two methods that do the common logic, for example - preProcess() and postProcess().

If the methods that execute this common logic are instnace methods of ObjectUtils, you can even override the common methods in sub-classes of ObjectUtils if you want to alter the common logic.

If the methods that share the common logic are static methods, preProcess() and postProcess() would also be static, and in this case there would be nothing specific to OOP in this approach.

func1() {   
  preProcess();    
  core functionality    
  postProcess();
}
Eran
  • 387,369
  • 54
  • 702
  • 768