Google

About Me

Thursday, May 27, 2010

Adapter



1     Adapter


Definition:-
1      Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
2      Wrap an existing class with a new interface.
3      Match interfaces of different classes
4      Adapt an interface to an expected interface
5      Impedance match an old component to a new system
6      Adapter is about creating an intermediary abstraction that translates, or maps, the old component to the new system. Clients call methods on the Adapter object which redirects them into calls to the legacy component. This strategy can be implemented either with inheritance or with aggregation



Core component:-
  • Client. The client class is that which requires the use of an incompatible type. It expects to interact with a type that implements the ITarget interface. However, the class that we wish it to use is the incompatible Adaptee.
  • ITarget. This is the expected interface for the client class. Although shown in the diagram as an interface, it may be a class that the adapter inherits. If a class is used, the adapter must override its members.
  • Adaptee. This class contains the functionality that is required by the client. However, its interface is not compatible with that which is expected.
  • Adapter. This class provides the link between the incompatible Client and Adaptee classes. The adapter implements the ITarget interface and contains a private instance of the Adaptee class. When the client executes MethodA on the ITarget interface, MethodA in the adapter translates this request to a call to MethodB on the internal Adaptee instance. 
    Example :-
    public class Client


    {
        private ITarget _target;

        public Client(ITarget target)
        {
            _target = target;
        }

        public void MakeRequest()
        {
            _target.MethodA();
        }
    }


    public interface ITarget
    {
        void MethodA();
    }


    public class Adaptee
    {
        public void MethodB()
        {
            Console.WriteLine("MethodB called");
        }
    }


    public class Adapter : ITarget
    {
        Adaptee _adaptee = new Adaptee();

        public void MethodA()
        {
            _adaptee.MethodB();
        }
    }


    Check list:-
    1. Identify the players: the component(s) that want to be accommodated (i.e. the client), and the component that needs to adapt (i.e. the adaptee).
    2. Identify the interface that the client requires.
    3. Design a “wrapper” class that can “impedance match” the adaptee to the client.
    4. The adapter/wrapper class “has a” instance of the adaptee class.
    5. The adapter/wrapper class “maps” the client interface to the adaptee interface.
    6. The client uses (is coupled to) the new interface

Design Patterns


                                                                                            Design Patterns
Design Patterns are about design and interaction of objects. They provide a communication platform concerning elegant, reusable solutions to commonly encountered programming challenges. It is a way to pursue an intent that uses classes and their methods in an OO language
Also we can say that, it is recurring solutions to software design problems you find again and again in real-world application development. Patterns are about design and interaction of objects, as well as providing a communication platform concerning elegant, reusable solutions to commonly encountered programming challenges.
With .NET the full capability of object-oriented programming came into the picture for the most of developers using Microsoft products.
You can do much more with .NET than with previous Microsoft technologies but it comes with a price, increased complexity. And design patterns have evolved over the time to help to keep things in order when applications become complex.
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.
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

  Structural Patterns
  Adapter
  Wrap an existing class with a new interface, 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

  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
  Iterator
  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

We can also categorize it :-

Interface Patterns

1     Adapter
2     Facade
3     Composite
4     Bridge

 Responsibility Patterns

1     Singleton
2     Observer
3     Mediator
4     Proxy
5     Chain of Responsibility
6     Flyweight

Construction Patterns

1     Builder
2     Factory Method
3     Abstract Factory
4     Prototype
5     Memento

Operation Patterns

1      Template Method
2     State
3     Strategy
4     Command
5     Interpreter

Extension Patterns

1     Decorator
2     Iterator
3     Visitor

Wednesday, November 25, 2009

Filter Gridview on keypress of textbox in javascript

    <script type="text/javascript" charset="utf-8">
   
   function filter (term, _id, cellNr){
    var suche = term.value.toLowerCase();
    var table = document.getElementById(_id);
    var ele;
    for (var r = 1; r < table.rows.length; r++){
        ele = table.rows[r].cells[cellNr].innerHTML.replace(/<[^>]+>/g,"");
        if (ele.toLowerCase().indexOf(suche)>=0 )
            table.rows[r].style.display = '';
        else table.rows[r].style.display = 'none';
    }
}
</script>
  
     Filter:<input type="text" id="FilterTextBox" name="FilterTextBox" onkeyup="filter(this, 'filterable1', 0)"/>

              
                    <asp:GridView runat="server" ID="filterable1" AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true">
                        <Columns>
                     
                            <asp:BoundField DataField="FirstName" HeaderText="Name" />
                            <asp:BoundField DataField="LastName" HeaderText="LastName" />
                        </Columns>
                    </asp:GridView>

Saturday, November 8, 2008

want to know about a mobile website

All you want to know about a mobile website got to the below mention link
http://www.codeproject.com/KB/mobile/All-about-a-mobile-websit.aspx
and
http://worldofasp.net/tut/XHTM_L-MP/Basics_of_XHTML-MP_178.aspx

Wednesday, July 9, 2008

State Managment

  • Viewstate
  • Session Variables
  • Application Variables
  • Cache
  • Cookies

Now the question arises that when to use what?

1- Viewstate

Viewstate is a hidden fields in an ASP.NET page, contains state of those controls on a page whose “EnableViewstate” property is “true”.

You can also explicitly add values in it, on an ASP.NET page like:

Viewstate.Add( “TotalStudents”, “87″ );

Viewstate should be used when you want to save a value between different roundtrips of a single page as viewstate of a page is not accessible by another page.

Because Viewstate renders with the page, it consumes bandwidth, so be careful to use it in applications to be run on low bandwidth.

2- Session Variable

Session variables are usually the most commonly used.

When a user visits a site, it’s sessions starts and when the user become idle or leave the site, the session ends.

Session variables should be used to save and retrieve user specific information required on multiple pages.

Session variables consumes server memory, so if your may have a huge amount visitors, use session very carefully and instead of put large values in it try to put IDs and references


3- Application variables

Application variables are shared variables among all users of a web application

Application variables behave like static variables and they are substitute of static variables as static variables are stateless in web applications

Only shared values should be persisted in Application variables, and as soon as they are not in use they should be removed explicitly.


4- Cache

Cache is probably the least used state feature of ASP.NET.

Cache is basically a resource specific state persistence feature, means unlike session it stick with resource instead of user, for instance: pages, controls etc.

Cache should be used or frequently used pages, controls, and data structures

Data cache can be used to cache frequently used list of values e.g. list of products

5- Cookies

Cookies are some values saved in browsers by the website to retrivbbe and use afterwards.

Usually cookies are used to help dynamic websites to identify visitors and retrieve their saved preferences.

Cookies are also used to facilitate auto login by persisting user id in a cookie save in user’s browser.

Because cookies have been saved at client side, they do not create performance issues but may create security issues as they can be hacked from browser.


Finally remember the following points on your finger-tips:

  • Viewstate is bandwidth hungry
  • Session variables are memory hungry as per number of users
  • Applications variables are shared
  • Cache is memory hungry as per number of resources
  • Cookies are the least secure