Archive for the 'Creational patterns' Category

23
Mar

Use singletons discreetly. Here are some tips to help you decide when to use the singleton pattern.

Singleton design pattern should be used discreetly. Many of us are building API’s and using singletons as a means to create a global object that can be accessed across the application.Here’s why you should use singleton discreetly:

  1. In many cases, using singleton is an anti-pattern;
  2. The singleton may be considered as an anti Action Script, since Action Script 3.0 doesn’t support private or protected constructor;
  3. With programming, it is usually not recommended to

use global objects, since it is hard to test them and the developer has to make assumptions regarding the application and how they will be used.

The main source of the problems is that using the singleton pattern makes the classes tightly coupled instead of loosely coupled, meaning that you can only test a few classes together rather than each of the classes at a time, as you should.

Before I go into a discussion on when is it a good idea to use the singleton pattern, I think it’s a good idea to show how the singleton is used in Action Script 3.0.

What is the singleton pattern?
The singleton pattern is used to restrict instantiation of a class to one object. The pattern keeps a private constructor to insure that the class was created only once.

Singleton in Action Script 3.0:
Action script does not support the option to declare constructor as private or protected inside of a package, which causes a design pattern problem, since any option you choose will be considered workaround or a “hack”.

One popular option is to use the inner classes, meaning creating another class inside of the package to behave as the private constructor, such as in the example below:

package {

	public class Manager
	{
		/**
		 * var to be used by the singleton pattern to return the instance of the class.
		 */
		private static var manager : Manager;

		/**
		 * Internal constructor. Should not be called from outside this class.
		 *
		 * @param enforcer	Enforce the creation of one class only.
		 *
		 */

		public function Manager(enforcer:AccessRestriction)
		{
			// TODO: throw exception
			if ( enforcer == null )
			{
				throw new Error("Manager error enforcer input param is undefined" );
			}
		}

		/**
		 * Method function to retrieve instance of the class
		 *
		 * @return The same instance of the class
		 *
		 */
		public static function getInstance() : Manager
		{
			if( manager == null )
				manager = new Manager(new AccessRestriction());

			return manager
		}
	}
}

class AccessRestriction {}

To view the source code as text file click here.

So when should you use the singleton design pattern?
Here’s a singleton recommended check list:

  1. Singleton should be used in cases where only one object is needed to coordinate actions and/or events across the application;
  2. The application will always use the object in the same way;
  3. It doesn’t really matter to the client what’s going on in the rest of the application, meaning that the singleton object is independent.

If you can confirm the check list, than it’s a good candidate to use the singleton pattern.

Have fun designing!

15
Mar

Boost Flex performance with Object Pooling Manager API

Object Pooling Manager API is an open source API that allows you to create a collection of objects and use them at any given time. These usable objects can be anything from a UI component to an XML list. The Object Pooling manager ensures the management of the collections of objects and allows the client to keep using them without costing any additional resources.

Flex is a single thread programming language which means that the program cannot split itself into more than one simultaneously tasks and it causes flex to slow down, especially when the cost of initializing a class is high. Object pooling can be used to manage object caching and boost performance.

A real life example is a book case. You have a case full of books and you decide to read one of the books. While you use the book you may stained a page by a coffee and changed the look of the book. After you are done using the book you return the book to the book case for later usage.

Object pooling works the same way, the object pooling class is singleton to insure you are not pulling more than one reusable object at a time. At any given time you pick a reusable object, the object is being removed from the collection so the client can change it and place it back for future usage.

The Flex API I developed is based on creating a usable collection of objects, the objects can be any component such as Flex Sprite or Canvas, array, class or anything you want it to be.

The object pooling can boost performance; especially in cases where the resources used to initializing a class instance are high, the number of times the class will be usage is often, and the number of instantiations in use at any single time is low.

All source code contained in this API has been published under the MIT licence and is protected as stated therein.

To see an example and download source code:
http://elromdesign.com/blog/Flex/API/ObjectPoolManager/

To view the ASDOC of all the API:
http://elromdesign.com/blog/Flex/API/asdoc/

Object Pooling UML

03
Mar

Custom cursor API is a great solution for creating sets of custom cursors

Custom cursor API is a great solution for creating sets of custom cursors. Each set is a concrete class that you can set. In addition, you can implement custom events such as: mouse down, mouse up and custom busy cursor.

CursorManager class is useful for replacing the cursor with a custom cursor or for creating a busy cursor, but what do you do when you want to create a set of custom cursors? You may also be interested in building few sets of custom cursors to be used in different part of the application and keep track of them in one centralized location.
For example you want to create couple of sets of cursors. One of them will be a custom image, the cursor will change on mouse down and mouse up events and the cursor will have a custom busy image. That set will be used in one component and another set will be used in a different component.

Custom cursor API is based on creating custom sets of cursors for usage in an application. You create a concrete class and set all the images, swf or sprite objects you want to assign to that set and the cursor will change according to how you set it in the concrete class.

All source code contained in this API has been published under the MIT licence and is protected as stated therein.

To see an example and download source code:
http://elromdesign.com/blog/Flex/API/Cursor/

To view the ASDOC of the API:
http://elromdesign.com/blog/Flex/API/asdoc/

Cursor UML Class diagram

21
Feb

SQLStatements Manager API will generate SQL commands for forms and other UI interactions.

Building a large Flex application that communicates with a remote server through SQL commands create an architect design problem. What do you do with all the SQL commands? Do you store them in a properties file or create 50-100 ColdFusion files? What do you do once you want to change some of these SQL commands?

The issue gets even worse once you need to submit few SQL commands for one operation. For example you have a form and once that form submit the client request to sign to a newsletter so you would need two SQL statements.

I created an elegant solution to that problem, utilizing the Abstract Method design pattern. The “SQLStatements Manager” (Flex SQL statements API) will let you create different CRUD (Create, Read, Update and Delete) statements for each form and you have one place where all your SQL commands are located so once it’s time to change them or update your database design, you don’t have to go through your entire application looking for all these SQL statements.

Once you have the SQL statement you can submit them to the database in order to process. I may give an example of implementation of the API using ColdFusion and Cairngorm.

To see an example and download source code:
http://elromdesign.com/blog/Flex/API/SQLStatementsManager/

To view the ASDOC of the API:
http://elromdesign.com/blog/Flex/API/asdoc/

This API will be in a new Flex 3.0 book I am writing called “Developing Enterprise Architect Applications.” click here to read more and pre-order.

All source code contained on this page has been published under the MIT license and is protected as stated therein.

SQLStatments Manager

15
Feb

Display different file types using FileViewer, factory pattern open source API

The API is responsible for displaying different files type. There are many scenarios where you want your application to use a different class or component to display different types of files. You can use “if-else” statements; however the problem is when you want to use different type of component or classes to handle the different types of files. Using the factory design pattern solve that problem; The concrete product needs to implements the “IView” interface and the component that is responsible for displaying the file can be of any type. “UIFileViewer” class is responsible for creating the component and adding it to the UIComponent.

This API will be in a new Flex 3.0 book I am writing called “Developing Enterprise Architect Applications.” click here to read more and pre-order.

All source code contained on this page has been published under the MIT licence and is protected as stated therein.

FileViewer UML

To see an example and download the source code click here:
http://elromdesign.com/blog/Flex/API/FileViewer/

ASDOC:
http://elromdesign.com/blog/Flex/API/asdoc/