java - How does the Command pattern decouple the sender from the receiver? -
the command pattern has ireceiver interface few methods , corresponding each method there concrete command objects (implementing interface icommand execute() method).
i have read client knows concrete receiver , concrete command , client setting receiver object in concrete command object. why said decouples sender , receiver?
when client knows concrete receiver feel not loose coupling , client in case can directly call apis (methods) on receiver object.
you can think of command pattern workflow follows.
commanddeclares interface commands, providing simple execute() method asks receiver of command carry out operation.the
receiverhas knowledge of carry out request.the
invokerholds command , cancommandexecute request calling execute method.the
clientcreatesconcretecommands, setsreceivercommand.the
concretecommanddefines binding between action , receiver.when
invokercalls executeconcretecommandrun 1 or more actions on receiver.
have @ sample code understand things in better way.
public class commanddemoex{ public static void main(string args[]){ // on command tv same invoker receiver r = new tv(); command oncommand = new oncommand(r); invoker invoker = new invoker(oncommand); invoker.execute(); // on command dvdplayer same invoker r = new dvdplayer(); oncommand = new oncommand(r); invoker = new invoker(oncommand); invoker.execute(); } } interface command { public void execute(); } class receiver { public void switchon(){ system.out.println("switch on from:"+this.getclass().getsimplename()); } } class oncommand implements command{ private receiver receiver; public oncommand(receiver receiver){ this.receiver = receiver; } public void execute(){ receiver.switchon(); } } class invoker { public command command; public invoker(command c){ this.command=c; } public void execute(){ this.command.execute(); } } class tv extends receiver{ public tv(){ } public string tostring(){ return this.getclass().getsimplename(); } } class dvdplayer extends receiver{ public dvdplayer(){ } public string tostring(){ return this.getclass().getsimplename(); } } output:
java commanddemoex switch on from:tv switch on from:dvdplayer to answer question :
i have read client knows concrete receiver , concrete command , client setting receiver object in concrete command object. why said decouples sender , receiver
to standardize words, replace "sender" "invoker". go through code.
invoker executes concretecommand(oncommand in case) passing concretereceiver.concretecommand executes commandthrough concretereceiver i.e.concretecommand defines binding between action , receiver.- if see workflow, invoker not change additional commands , can add business logic in
execute()method of invoker java.lang.thread, has been explained below. - in way
client (sender) , receiver loosely couple through invoker, has knowledge of command executed.
thread example link
you can create thread implementing runnable object.
thread t = new thread (new myrunnable()).start(); =>
invoker invoker = new invoker(new concretecommand()); invoker.start() and have logic in start() call concretecommand.execute() run() in above case.
start() method call run() method in thread. happens if directly call run() method directly? won't treated thread.
like start() method of thread, can add business logic in invoker.
public synchronized void start() { /** * method not invoked main method thread or "system" * group threads created/set vm. new functionality added * method in future may have added vm. * * 0 status value corresponds state "new". */ if (threadstatus != 0) throw new illegalthreadstateexception(); group.add(this); start0(); if (stopbeforestart) { stop0(throwablefromstop); } } private native void start0(); // native code not here method call run() method public void run() { if (target != null) { target.run(); } } edit:
on last query
here creating command object, receiver object , invoker object.then passing receiver object in command object , passing command object in invoker object. each receiver here tv , dvdplayer. in method 'main' object of tv , dvdplayer known , in fact created. can tvobject.switchon() , dvdplayer.switchon(). how command pattern help
client need not worry changes in receiver class. invoker directly works on concretecommand, has receiver object. receiver object may change siwtchon() switchondevice() in future. client interaction not change.
if have 2 different commands switchon() , switchoff(), still can use same invoker.
Comments
Post a Comment