spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / gr / column22 / 1 current pageTo page 2To page 3
[next]

Senior Consultant/Information Security - permanent position (TX)
Next Step Systems
US-TX-Irving

Justtechjobs.com Post A Job | Post A Resume
Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

Doodle, A Demo Drawing Program - Part 3

By Guyon Roche

Introduction

In Doodle - Part 2, the Doodle application gained an application framework with the document-view model. In this installment, Doodle starts to connect these components. Not much has changed visually except that once a line is drawn, Doodle allows the user to delete them by clicking on them (this will eventually be extended into a full selection mechanism).

Demonstration

Doodle - Part 3

Click and drag the mouse over the 'canvas' on the left to draw lines. Click on the lines to delete them.

Once again the internal workings of the Doodle code have been upated. The namespace technique has been extended and the Canvas class has gained some new subclasses to keep track of the onscreen objects.

Code Structure

In any large application it's important to use a programming style that's easy to read and maintain. The larger the application, the more important it becomes to impose a solid structure to the programming style. With this in mind, I've chosen a JavaScript class model developed for Preezo which supports single inheritance and simple, tightly bound class definition code.

While it may seem that the new class model has rendered the Doodle code unrecognizable, in practice, very little has changed: where once a class was created like this...

That same class is now created like this...

The new class model is based around the Class factory object contained within the namespace JSX. Its prime objective is to help make class oriented JavaScript programming more convenient. The entire class is bound together within a single object literal passed into the function called JSX.Class.create(). All aspects of the class are easy to find; the internal name of the class is useful for identifying the class by descendant classes, an optional base class, the constructor function, static members and class members typically follow. The most important benefit of this style of programming JavaScript is that all the details of the class are coded close together, useful when an application starts to grow complex with many tens or even hundreds of classes. Another stylistic benefit is that the symbol Doodle.Doc is now formally identified as a class and not just some function instance with a modified prototype.

Where methods were assigned to the class prototype, they're now included within the methods collection, reducing a large amount of the ClassName.prototype cruft.

Start With The Document

In general, when making global changes to document-view applications, I find it helps to begin with the document as this is the focus of most of the code in the application.

The following methods (added to the Doodle.Doc methods collection) control access to the shapes collection.

It's generally good coding practice to discourage users of a class from accessing and modifying class or instance member variables directly. Allowing access to member variables creates dependencies on implementation that may be difficult to break later on. It's better to provide functions like the four above that perform the required tasks. This allows the implementation to be changed without much impact on other code.

Would You Like A Little Java With Your Script?

The next modification is the addition of a class heirarchy for Doodle document shape objects. While the Doodle application supports only one shape (at the moment), it's a good time to set up the structure of a document shape class heirarchy before things get too complicated. The ideal class name for a shape class would be simply "Shape" but there may be other shape oriented classes in the Doodle application that could be called "Shape." This will also be true as we develop the shape heirarchy further with classes like "Line, Ellipse" and so on. The solution here is to use the namespacing technique once more so that the document's shape class will be called Doodle.Doc.Shape and the graphics library's shape will be JSX.Graphics.Shape.

Some readers may notice a passing resemblance to the Java language with its dot-delimited naming heirarchies. One might wonder if enabling this notation was intentional on the part of the JavaScript language designers.

The name field here isn't the fully qualified 'Doodle.Doc.Shape' but simply the short name 'Shape' which descendants from the document shape class will use to uniquely identify this class.

Notice also the "Types" element in the statics collection. The purpose of this is to act as an enumeration of different shape types. Each item in the object literal equates a name with a number. By convention, the name (e.g. LINE) is written in uppercase. Since it's included in the statics collection, the Types enumeration will be added as a property of the Doodle.Doc.Shape class itself and not to the class prototype so it can be accessed easily without requiring a Shape instance. So far there's only one shape type enumeration, but as each new shape type is added, a new enumeration value will be included here.

The Shape class maintains a numerical ID value which will be used to distinguish between different shape instances.

The document Line class now inherits from Shape. Inheritance is specified by adding a base class link in the class definition.

Notice that with the inheritance model used in this code, the Line constructor must call the Shape constructor explicitly since this isn't done automatically by the inheritance model. This gives the Line class the opportunity to choose alternate arguments to be passed to the Shape base class.

home / programming / javascript / gr / column22 / 1 current pageTo page 2To page 3
[next]

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
  Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Fixing MySQL Replication · Firewall Guide: First Steps to Securing the Enterprise · VoxOx Tames the Tumultuous Communications Tangle

Created: October 13, 2006
Revised: November 29, 2006

URL: http://webreference.com/programming/javascript/gr/column22/1