Skip to content
September 29, 2008 / Shrikant Patil

Abstract Factory Method: Flex Application using Abstract Factory Method Pattern

I am much exited to learn the design patterns. I have posted the simple Factory and Abstract Factory patterns in previous posts. The real world example gives the real usage of the design patterns. So I started here with a ‘Ice-Cream Shop” Application. The shop contains different king of ice creams. In this application client can submit an order for ice cream and application uses the Abstract Factory pattern to create the ice cream order objects.
Here is the flow diagram of the application.
As we can see from the above diagram, The client (ice cream shop) make a request to the Abstract Factory class, which is Abstract Class and defines a Abstract Factory method “processOrder()” which is get overridden by the subclasses. The class also defines “takeOrder()” method which take two parameters of order name and quantity of the order. The another method “getBill()” is used to get the bill for the order. The Abstract Factory method of the class returns a concrete Product object which implements the “IIcecreamOrder” interface, back to the client (shop).
Let us start coding the application:

Creating Products classes:

All order (product) object of our application implement the IIcecreamOrder interface, which has the signature as below;


package flexScript
{
    public interface IIcecreamOrder
    {
        function calculateBill():int
        function processOrder(order:String, qty:int):void;
    }
}

Lets start creating order classes of the application implementing the above interface; In our application we have two types of order objects 1) Vanilla and 2) Pista

package flexScript
{
    public class VenillaOrder implements IIcecreamOrder
    {
        public var qunatity:int;
        public var price:int = 20;
        public var bill:int;
        public function processOrder(order:String, qty:int):void
        {
            trace("Processing Venilla Order :"+order);
            qunatity = qty;
        }
        public function calculateBill():int{
            bill = price * qunatity;
            return bill;
        }

    }
}
package flexScript
{
    public class PistaOrder implements IIcecreamOrder
    {
        public var qunatity:int;
        public var price:int = 12;
        public var bill:int;
        public function processOrder(order:String, qty:int):void
        {
            trace("Processing Pista Order:"+order);
            qunatity = qty;

        }
        public function calculateBill():int{
            bill = price * qunatity;
            return bill;
        }

    }
}

Creating Creators classes:
At first we need to create an Abstract class which defines a abstract Factory method, which is overridden in subclasses.

package flexScript
{
    import flash.errors.IllegalOperationError;

    //Abstract Creator Class(can be subclassed not instantied
    public class IceCreamFactory
    {
        private var IceCreamOrder:IIcecreamOrder;

        public function takeOrder(order:String, qty:int):void{
            IceCreamOrder = this.procesOrder();
            IceCreamOrder.processOrder(order, qty);

        }
        //Abstract Factory Method
        protected function procesOrder():IIcecreamOrder{
            throw new Error("Abstract Method");
            return null;
        }
        public function getBill():int{
            return IceCreamOrder.calculateBill();
        }

    }
}

Let us start with creating concrete Creator classes by extending the base Abstract class. Note the concrete creators only override the Abstract Factory method defined in base Abstract class.

package flexScript
{
    public class VenillaOrderFactory extends IceCreamFactory
    {
        override protected function procesOrder():IIcecreamOrder{
            trace("Creating new Venilla Order");
            return new VenillaOrder();
        }
    }
}
package flexScript
{
    public class PistaOrderFactory extends IceCreamFactory
    {
        override protected function procesOrder():IIcecreamOrder{
            trace("Creating new Pista Order");
            return new PistaOrder();
        }

    }
}

Creating Client (ice Cream Shop):
The client is nothing but application file of the flex application. It contains a form which display to choose a ice cream type to order and quantity. After clicking on Submit order. The Description box displays the Bill of the order.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" creationComplete="init()">
    <mx:Script>
        <!&#91;CDATA&#91;
            import flexScript.*;

            private function init():void{

            }
            private function placeOrder():void{
                var orderFor:IceCreamFactory;
                switch(orderChooser.selectedLabel){
                    case "Venilla": orderFor = new VenillaOrderFactory();
                                    break;
                    case "Pista":    orderFor = new PistaOrderFactory();
                                    break;
                }
                orderFor.takeOrder(orderChooser.selectedLabel,int(qty.text));
                desc.text = "Bill :"+orderFor.getBill()
            }
        &#93;&#93;>
    </mx:Script>
    <mx:VBox>
        <mx:Form>
            <mx:FormHeading label="Ice Cream Shop"/>
            <mx:FormItem label="Order :">
                <mx:ComboBox id="orderChooser">
                    <mx:dataProvider>
                        <mx:String>Venilla</mx:String>
                        <mx:String>Pista</mx:String>
                    </mx:dataProvider>
                </mx:ComboBox>
            </mx:FormItem>
            <mx:FormItem label="Quantity :">
                <mx:TextInput id="qty"/>
            </mx:FormItem>
            <mx:FormItem>
                <mx:Button label="Submit Order" click="placeOrder()"/>
            </mx:FormItem>
            <mx:FormItem>
                <mx:TextArea id="desc"/>
            </mx:FormItem>
        </mx:Form>
    </mx:VBox>
</mx:Application>

This is my starting point of design patterns, i am not an expert. I am trying and sharing what I am learning. If any mistakes, errors occurred please let me know, so that I can understand it better.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: