Thursday 27 October 2011

Creational Pattern - A Close Look

 Ever Wondered in  a new project while creating a new module or a page that would have given a compilation error or a screen pops up without any default data being loaded in the master controls.

 We know consciously or unconsciously that we did not implement an interface or did not implement a  default method or set a value in default property.


   We have been there many times. I would say most of this issue happens either the COMPILATION ERROR or a BLANK SCREEN WITHOUT DATA are due to following reasons.


  • We did not understand what they have implemented in different layers.
  • Current project is not designed using any design pattern, that force the user to implement certain methods by default, eg. say Interface
  • Multiple patterns are used which solves different problem and we cannot figure out what is the actual purpose of the pattern which is used to solve the respective problems.
 Here we will see real time example where different pattern are used to get solution for different type of problems.


 Let us consider my previous project about travel agent for booking airline tickets. This is the biggest project i have worked till  now. We will only talk about scope which will be useful for us in explaining our Creational Pattern.

Problem Statement:


      Travel agent named OBTS(Osaka Business Travel System) manages travel needs of customers. Their main business is to take care of business travels. They reserve/book tickets for airlines/Hotel/Car that is needed for business travel. They also take care of Round trip of WORLD(RTW).


 For example: Business user and his wife flies from Japan to California takes a Rental car drives to Santa Clara, catches a flight and travels to Singapore drops his wife there and travels back to Japan. In Singapore he stays for two days.


OBTS Travel Agent will gather details about the person who travel and reserves Air/Hotel/Car. The details about the traveller and travel should be saved in our local database before booking the travel itinerary. The agent who booked the itinerary should also be saved in the itinerary.


Note: For reserving tickets in various geographical regions we need to access different servers.


Amadeus - North/South America
Abacus - Asia Pacific 
AXES - Japan/Korea
Galileo - Europe


These are commonly used servers. Each servers have different MODES OF COMMUNICATION


Amadeus - XML
Abacus - Webservice
AXES - MQ Series
Galileo - HTTP


We will discuss two CREATIONAL PATTERN which was used to solve the problem.


  1. Abstract Factory Pattern
  2. Singleton Pattern 
Abstract Factory Pattern: 


     Abstract Factory Pattern means providing an interface for creating families of related or dependent objects without specifying their concrete classes.


 In our real time example we will solve the following problem of booking tickets in different servers. Above itinerary has to be booked in different servers with different mode of inputs. Data will be same, but data format and way of communication is different for each server of same itinerary.


Japan to California - AXES
California  to Santa Clara - Amadeus
Santa Clara to Singapore - Abacus


Our first step is build a interface which has the following method that need to Book/Update/Cancel tickets. This will be the object type that will be returned when we try to Create/Modify tickets. We will name it as  IAirlineServerFactory


    public interface IAirlineServerFactory
    {
        bool BookTicket(ItineraryInfo itinerary);
        bool UpdateTicket(ItineraryInfo itinerary);
        bool CancelTicket(ItineraryInfo itinerary);

    }


IAirlineServerFactory will be the interface which gives us the objects by hiding underlying Concrete Class. Next step is to create Concrete class which will have implementation for all different servers. All the implemented class should inherit our IAirlineServerFactory.
 
    public class AbacusServer : IAirlineServerFactory
    {
        public bool BookTicket(ItineraryInfo itinerary)
        {
            //Implementation Here
            return true;
        }
        public bool UpdateTicket(ItineraryInfo itinerary)
        {
            //Implementation Here
            return true;
        }
        public bool CancelTicket(ItineraryInfo itinerary)
        {
            //Implementation Here
            return true;
        }
    }
For simplicity, we will consider only AbacusServer where the data communication is through webservice. Now we need to handle the  creation of Classes for booking tickets for different Server.
    public class AirlineServer
    {

        public static IAirlineServerFactory CreateAirlineServerFactory(String serverType)
        {
            IAirlineServerFactory airlineServer= null;

            if (serverType == "Axes")
            {
                airlineServer = (IAirlineServerFactory)new AxesServer();
            }
            else if (serverType == "Abacus")
            {
                airlineServer = (IAirlineServerFactory)new AbacusServer();
            }
            else if (serverType == "Amadeus")
            {
                airlineServer = (IAirlineServerFactory)new AmadeusServer();
            }
            else if (serverType == "Galileo")
            {
                airlineServer = (IAirlineServerFactory)new GalileoServer();
            }
            return airlineServer;

        }
        
    }


AirlineServer Class acts as factory that actually creates the dependent object based on the servertype. Let us look into FlightItinerary which consumes the class for booking tickets. .

    public class FlightItinerary
    {
        public bool BookTickets(ItineraryInfo itinerary, String serverType)
        {
            IAirlineServerFactory airlineServer = AirlineServer.CreateAirlineServerFactory(serverType);
            return airlineServer.BookTicket(itinerary);
        }
    }
 Thus the concrete class implementation is masked by the IAirlineServerFactory, so we get to book ticket without knowing the implementation of server. To FlightItinerary, it is AirlineServer that has been exposed.


Let us look into second part of the problem


The details about the traveller and travel should be saved in our local database before booking the travel itinerary. The agent who booked the itinerary should also be saved in the itinerary.


We can solve this problem using another Creational pattern named as Singleton Pattern


  Singleton pattern will ensure a class has only one instance and it has only one global entry Point.


We need to save the itinerary in our local database with the details of the agent who booked the ticket.  The details about the agent will always be the same until he logs off. So we dont need to access the database everytime a ticket is booked for agent details.


    public class AgentInfo
    {
        private static AgentInfo _instance;


        public static AgentInfo  GetAgentInfo()
        {
            if (_instance == null)
            {
                _instance = new AgentInfo();
                //Implementation for setting AgentDetails
            }
            return _instance;
        }

    }

  Whenever the agent info needs to saved in the database the application will be get the instance from GetAgentInfo(), which in turn will return new instance if it is for the first time or already created instance if the instance is already created.

Thus booking ticket in different server is handled using Abstract Factory pattern and the user details that needs to be retrieved everytime,  for saving in the local database have been handled by Singleton pattern.

Monday 24 October 2011

Simple understanding of Design Pattern

Reason for writing this blog about Design Pattern is that it has been Greek, Latin and Hebrew for me  long time. I figured out, from some of my friends that you need to know about Design Patterns if u want to excel in Technical career. I told my self OK, am gonna figure out this myself. So i downloaded a 400 pages PDF book about Design patterns and read the whole book.

     I felt only one thing. this is really Greek and Latin.

   Actually i heard the word for the first time about design pattern from my client's architect Doi San, when i was working for a project in Japan. He said we use combined Data factory and Singleton pattern in this project. I could relate to Singleton pattern from the project, because the logged in details about the user, will be fetched from database for the first time, when the user logs in and the user object is created and saved.

 Later if we need the user details, the object will not be instantiated, it will send the object that was already created and saved, that way, the database call is only once.

   I could not understand what is Data Factory pattern for a long time even though i implemented almost 5 modules in the project throughout my 6 months tenure in Osaka. I will explain about this later, about this pattern.

  Then I tried using wikipedia to learn it in a layman's term, but no use. Every resource i referred gave me the same definition. So I went back to 400 Page PDF book.

I read the first chapter at least five time to figure out what exactly it means, once i figured out the definition by myself. It became a cake walk for me.

 So I thought, i have figured it out, why not represent in a way that will be simple & easy for others to learn, so they don't have to go through the pain i went through.

 So i have put up this blog which will be useful for others. Stick with me in reading this. You can take these below as guidelines and remember these and you can master it from there. Because first step is always the tough step.

My read of design pattern is
" Design Pattern is set of solutions available for set of problems ".

 The Gang of Four (GOF) patterns are generally considered the foundation for all other patterns. . We will discuss only  about two principles which i feel very important.
  • High Cohesion (Cohesion  meaning Relationship between methods in a class)
  • Low Coupling  (Coupling meaning Dependency between two or more classes)
Remember the above two(also the definition) which is very important, if u are interested you can read more of GOF principles.

GOF is categorized into three

  • Creational
  • Behavioral
  • Structural
Way to Remember this is Chocolates are Bitter and Sweeter.

Chocolates - CREATIONAL
Bitter          - BEHAVIORAL
Sweeter      - STRUCTURAL

What is Creational Pattern ?
                      Solution that denotes how objects should be created to solve a problem
What is Behavioral Pattern ?
                    Solution that denotes how objects should be Communicated to solve a problem
What is Structural Pattern ?
                    Solution that denotes how entities should be related to solve a problem

    If you can remember these definitions and relate what it means, then you are half way through. Let us discuss about each and every category in following blogs.






Build Bot using LUIS