java qr code generator library Reusing user action handlers: Command pattern in Java

Generator QR Code 2d barcode in Java Reusing user action handlers: Command pattern

3.2.3 Reusing user action handlers: Command pattern
QR-Code Maker In Java
Using Barcode drawer for Java Control to generate, create QR-Code image in Java applications.
www.OnBarcode.com
QR-Code Recognizer In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
It may be obvious to say that in most applications, the user is telling (through mouse clicks and keyboard presses) the app to do something, and the app then does it. In a simple program, we might present the user with only one way to
Printing Data Matrix ECC200 In Java
Using Barcode creator for Java Control to generate, create Data Matrix 2d barcode image in Java applications.
www.OnBarcode.com
DataMatrix Encoder In Java
Using Barcode encoder for Java Control to generate, create Data Matrix ECC200 image in Java applications.
www.OnBarcode.com
Introducing order to Ajax
Make QR Code 2d Barcode In Java
Using Barcode drawer for Java Control to generate, create QR Code ISO/IEC18004 image in Java applications.
www.OnBarcode.com
Create Code39 In Java
Using Barcode encoder for Java Control to generate, create Code39 image in Java applications.
www.OnBarcode.com
perform an action, but in more complex interfaces, we will often want the user to be able to trigger the same action from several routes. Implementing a button widget Let s say that we have a DOM element styled to look like a button widget that performs a calculation when pressed and updates an HTML table with the result. We could define a mouse-click event-handler function for the button element that looks like this:
2D Barcode Generator In Java
Using Barcode generation for Java Control to generate, create 2D Barcode image in Java applications.
www.OnBarcode.com
International Standard Book Number Encoder In Java
Using Barcode maker for Java Control to generate, create ISBN - 10 image in Java applications.
www.OnBarcode.com
function buttonOnclickHandler(event){ var data=new Array(); data[0]=6; data[1]=data[0]/3; data[2]=data[0]*data[1]+7; var newRow=createTableRow(dataTable); for (var i=0;i<data.length;i++){ createTableCell(newRow,data[i]); } }
Reading QR In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
QR Code JIS X 0510 Maker In None
Using Barcode printer for Office Word Control to generate, create QR Code ISO/IEC18004 image in Office Word applications.
www.OnBarcode.com
We re assuming here that the variable dataTable is a reference to an existing table and that the functions createTableRow() and createTableCell() take care of the details of DOM manipulation for us. The interesting thing here is the calculation phase, which could, in a real-world application, run to hundreds of lines of code. We assign this event handler to the button element like so:
Decode Code39 In None
Using Barcode reader for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Painting Barcode In None
Using Barcode drawer for Excel Control to generate, create Barcode image in Office Excel applications.
www.OnBarcode.com
buttonDiv.onclick=buttonOnclickHandler;
Printing DataMatrix In None
Using Barcode printer for Software Control to generate, create Data Matrix 2d barcode image in Software applications.
www.OnBarcode.com
Make Data Matrix ECC200 In None
Using Barcode creator for Online Control to generate, create Data Matrix image in Online applications.
www.OnBarcode.com
Supporting multiple event types Let s say that we have now supercharged our application with Ajax. We are polling the server for updates, and we want to perform this calculation if a particular value is updated from the server, too, and update a different table with the data. We don t need to go into the details of setting up a repeated polling of the server here. Let s assume that we have a reference to an object called poller. Internally, it is using an XMLHttpRequest object and has set its onreadystatechange handler to call an onload function whenever it has finished loading an update from the server. We could abstract out the calculation and display phases into helper functions, like this:
Barcode Decoder In Java
Using Barcode Control SDK for Java Control to generate, create, read, scan barcode image in Java applications.
www.OnBarcode.com
Barcode Recognizer In VB.NET
Using Barcode Control SDK for .NET Control to generate, create, read, scan barcode image in VS .NET applications.
www.OnBarcode.com
function buttonOnclickHandler(event){ var data=calculate(); showData(dataTable,data); } function ajaxOnloadHandler(){ var data=calculate();
Barcode Decoder In Visual C#
Using Barcode decoder for .NET framework Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
PDF 417 Encoder In Visual Studio .NET
Using Barcode maker for ASP.NET Control to generate, create PDF 417 image in ASP.NET applications.
www.OnBarcode.com
Some small refactoring case studies
Generating 1D Barcode In Visual C#
Using Barcode drawer for .NET Control to generate, create Linear Barcode image in .NET framework applications.
www.OnBarcode.com
Recognizing PDF 417 In C#.NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
showData(otherDataTable,data); } function calculate(){ var data=new Array(); data[0]=6; data[1]=data[0]/3; data[2]=data[0]*data[1]+7; return data; } function showData(table,data){ var newRow=createTableRow(table); for (var i=0;i<data.length;i++){ createTableCell(newRow,data[i]); } } buttonDiv.onclick=buttonOnclickHandler; poller.onload=ajaxOnloadHandler;
A lot of the common functionality has been abstracted out into the calculate() and showData() functions, and we re only repeating ourselves a little in the onclick and onload handlers. We ve achieved a much better separation between the business logic and the UI updates. Once again, we ve stumbled upon a useful repeatable solution. This time it is known as the Command pattern. The Command object defines some activity of arbitrary complexity that can be passed around in code easily and swapped between UI elements easily. In the classic Command pattern for objectoriented languages, user interactions are wrapped up as Command objects, which typically derive from a base class or interface. We ve solved the same problem in a slightly different way here. Because JavaScript functions are first-class objects, we can treat them as Command objects directly and still provide the same level of abstraction. Wrapping up everything that the user does as a Command might seem a little cumbersome, but it has a hidden payoff. When all our user actions are wrapped up in Command objects, we can easily associate other standard functionality with them. The most commonly discussed extension is to add an undo() method. When this is done, the foundations for a generic undo facility across an application are laid. In a more complex example, Commands could be recorded in a stack as they execute, and the user can use the undo button to work back up the stack, returning the application to previous states (figure 3.3). Each new command is placed on the top of the stack, which may be undone item by item. The user creates a document by a series of write actions. Then she selects the entire document and accidentally hits the delete button. When she invokes the undo function, the topmost item is popped from the stack, and its
Copyright © OnBarcode.com . All rights reserved.