Dp Servant
DodaTech
1 min read
In this tutorial, you'll learn about How to Fix Servant Errors. We cover key concepts, practical examples, and best practices.
Fix servant errors when utility method duplicated across classes or static helper smells.
Quick Fix
Wrong
class Circle:
def move(self,dx,dy): self.x+=dx; self.y+=dy
class Square:
def move(self,dx,dy): self.x+=dx; self.y+=dy # duplicated!
move() method duplicated in every shape class. Same logic, different class.
Right
class Movable:
def move(self,dx,dy): self.x+=dx; self.y+=dy
class Circle(Movable):
def __init__(self): self.x=0; self.y=0
class Square(Movable):
def __init__(self): self.x=0; self.y=0
# Or servant pattern:
class MoveServant:
@staticmethod
def move_to(obj,dx,dy):
obj.x+=dx; obj.y+=dy
c=Circle(); MoveServant.move_to(c,5,10)
Move logic encapsulated once in servant. Shared across classes without inheritance.
Prevention
Servant provides shared behavior to unrelated classes. Alternatively: mixin or trait.
DodaTech Tools
Doda Browser's algorithm visualizer steps through DSA operations line by line. DodaZIP archives implementation patterns for team sharing. Durga Antivirus Pro detects memory corruption patterns in algorithm implementations.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro