2

Say I have different types of operations which i need to perform. I have an abstract class 'BaseOperation' which has all the information common to all operations. Now i need to categorize these operations based on an attribute and then further categorize every operation in each category based on some other attribute as shown in figure below. The goal is to log the operation type and subtype to prioritize investigation in case of any failures. enter image description here

The way i thought i would do this is: I can have two enums corresponding to type and subtype in 'BaseOperation'. I can define a additional layer of abstract classes deriving the base which set the type of the operation and have operations derive the class of their type. The subtype for each operation can be set foreach operation individually.

I am not sure if this will be the best thing to do and if there is any design pattern i can follow for this scenario (i couldn't find any yet). Also i need this design to be extendable, say i want another attribute to be added for each operation (which i can easily do above by adding another enum in base and setting it again for each operation).

  • please don't cross-post: stackoverflow.com/questions/51019244/how-can-i-design-this "Cross-posting is frowned upon as it leads to fragmented answers splattered all over the network..." – gnat Jun 26 '18 at 6:38
  • 2
    What is best is often a matter of opinion between 2 or 3 choices and design patterns are not a silver bullet that get you out of it. Do you see a flaw in your design using enums that makes it unworkable? If not, why not just go for it? – Bart van Ingen Schenau Jun 26 '18 at 6:40
  • Why do you need enums? You already have the classes. – kevin cline Jun 26 '18 at 6:42
  • @kevincline, Why do they need classes? They already have enums... – David Arno Jun 26 '18 at 6:47
1

I think what you are looking for is the Decorator Pattern. Write an abstract base class like AbstractOperation, then make your BaseOperation a concrete subclass. Your decorator would be another abstract class which also inherits from AbstractOperation, and stores the “inner” operation class. Because your decorator classes are also subclasses of AbstractOperation, you can use them interchangeably with BaseOperation, just adding whatever extra functionality you need, such as custom logging.

Using the Decorator pattern makes adding additional subtypes as easy as simply writing another concrete class, and the rest of your code will be none the wiser.

  • i considered decorator pattern too for my design but felt it to be overkill as it is a run time solution. Using this pattern, I will have to specify the attributes while instantiating an operation. Since the attributes are same for all the operation i felt this should be solved at compile time. Let me know if i understand this wrong. – utkarsh Jun 26 '18 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.