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 DesignStep 6 :: DevelopmentStep 7 :: TestStep 8 :: DeploymentStep 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();
}
}
- Accesing resources in shared mode- Configuration Classes- Logger Classes- Serialization- Network Port Interface
Singleton Design Pattern |
- using System;
- namespace DesignPatternsSingleton
- {
- /// <summary>
- /// MainApp startup class for Design Patterns :: Singleton
- /// </summary>
- class MainApp
- {
- /// <summary>
- /// The Singleton class.
- /// </summary>
- public class Singleton
- {
- private static Singleton _instance;
- // Constructor is 'protected'
- protected Singleton()
- {
- }
- public static Singleton Instance()
- {
- // Uses lazy initialization.
- // Note: this is not thread safe.
- if (_instance == null)
- {
- _instance = new Singleton();
- }
- return _instance;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Threading;
- namespace DesignPatternsSingleton
- {
- /// <summary>
- /// MainApp startup class for Real-World
- /// Singleton Design Pattern.
- /// </summary>
- class MainApp
- {
- /// <summary>
- /// Entry point into console application.
- /// </summary>
- static void Main()
- {
- LoadBalancer b1 = LoadBalancer.GetLoadBalancer();
- LoadBalancer b2 = LoadBalancer.GetLoadBalancer();
- LoadBalancer b3 = LoadBalancer.GetLoadBalancer();
- LoadBalancer b4 = LoadBalancer.GetLoadBalancer();
- // Same instance?
- if (b1 == b2 && b2 == b3 && b3 == b4)
- {
- Console.WriteLine("Same instance\n");
- }
- // Load balance 15 server requests
- LoadBalancer balancer = LoadBalancer.GetLoadBalancer();
- for (int i = 0; i < 15; i++)
- {
- string server = balancer.Server;
- Console.WriteLine("Dispatch Request to: " + server);
- }
- // Wait for user
- Console.ReadKey();
- }
- }
- /// <summary>
- /// The 'Singleton' class
- /// </summary>
- class LoadBalancer
- {
- private static LoadBalancer _instance;
- private List<string> _servers = new List<string>();
- private Random _random = new Random();
- // Lock synchronization object
- private static object syncLock = new object();
- // Constructor (protected)
- protected LoadBalancer()
- {
- // List of available servers
- _servers.Add("ServerI");
- _servers.Add("ServerII");
- _servers.Add("ServerIII");
- _servers.Add("ServerIV");
- _servers.Add("ServerV");
- }
- public static LoadBalancer GetLoadBalancer()
- {
- // Support multithreaded applications through
- // 'Double checked locking' pattern which (once
- // the instance exists) avoids locking each
- // time the method is invoked
- if (_instance == null)
- {
- lock (syncLock)
- {
- if (_instance == null)
- {
- _instance = new LoadBalancer();
- }
- }
- }
- return _instance;
- }
- // Simple, but effective random load balancer
- public string Server
- {
- get
- {
- int r = _random.Next(_servers.Count);
- return _servers[r].ToString();
- }
- }
- }
- }
No comments:
Post a Comment