简体   繁体   中英

How to generalize logic for two classes that do exactly the same but for different entity?

I have two classes in my project: first do something with comments , second with alerts : AlertFilterService and CommentFilterService

They have almost identical constructors and exactly the same methods signature, like do_somethig_for_alerts(self) and do_something_for_comments(self) .

class AlertFilterService:
  do_somethig_for_alerts(self):
    some_code
    if
       code
    else:
       message['status'] = AlertStatus.NEW.value
       await self.db.store_alert(message)

class CommentFilterService
  do_somethig_for_comments(self):
    some_code
    if
       code
    else:
       message['status'] = CommentStatus.NEW.value
       await self.db.store_comment(message)

How to avoid code duplication? I wanna to have an abstract class like FilterService (that will have all common parts) and two concrete implementations. What is the best way to do this?

If you have two almost identical classes with the other having a small difference, you can subclass the other by extending it.

If you want to have a single class, implement 2 different methods to handle the different cases or just make one method that accepts a parameter that will handle the case depending on the parameter passed

You can try something like that

def select_val_type(tag)            
            if (tag== "comments"
                    val== Comments.NEW.value
            else:
                    val == AlertStatus.NEW.value
            message['status'] = val
            if(tag == "comments")
                    await self.db.store_comments(message)
            else:
                    await self.db.store_alert(message)

There are quite a few ways you could go with this, so without much context, I'll stick to a very general answer.

This is a very general / rough implementation / use case of the state pattern . See this link for an implementation.

class MessageStatus(object):
    def __init__(self, status_instance):
        self._status_instance = status_instance # maintain a ptr to the class containing your attributes

    def __call__(self):
        # some code
        if some_condition:
           # more code
        else:
            message['status'] = self.cls.NEW.value
            await self.status_instance.db.store_comment(message) 

The state pattern is a behavioral software design pattern that implements a state machine in an object-oriented way. With the state pattern, a state machine is implemented by implementing each individual state as a derived class of the state pattern interface, and implementing state transitions by invoking methods defined by the pattern's superclass.

Note: Per the previous outline of the state pattern, if you wanted to make this conformant with the state pattern, I'd recommend refactoring your code such that the comment / alert status classes are subclasses of a generic state class, which would contain the logic above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM