Dp Command
DodaTech
1 min read
In this tutorial, you'll learn about How to Fix Command Errors. We cover key concepts, practical examples, and best practices.
Fix command errors when operations hardcoded in UI layer instead of encapsulated commands.
Quick Fix
Wrong
class Button:
def __init__(self,label): self.label=label
def click(self):
if self.label=='Save':
print('Saving...')
elif self.label=='Print':
print('Printing...')
Button knows about application logic. Adding new action requires modifying Button class.
Right
from abc import ABC,abstractmethod
class Command(ABC):
@abstractmethod
def execute(self): pass
class SaveCommand(Command):
def execute(self): print('Saving...')
class PrintCommand(Command):
def execute(self): print('Printing...')
class Button:
def __init__(self,label,cmd):
self.label=label; self.cmd=cmd
def click(self): self.cmd.execute()
Button('Save',SaveCommand()).click()
Button doesn't know what happens. Command object encapsulates action. Undo/redo possible.
Prevention
Command pattern encapsulates request as object. Parameterize, queue, log, undo operations.
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