Command pattern

In computer programming, command pattern is one of design patternss that use a value object to communicate the details of the action that is desired.

When: There is a proliferation of similar methods, and the interface to implement that kind of object is becoming unwieldy.

Symptoms: Too many public methods for other objects to call. An interface that is unworkable and always changing. You feel that a method name must include prose describing the exact action, and this is preventing layering your code.

A command object is a case of using a value object to communicate which action is to be performed, along with any argument data. This is sent to a single method in the class that handles commands of the given type. That object is free to implement command processing with a switch, a variable method dispatch, or a call to a variable subclass. This lets you make changes to which commands are defined only in the definition of the command objects itself and the classes that actually use that command, rather than every class that wants to implement the command processing interface. It also frees up the command implementing the command processing interface to use any number of ideas for dispatching the command, once it has it:

 # example of a switch style arrangement:

sub doCommand { my $me = shift; my $cmd = shift; $cmd->isa('BleahCommand') or die; my $instr = $cmd->getInstructionCode(); if($instr eq 'PUT') { # PUT logic here } elsif($instr eq 'GET') { # GET logic here } # etc }

# example of a variable method call arrangement: sub doCommand { my $me = shift; my $cmd = shift; $cmd->isa('BleahCommand') or die; my $instr = $cmd->getInstructionCode(); my $func = "process_" . $instr; return undef unless defined &$func; return $func->($cmd, @_); }

# example of a variable subclass arrangement. # this assumes that %commandHandlers is set up with a list of object references.

sub doCommand { my $me = shift; my $cmd = shift; $cmd->isa('BleahCommand') or die; my $insr = $cmd->getInstructionCode(); my $objectRef = $commandHandlers{$instr}; return $objectRef ? $objectRef->handleCommand($cmd, @_) : undef; }

Since Perl offers //AUTOLOAD//, this idea could be emulated. If a package wanted to process an arbitrary and growing collection of commands to the best of its ability, it could catch all undefined method calls using //AUTOLOAD//, and then attempt to dispatch them (this assumes //%commandHandlers// is set up with a list of object references keyed by method name):

 sub AUTOLOAD {
   my $me = shift;
   (my $methodName) = $AUTOLOAD m/.*::(\\w+)$/;
   return if $methodName eq 'DESTROY';
   my $objectRef = $commandHandlers{$methodName};
   return $objectRef ? $objectRef->handleCommand($methodName, @_) : undef;
 }

This converts calls to different methods in the current object to calls to a //handleCommand()// method is different objects. This is an example of using Perl to shoehorn a Command Object pattern onto a non Command Object interface.

The article is originally from Perl Design Patterns Book

External Links


See also: Command object, Design patternss, Threaded command object, Scheduler pattern


In the News

Endocannabinoids -- The Brain's Cannabis -- Demonstrate Novel Modes Of
Research teams from Louisiana, Japan and Scotland report on endocannabinoids as a novel neural messenger in various stress-related situations with possible applications in eating, disease treatment and social behavior. The Tulane/LSU team found that endocannabinoids acted as an intrabrain messenger to shutdown the neuroendocrine stress response. The Japanese team said it next would look at endocannabinoids involvement in autonomic, endocrine and immune function. The Edinburgh group is studying endocannabinoids' influence on oxytocin and social behavior.

Select Exotics: Frequently Asked Questions
Questions and answers about exotic cat breeds, including the savannah ("created by the breeding of a serval to a domestic cat"), the serval (wild African cat), the Bengal (bred from an Asian leopard and a domestic cat), and the safari (cross between a wild Geoffroy's cat from South America and a domestic cat). Discusses legal aspects, behavior, care, and related topics. Includes images. From an exotic cat breeder.

New Dopamine Brain Target Discovered; Potential Breakthrough For Schiz
A team of Canadian researchers, lead by Dr. Susan George and Dr. Brian O'Dowd at the Center for Addiction and Mental health, discovered a Gq/11-coupled signalling unit that triggers a calcium signal. This novel target is turned on by stimulating D1 and D2 dopamine receptors. This is the first time that a direct connection between dopamine and calcium signals has been reported. This data has significant implications for schizophrenia.

Research Offers Hope Of New Treatments For Liver Damage 'Plague'
Millions of patients suffering from liver damage (cirrhosis) and failure may benefit from research by the Universities of Edinburgh and Southampton which may lead to new life-saving treatments. There is currently no cure for liver cirrhosis and a patient's only hope of survival is to receive a liver transplant.

Drugs and addiction
Many people in the modern Western world delude themselves that their culture is generally free from the effects of intoxicating substances except in the criminal underworld, and that ‘nice people don’t take drugs’. But Richard Rudgley of the University of Oxford, a researcher of the oasis communities of Chinese Central Asia, shows that our culture [...]

Global Nuclear Disarmament Fund: "Full Circle"-- The Epic Return to Tr
"Almost 60 years ago a flame was kindled from the embers of the City of Hiroshima after the ... first atomic bomb was dropped."This site describes how in July and August 2005 Zen monks will walk, carrying this "atomic flame,"from San Francisco to the Trinity Test Site in Alamogordo, New Mexico (location of the first atomic bomb detonation), where the flame will be extinguished. Includes a route map and schedule. Also in Japanese.

Low-dose Aspirin Shown To Reduce Risk Of First Stroke In Women
In a long-awaited clinical trial conducted among nearly 40,000 initially healthy middle-aged American women, regular use of low-dose aspirin over a 10-year period was found to reduce the risk of stroke 17 percent. However, among the same population, researchers from Harvard-affiliated Brigham and Women's Hospital (BWH) also found that low-dose aspirin did not benefit most women in terms of preventing first heart attacks or cardiac deaths.

Greenhouse Gas Burial: Storing Unwanted Carbon Dioxide In Unmineable C
Deep coal seams that are not commercially viable for coal production could be used for permanent underground storage of carbon dioxide generated by human activities, thus avoiding atmospheric release, according to two studies published in the Journal of Environment and Pollution. An added benefit of storing carbon dioxide in this way is that additional useful methane will be displaced from the coal beds.

Blood Test Shows Promise As Monitor For Antiangiogenic Cancer Therapy
Scientists have uncovered critical information that may lead to an urgently needed method for effective monitoring of antiangiogenic cancer therapies. The research, published in the January issue of Cancer Cell, is likely to facilitate development of new antiangiogenic drugs or treatment strategies and allow for accurate determination of the optimal drug doses to use for such therapies.

The Shot Heard Round the World: Development of the Salk Polio Vaccine
This illustrated timeline presents events surrounding the development of the polio vaccine by Dr. Jonas Salk. Discusses early history of polio, the first major U.S. polio epidemic in 1916, the research of Salk and others to develop a polio vaccine, the March of Dimes polio vaccine fundraising campaign, and the drop in polio incidence since the first widespread use of Salk's vaccine in 1955. From the University of Pittsburgh School of Pharmacy.


MP3 Music Downloads

Preview songs, Download Free Music,Burn CDs at ITunes.com
iTunes_RGB_9mm

 


Google




InformationQuickFind.com - Find Information Fast

Links