Friday 25 July 2014

Design Patterns in dot net or object oriented Programming.

Design Patterns


In 1994, four authors Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides published a book (Design Patterns: Elements of Reusable Object-Oriented Software) for explaining the concept of Design Pattern in Software development. These four authors are collectively known as Gang of Four (GOF).

The 23 Design patterns are defined by the Gang of Four programmers. These 23 patterns are divided into three groups depending on the nature of the design problem they intend to solve.

What is Design Patterns:

  • Design Patterns in object oriented world is reusable solution to common software design problems which occur again and again in real world application development. It is a template or description for how to solve a problem which can be used in many different situations.

  • "A pattern is a recurring solution to a problem in a context."

  • "Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice."

  • Patterns are used by developers to their particular design to solve their problems. Patterns usage and choice to choose among different design patterns is based on individual need and their problem.
  • Design patterns are the most powerful tool for software developer. It is important to understand design patterns rather than memorizing its classes, methods and properties. It is also important to learn how to apply pattern to specific problem to get the desired result. This will be required continuous practice of using and applying design patterns in day to day software development. First, identify the software design problem, then see how to address these problems using design patterns and find out the best suited design problem to solve the problem.

  • There are 23 design patterns also known as Gang of four design patterns (GoF). Gang of four are the authors of the book, “Design Patterns: Elements of Reusable Object Oriented Software”. These 23 patterns are grouped into three main categories based on their:



Basically 3 types of Design Patterns:

1.       Creational Patterns:
2.       Behavioral Patterns:
3.       Structural Patterns:

 Creational Patterns:

1.       Singleton: Ensure that only one instance of a class is created and Provide a global  
       access point to the object.
2.       Factory: Creates objects without exposing the instantiation logic to the client and Refers to the newly created object through a common interface.
3.       Factory Method: Defines an interface for creating objects, but let subclasses to decide which class to instantiate and Refers to the newly created object through a common interface.
4.       Abstract Factory: Offers the interface for creating a family of related objects, without explicitly specifying their classes.
5.       Builder: Defines an instance for creating an object but letting subclasses decide which class to instantiate and allows a finer control over the construction process.
6.       Prototype: Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
7.       Object Pool: Reuses and shares objects that are expensive to create.

Behavioral Patterns:

1.       Chain of Responsibility: It avoids attaching the sender of a request to its receiver, giving this way other objects the possibility of handling the request too. The objects become parts of a chain and the request is sent from one object to another across the chain until one of the objects will handle it.
2.       Command: Encapsulate a request in an object, Allows the parameterization of clients with different requests and Allows saving the requests in a queue.
3.       Interpreter: Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language / Map a domain to a language, the language to a grammar, and the grammar to a hierarchical object-oriented design.
4.       Iterator: Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
5.       Mediator: Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
6.       Observer: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
7.       Strategy: Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
8.       Template Method: Define the skeleton of an algorithm in an operation, deferring some steps to subclasses / Template Method lets subclasses redefine certain steps of an algorithm without letting them to change the algorithm's structure.
9.       Visitor: Represents an operation to be performed on the elements of an object structure / Visitor lets you define a new operation without changing the classes of the elements on which it operates.
10.   Null Object: Provide an object as a surrogate for the lack of an object of a given type. / The Null Object Pattern provides intelligent do nothing behavior, hiding the details from its collaborators.

Structural Patterns:

1.       Adapter: Convert the interface of a class into another interface clients expect. / Adapter lets classes work together, that could not otherwise because of incompatible interfaces.
2.       Bridge: Compose objects into tree structures to represent part-whole hierarchies. / Composite lets clients treat individual objects and compositions of objects uniformly.
3.       Composite: Compose objects into tree structures to represent part-whole hierarchies. / Composite lets clients treat individual objects and compositions of objects uniformly.
4.       Decorator: add additional responsibilities dynamically to an object.
5.       Flyweight: use sharing to support a large number of objects that have part of their internal state in common where the other part of state can vary.
6.       Memento: capture the internal state of an object without violating encapsulation and thus providing a mean for restoring the object into initial state when needed.
7.       Proxy: provide a “Placeholder” for an object to control references to it.

Wednesday 23 July 2014

Sealed Classes and Methods in c#.net.


Sealed Classes:




Sealed Classes are those classes that cannot be inherited by other classes. If we want to prevent a class to be inherited by other class then we make that class as Sealed Class. To create a class as sealed class, create the class using the keyword sealed.


Syntax:-

[Access Modifier] sealed class classname

{

//Class Body

}



Sealed Methods:

The virtual nature of a method persists for any number of levels of inheritance.

For example there is a class “A” that contains a virtual method “M1”. Another class “B” is inherited from “A” and another class “C” is inherited from “B”. In this situation class “C” can override the method “M1” regardless of whether class “B” overrides the method “M1”.

At any level of inheritance, if you want to restrict next level of derived classes from overriding a virtual method, then create the method using the keyword sealed along with the keyword override.

Syntax:-

[Access Modifier] sealed override returntype methodname ([Params])

{

//Method Body

}

Advantages and Disadvantages of Session in asp.net.



Following are the basic advantages and disadvantages of session:

Advantages:

  • It stores user states and data to all over the application.
  • Easy mechanism to implement and we can store any kind of object.
  • Stores every user data separately.
  • Session is secure and transparent from user because session object is stored on the server.

Disadvantages:

  • Performance overhead in case of large number of user, because of session data stored in server memory.
  • Overhead involved in serializing and De-Serializing session Data. Because In case of StateServer and SQLServer session mode we need to serialize the object before store.