September 10, 2012

Object Oriented Programming Overview

This article covers the basics of software architecture. There are some good articles out there, but still developers struggle to understand the basic concepts, and more importantly, the way to apply them correctly. Newcomers will always struggle to understand a precise definition of a new concept, because it is always a new and hence unfamiliar idea. It will be difficult. When you start to design your very first system, you will try to apply everything you know or learned from everywhere.

The knowledge of the actions of great men, acquired by long experience in contemporary affairs, and a continual study of antiquity” – seems applicable here, isn’t it?

If you are a developer, who has a minimum of three or more years of continuous development experience and has that hunger to learn more, to step-in to the next level to become a software architect, this article is for you.

Software Architecture:
  1. Partitioning problems and system to be built in discrete pieces
  2. Techniques used to create interfaces between these pieces
  3. Easily manageable overall structure and flow
Why Architecture is important?
  1. User interface is loosely coupled and easy to change
  2. Main theme of software architecture is to define non-functional requirements of system and define the environment
  3. The detail design of system should follow the definition by which functional behaviors done within architecture rules
  4. Architecture is using because of:
    • Controls complexity
    • Enables re-use
    • Increase predictability
    • Give consistency
What is OOP?

OOP is a design philosophy.  Use different sets of programming languages. In OOP’s everything is grouped as Object. Able to gain reusability by means of four OOP’s concepts.
 
OOP’s Common Example:

Let’s take your hand as an example.
  1. The hand is a class
  2. Your body has two objects of type hand named Left Hand and Right Hand
  3. The function is managed by providing set of electric current from your shoulder to rest of body. Here’s shoulder is Interface which your body to interact with your hands
  4. The hand is a well architecture class
  5. The hand is being re-used to create Left Hand and Right Hand by slightly changes the properties of it
What is an Object?

Object can be considered as a thing that can performed related set of activities. The set of activities that can perform by object is called object’s behavior.

Object Example:
  1. Hand can grip something
  2. Student can give the name or address
           Here Hand and Student is a object

What is a Class?

It’s a blueprint / plan / template that described the details of object. Class is composed of three things.
  1. Name
  2. Attributes
  3. Operations
How to identify and design a Class?

Designing a Class is an art. Each designer uses different techniques to create / design class, however according to OODP (Object Oriented Design Principle) there are five principle that you must follow when design a class; 
  1. SRP – Single Responsibility Principle : A class should have one and only one reason to change
  2. OCP – Open Closed Principle : You should be able to extend class behavior without modifying it
  3. LSP – Liskov Substitution Principle : Derived classes must be substitutable for their base class
  4. DIP – Dependency Inversion Principle : Depend on abstractions, not on concretions
  5. ISP – Interface Segregation Principle : Make fine grained interfaces that are client specific
 What is Encapsulation?

It achieved mainly by creating classes, the classes expose public methods and properties. The class is a kind of container / cell / capsule which encapsulate set of method, attribute and properties to provide its intended functionalities to other classes.
What is Association?

It’s a (*a*) relationship between two classes. It allows one object instance to cause another to perform an action on its behalf. In more general term, it used to create relationship between classes where one class uses another class.
What is Aggregation?
It’s a (*the*) relationship between two classes, when object of one class has an (*has*) object of another. If second is the part of first, then it called aggregation.
What is Abstraction?
Abstraction is emphasis on the idea, qualities and properties rather than particulars. The important of abstraction is derived from its ability to hide irrelevant details. It is essential in the construction of programs. The primary means of managing complexity in the large systems. Abstraction is closely related to generalization.
What is Generalization?
 It reduces complexity by replacing multiple entities by which perform similar functions with a single construct.  The primary means of managing complexity by collecting individuals into groups and providing a representative which can be use to specify any individual of groups.
What is Abstract Class?
It can be only be use as a super class for other classes that can extend the abstract class.  They are ideal when implementing frameworks.
What is an Interface?
It separates the implementation and defines the structure. Interface can be used to define a generic template and then one or more abstract classes to define partial implementations of the interface. If the class that implements an interface doesn’t define all the methods of interface then it must be declared as abstract class and method definition must be provided by the subclass that extends the abstract class.
What is an Class?
Class can be defined to implement an interface and also it supports multiple implementations. When class implements an interface, an object of such class can be encapsulate inside an interface.

Differences between Class & Interface?
Interface definition begins with the keyword interface so it is of type interface
Abstract Classes are declared with the abstract keyword so it is of type class
It has no implementation but they have to implemented
There’s method can have implementations and they have to be extended
It can inherit more than one interfaces
It can implement more than one interfaces but inherit only with one class
It have only method declaration
There’s method can’t have implementation only when declared abstract

What are implicit and explicit interface implementations?
The concept of implicit and explicit interface implementation provide safe way to implement methods of multiple interfaces by hiding, exposing and preserving identities of each of interface methods, even when method signatures are same.

What is Inheritance?
Ability to new class to be created, from an existing class by extending it is called Inheritance. Inheritance is closely related to specialization.
Example :
public class Exception
{
      // Parent Class
}
public class IOException : Exception
{
      // Child Class inherit with Parent Class
}

What is Specialization?
It is described as (is-a) relationship. When we say that Dog is mammal, we mean that Dog is specialized kind of mammal which contains all characteristics of any kind mammals, but it specializes these characteristics to familiar characteristics.
What is Polymorphism?
A generic term means Many Shapes. In OOP’s, define as the ability to request that the same operations be performed by a wide range of different types of things.
What is Method Overloading?
Ability to define several methods all with the same name.

Example :
public class MyLogger
{
    public void LogError(Exception e)
    {
        // Implementation goes here   

    }

    public bool LogError(Exception e, string message)
    {
        // Implementation goes here   

    }
}
What is Operator Overloading?

The operator overloading (less commonly known as ad-hoc polymorphisms) is a specific case of polymorphisms in which some or all of operators like +, - or == are treated as polymorphic functions and as such have different behaviors depending on the types of its arguments.
Example :
public class Complex
{
    private int real;
    public int Real
    { get { return real; } }

    private int imaginary;
    public int Imaginary
    { get { return imaginary; } }

    public Complex(int real, int imaginary)
    {
        this.real = real;
        this.imaginary = imaginary;
    }

    public static Complex operator +(Complex c1, Complex c2)
    {
        return new Complex(c1.Real + c2.Real, c1.Imaginary + c2.Imaginary);
    }
}

What is Method Overriding?
Method overriding is a language feature that allows a subclass to override a specific implementation of a method that is already provided by one of its super-classes.
Example :
public class Complex
{
    private int real;
    public int Real
    {
get { return real; } }
    private int imaginary;
    public int Imaginary
   { get { return imaginary; } }

    public Complex(int real, int imaginary)
    {
        this.real = real;
        this.imaginary = imaginary;
    }

    public static Complex operator +(Complex c1, Complex c2)
    {
        return new Complex(c1.Real + c2.Real, c1.Imaginary + c2.Imaginary);
    }

    public override string ToString()
    {
        return (String.Format("{0} + {1}i", real, imaginary));
    }
}
What is Use – Case?
A use case is a thing an actor perceives from the system. A use case maps actors with functions. A use case encodes a typical user interaction with the system. In particular, it:
  1. Captures some user-visible function.
  2. Achieves some concrete goal for the user.






What is Class Diagram?
A class diagrams are widely used to describe the types of objects in a system and their relationships. Class diagrams model class structure and contents using design elements such as classes, packages and objects. Class diagrams describe three different perspectives when designing a system, conceptual, specification, and implementation. These perspectives become evident as the diagram is created and help solidify the design.













What is Package Diagram?
Package diagrams are used to reflect the organization of packages and their elements. When used to represent class elements, package diagrams provide a visualization of the name-spaces.
What is Sequence Diagram?
A sequence diagrams model the flow of logic within a system in a visual manner, it enable both to document and validate your logic, and are used for both analysis and design purposes. Sequence diagrams are the most popular UML artifact for dynamic modeling, which focuses on identifying the behavior within your system.
What is Two-Tier Architecture?

The two-tier architecture is refers to client/ server architectures as well. According to the modern days use of two-tier architecture the user interfaces (or with ASP.NET, all web pages) runs on the client and the database is stored on the server. The actual application logic can run on either the client or the server. So in this case the user interfaces are directly access the database. Those can also be non-interface processing engines, which provide solutions to other remote/ local systems. In either case, today the two-tier model is not as reputed as the three-tier model. The advantage of the two-tier design is its simplicity, but the simplicity comes with the cost of scalability. The newer three-tier architecture, which is more famous, introduces a middle tier for the application logic.








What is Three – Tier – Architecture?
Three-tier is a client-server architecture in which the user interface, functional process logic, data storage and data access are developed and maintained as independent modules, some time on separate platforms. The 3-Tier architecture has the following three tiers.
  1. Presentation Tier or Web Server: User Interface, displaying/ accepting data/ input to/ from the user.
  2. Application Logic/ Business Logic/ Transaction Tier or Application Server: Data validation, acceptability check before being added to the database and all other business/ application specific operations.
  3. Data Tier or Database Server: Simple reading and writing method to database or any other storage, connection, command, stored procedures etc.

















What is MVC architecture?

The Model-View-Controller (MVC) architecture separates the modeling of the domain, the presentation, and the actions based on user input into three separate classes.
  1. Model : DataSet and typed DataSet (some times business object, object collection, XML etc) are the most common use of the model.
  2. View : The ASPX and ASCX files generally handle the responsibilities of the view.
  3. Controllers : The handling of events or the controlling is usually done in the code-behind class.












What is SOA?

A service-oriented architecture is essentially a collection of services. These services communicate with each other. The communication can involve either simple data passing or it could involve two or more services coordinating some activity. Some means of connecting services to each other is needed.
What is Data Access Layer?

The data access layer (DAL), which is a key part of every n-tier system, is mainly consist of a simple set of code that does basic interactions with the database or any other storage device. These functionalities are often referred to as CRUD (Create, Retrieve, Update, and Delete).
 
What is Business Logic Layer?

I have become aware that not everyone agrees to what business logic actually is, and in many cases it's just the bridge in between the presentation layer and the data access layer with having nothing much, except taking from one and passing to the other. 
As a general advice when you define business entities, you must decide how to map the data in your tables to correctly defined business entities. The business entities should meaningfully define considering various types of requirements and functioning of your system. It is recommended to identify the business entities to encapsulate the functional/ UI (User Interface) requirements of your application, rather than define a separate business entity for each table of your database.
What are GoF (Gang of Four Design Pattern)?
The Gang of Four (GoF) patterns are generally considered the foundation for all other patterns. They are categorized in three groups: Creational, Structural, and Behavioral. Here you will find information on these important patterns.

1) Creational Patterns :
             Abstract Factory Creates an instance of several families of classes
             Builder Separates object construction from its representation
             Factory Method Creates an instance of several derived classes
             Prototype A fully initialized instance to be copied or cloned
             Singleton A class of which only a single instance can exist
2) Structural Patterns :
             Adapter Match interfaces of different classes
             Bridge Separates an object’s interface from its implementation
             Composite A tree structure of simple and composite objects
             Decorator Add responsibilities to objects dynamically
             Facade A single class that represents an entire subsystem
             Flyweight A fine-grained instance used for efficient sharing
             Proxy An object representing another object
3) Behavioral Patterns :
             Chain of Resp. A way of passing a request between a chain of objects
             Command Encapsulate a command request as an object
             Interpreter A way to include language elements in a program
             Iterate Sequentially access the elements of a collection
             Mediator Defines simplified communication between classes
             Memento Capture and restore an object's internal state
             Observer A way of notifying change to a number of classes
             State Alter an object's behavior when its state changes
             Strategy Encapsulates an algorithm inside a class
             Template Method Defer the exact steps of an algorithm to a subclass
             Visitor Defines a new operation to a class without change
Difference between Abstract Factory and Builder Design Pattern?
The two design patterns are fundamentally different. However, when you learn them for the first time, you will see a confusing similarity. So that it will make harder for you to understand them. But if you continue to study eventually, you will get afraid of design patterns too. If you observe the figure carefully, you will see an easily understandable color pattern.




























Please follow up with the numbers in the image when reading the listing below. 
  1. Both patterns have used a generic class as the entry-class. The only difference is the name of the class. One pattern has named it as “Client”, while the other named it as “Director”. 
  2. Here again the difference is the class name. It is “AbstractFactory” for one and “Builder” for the other. Additionally both classes are of type abstract.
  3. Once again both patterns have defined two generic (WindowsFactory & ConcreteBuilder) classes. They both have created by inheriting their respective abstract class.
  4. Finally, both seem to produce some kind of a generic output.
Let’s compare the two again side by side for one last time, but this time, focusing on the differences.
Abstract Factory : Emphasizes a family of product objects
Builder                 : Focuses on constructing a complex object step by step

Abstract Factory : Focus on *what* is made
Builder                 : Focus on *how* it is made

Abstract Factory : Focus on defining many different types of *factories* to build many *products*, and it is not a one builder for just one product
Builder                 : Focus on building a one complex but one single *product*

Abstract Factory : Defers the choice of what concrete type of object to make until run time
Builder                 : Hide the logic/ operation of how to compile that complex object

Abstract Factory : *Every* method call creates and returns different objects
Builder                 : Only the *last* method call returns the object, while other calls partially build the object

Abstract Factory, Builder, and Prototype can use Singleton in their implementations. So the conclusion would be that the two design patterns exist to resolve two type of business problems, so even though they look similar, they are not.
What is Implementation or Behavior?
The code inside the method is called Implementation / Behavior.
What is Early Binding?
Early binding means that our code directly interacts with the object, by directly calling its methods. Early binding also allows the IDE to use IntelliSense to aid our development efforts; it allows the compiler to ensure that we are referencing methods that do exist and that we are providing the proper parameter values.
What is Late Binding?
Late binding means that our code interacts with an object dynamically at run-time. This provides a great deal of flexibility since our code literally doesn't care what type of object it is interacting with as long as the object supports the methods we want to call. Because the type of the object isn't known by the IDE or compiler, neither IntelliSense nor compile-time syntax checking is possible but we get unprecedented flexibility in exchange.
Conclusion :
When you talk about designing a software system, the correct handling of OOP concept is very important. In order to do it, the foundation of a system places a vital role. The design or the architecture of a software system is the foundation. It hold the system together, hence designing a system properly (this never mean an *over* desinging) is the key to the success.

No comments:

Post a Comment