Using the Adapter Pattern
page 6 of 8
by Brian Mains
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 37155/ 74

Sample Application

Below are some screen shots of examples taken from a sample application that I developed.  The first example is querying the "I Luv Books" provider by ISBN.

Figure 1

When searching for "ASP.NET," the following results come up.

Figure 2

Switching over to the "Books Express" provider, below are the two screenshots of using it for connecting to the data store.

Figure 3

Figure 4

At the root of this is a BookAdapter object that represents one of the two available book adapters.  The following code gets the correct adapter to use based on the radio button list.

Listing 7

private BookAdapter GetAdapter()
{
  if (this.rbBooksExpress.Checked)
    return new BooksExpressAdapter();
  else
    return new ILuvBooksAdapter();
}

Note that this could pull the provider name/type from a data source or configuration file. For this example, I used the Template Method pattern to retrieve the object.  Next, based on which button was clicked, an event handler runs, passing the book results to another method.

Listing 8

private void btnISBNSearch_Click(object sender, EventArgs e)
{
  BookAdapter adapter = this.GetAdapter();
  Book book = adapter.RetrieveByISBN(this.mtbISBN.Text);
 
  if (book == null)
    this.NotifyNoBooks();
  else
  this.LoadBookResults(new Book[]
  {
    book
  }
  );
}
 
private void btnSearch_Click(object sender, EventArgs e)
{
  BookAdapter adapter = this.GetAdapter();
  Book[]books = adapter.RetrieveBySearch(this.txtSearch.Text, chkAny.Checked);
 
  if (books == null || books.Length == 0)
    this.NotifyNoBooks();
  else
    this.LoadBookResults(books);
}

Because we reference the provider through the BookAdapter abstract class, we do not need to know anything specific about the provider, which makes this portion of the code dynamic.  That is the benefit of inheritance and polymorphism through the adapter pattern.  Derived types do not need to declare their specific type, so we can work with the class interface that BookAdapter provides.  NotifyNoBooks simply shows a message box and so I will not show that, but LoadBookResults takes the array and creates a series of list items for each book.

Listing 9

private void LoadBookResults(Book[]books)
{
  this.lvwBooks.Items.Clear();
 
  foreach (Book book in books)
  {
    ListViewItem item = new ListViewItem(book.Title);
    item.SubItems.Add(book.Author);
    item.SubItems.Add(book.Description);
    item.SubItems.Add(book.ISBN);
    this.lvwBooks.Items.Add(item);
  }
}

View Entire Article

User Comments

Title: Great !!   
Name: Gourik kumar Bora
Date: 2011-05-12 2:09:23 AM
Comment:
thanks a lot Brian ....
Title: Excellent article   
Name: Chetan P
Date: 2008-08-18 6:01:55 AM
Comment:
This is an excellent article i read about adapter pattern. I wish if it was given with UML diagrams, it would more appropriate to understand at a glance.
Title: Great   
Name: vishal
Date: 2007-12-05 12:54:21 AM
Comment:
very easy to understand and really simple yet powerful post.
Title: good one   
Name: rakesh gurjar
Date: 2007-05-21 9:11:14 AM
Comment:
nice to understand concept of adapter pattern






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-11-29 1:20:20 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search