Monday, July 25, 2016

Subjects


Subject 1: What is a Programming Framework and why do developers use .NET Framework, Java framework(s), etc.?

Subject 2: What is O Notation and why does it have a "critical" role in software development?

Subject 3: What should developers avoid in writing code?

Subject 4: Solid Skills in OOD & OOP. [Part 1]


Precise Definitions for OOD and OOP: OOD is the modeling methodology of OOP; where, OOP is a programming model, in which identifying objects and recognizing its data pieces known as properties are the primary steps to generalize each object as a class. Once a class is plotted, each distinct logic sequence is considered for it as a method (like an action in a non-OOP world). Since a class, by its nature, does exist in one or more hierarchical systems, its positions and relationships are detected to provide the possibility of employing concepts of inheritance, abstraction, polymorphism (including overloading & overriding), etc. to optimize the code in terms of reusability, data hiding (encapsulation) and avoiding duplicate code snippets. 

SDLC (Quick Reference)
 
Step 1 :: Plot (Defining/Planning + Mockup Screens)
Step 2 :: Requirement Analysis [+ modeling]
Step 3 :: System Analysis [+ modeling]
Step 4 :: System Design [+ modeling]
Step 5 :: UI Design
Step 6 :: Development
Step 7 :: Test
Step 8 :: Deployment
Step 9 :: Maintenance ==> Evolution :: Go To Step 1 or 2.


Appendix 1: Naming Conventions

Pascal Casing: The first character of all words are Upper Case and other characters are lower case. 
Example: BulkStreamReader

Camel Casing: The first character of all words, except the first word are Upper Case and other characters are lower case.
Example: bulkStreamReader

Hungarian Notation: (not recommended) The name of a variable or function indicates its type or intended use.
Examples:
lAccountNum : variable is a long integer ("l");
arru8NumberList : variable is an array of unsigned 8-bit integers ("arru8");
m_sFirstName : a string member variable for "first name".


Design Patterns: Abstract Factory:


Abstract Factory - Analogy to Digest the Pattern:

 

    /// <summary>

    /// Abstract Product A.

    /// </summary>

    public abstract class RallyRacingCar

    {

        public abstract void Pass(RoadRacingCar roadRacingCar);

    }

 

    /// <summary>

    /// Abstract Product B.

    /// </summary>

    public abstract class RoadRacingCar

    {

        public abstract void Pass(RallyRacingCar rallyRacingCar);

    }

 

    /// <summary>

    /// Product A 1.

    /// </summary>

    public class Porsche997GT3 : RallyRacingCar

    {

        public override void Pass(RoadRacingCar roadRacingCar)

        {

            Console.WriteLine("\nPorsche 997 GT3 passed a road racing car of type: " + roadRacingCar.GetType().Name);

        }

    }

 

    /// <summary>

    /// Product A 2.

    /// </summary>

    public class LotusExige360 : RallyRacingCar

    {

        public override void Pass(RoadRacingCar roadRacingCar)

        {

            Console.WriteLine("\nLotus Exige 360 passed a road racing car of type: " + roadRacingCar.GetType().Name);

        }

    }

 

    /// <summary>

    /// Product B 1.

    /// </summary>

    public class BMW135i : RoadRacingCar

    {

        public override void Pass(RallyRacingCar rallyRacingCar)

        {

            Console.WriteLine("\nBMW 135i passed a rally racing car of type: " + rallyRacingCar.GetType().Name);

        }

    }

  

 

    /// <summary>

    /// Product B 2.

    /// </summary>

    public class HondaCivicSi : RoadRacingCar

    {

        public override void Pass(RallyRacingCar rallyRacingCar)

        {

            Console.WriteLine("\nHonda Civic Si passed a rally racing car of type: " + rallyRacingCar.GetType().Name);

        }

    }

 

 

    /// <summary>

    /// Abstract Factory.

    /// </summary>

    public abstract class CarFactory

    {

        public abstract RallyRacingCar CreateRallyRacingCar();

        public abstract RoadRacingCar CreateRoadRacingCar();

    }

 

 

    /// <summary>

    /// Concrete Factory 1.

    /// </summary>

    public class EuropeCarFactory : CarFactory

    {

        public override RallyRacingCar CreateRallyRacingCar()

        {

            return new Porsche997GT3();

        }

 

        public override RoadRacingCar CreateRoadRacingCar()

        {

            return new BMW135i();

        }

    }

 

    /// <summary>

    /// Concrete Factory 2.

    /// </summary>

    public class AmericaCarFactory : CarFactory

    {

        public override RallyRacingCar CreateRallyRacingCar()

        {

            return new LotusExige360();

        }

 

        public override RoadRacingCar CreateRoadRacingCar()

        {

            return new HondaCivicSi();

        }

    }

 

    /// <summary>

    /// Client.

    /// </summary>

    public class Consumer

    {

        private RallyRacingCar myRallyRacingCar;

        private RoadRacingCar myRoadRacingCar;

 

         // Constructor

         public Consumer(CarFactory factory)

         {

            myRallyRacingCar = factory.CreateRallyRacingCar();

            myRoadRacingCar = factory.CreateRoadRacingCar();

        }

 

 

         public void DriveRoadRacingCar()

         {

             myRoadRacingCar.Pass(myRallyRacingCar);

         }

 

         public void DriveRallyRacingCar()

         {

             myRallyRacingCar.Pass(myRoadRacingCar);

         }

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            // Abstract Factory No. 1.

            CarFactory factory1;

            // Abstract Factory No. 2.

            CarFactory factory2;

 

            // Drive 2 cars made in Europe.

            factory1 = new EuropeCarFactory();

            Consumer consumer1 = new Consumer(factory1);

            consumer1.DriveRallyRacingCar();

            consumer1.DriveRoadRacingCar();

 

            // Drive 2 cars made in America.

            factory2 = new AmericaCarFactory();

            Consumer consumer2 = new Consumer(factory2);

            consumer2.DriveRallyRacingCar();

            consumer2.DriveRoadRacingCar();

 

            Console.ReadKey();

        }

    }

 

UML Notations for Classes and Tables - pdf


Design Patterns: Singleton

There are many common situations when singleton pattern is used:
 - Accesing resources in shared mode
 - Configuration Classes
 - Logger Classes
 - Serialization
 - Network Port Interface

Singleton Design Pattern

Singleton Design Pattern
Check list
1.Define a private static attribute in the "single instance" class.
2.Define a public static accessor function in the class.
3.Do "lazy initialization" (creation on first use) in the accessor function.
4.Define all constructors to be protected or private.
5.Clients may only use the accessor function to manipulate the Singleton.
 
  1. using System;

  2. namespace DesignPatternsSingleton
  3. {
  4.   /// <summary>
  5.   /// MainApp startup class for Design Patterns :: Singleton
  6.   /// </summary>
  7.   class MainApp
  8.   {
  9.   /// <summary>
  10.   /// The Singleton class.
  11.   /// </summary>
  12.   public class Singleton
  13.   {
  14.     private static Singleton _instance;

  15.     // Constructor is 'protected'
  16.     protected Singleton()
  17.     {
  18.     }

  19.     public static Singleton Instance()
  20.     {
  21.       // Uses lazy initialization.
  22.       // Note: this is not thread safe.
  23.       if (_instance == null)
  24.       {
  25.         _instance = new Singleton();
  26.       }

  27.       return _instance;
  28.     }
  29.   }
  30. }

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;

  4. namespace DesignPatternsSingleton
  5. {
  6.   /// <summary>
  7.   /// MainApp startup class for Real-World
  8.   /// Singleton Design Pattern.
  9.   /// </summary>
  10.   class MainApp
  11.   {
  12.     /// <summary>
  13.     /// Entry point into console application.
  14.     /// </summary>
  15.     static void Main()
  16.     {
  17.       LoadBalancer b1 = LoadBalancer.GetLoadBalancer();
  18.       LoadBalancer b2 = LoadBalancer.GetLoadBalancer();
  19.       LoadBalancer b3 = LoadBalancer.GetLoadBalancer();
  20.       LoadBalancer b4 = LoadBalancer.GetLoadBalancer();

  21.       // Same instance?
  22.       if (b1 == b2 && b2 == b3 && b3 == b4)
  23.       {
  24.         Console.WriteLine("Same instance\n");
  25.       }

  26.       // Load balance 15 server requests
  27.       LoadBalancer balancer = LoadBalancer.GetLoadBalancer();
  28.       for (int i = 0; i < 15; i++)
  29.       {
  30.         string server = balancer.Server;
  31.         Console.WriteLine("Dispatch Request to: " + server);
  32.       }

  33.       // Wait for user
  34.       Console.ReadKey();
  35.     }
  36.   }

  37.   /// <summary>
  38.   /// The 'Singleton' class
  39.   /// </summary>
  40.   class LoadBalancer
  41.   {
  42.     private static LoadBalancer _instance;
  43.     private List<string> _servers = new List<string>();
  44.     private Random _random = new Random();

  45.     // Lock synchronization object
  46.     private static object syncLock = new object();

  47.     // Constructor (protected)
  48.     protected LoadBalancer()
  49.     {
  50.       // List of available servers
  51.       _servers.Add("ServerI");
  52.       _servers.Add("ServerII");
  53.       _servers.Add("ServerIII");
  54.       _servers.Add("ServerIV");
  55.       _servers.Add("ServerV");
  56.     }

  57.     public static LoadBalancer GetLoadBalancer()
  58.     {
  59.       // Support multithreaded applications through
  60.       // 'Double checked locking' pattern which (once
  61.       // the instance exists) avoids locking each
  62.       // time the method is invoked
  63.       if (_instance == null)
  64.       {
  65.         lock (syncLock)
  66.         {
  67.           if (_instance == null)
  68.           {
  69.             _instance = new LoadBalancer();
  70.           }
  71.         }
  72.       }

  73.       return _instance;
  74.     }

  75.     // Simple, but effective random load balancer
  76.     public string Server
  77.     {
  78.       get
  79.       {
  80.         int r = _random.Next(_servers.Count);
  81.         return _servers[r].ToString();
  82.       }
  83.     }
  84.   }
  85. }


No comments:

Post a Comment