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

Books Express Adapter

Below is the BooksExpressAdapter class.

Listing 3

public class BooksExpressAdapter: BookAdapter
{
  private BooksProvider _provider = null;
 
  protected BooksProvider Provider
  {
    get
    {
      if (_provider == null)
        _provider = new BooksProvider();
      return _provider;
    }
  }
 
  public override Book RetrieveByISBN(string isbn)
  {
    BookItem item = this.Provider.GetByISBN(isbn);
    if (item != null)
      return this.CreateBook(item);
    else
      return null;
  }
 
  public override Book[]RetrieveBySearch(string searchText, bool anyTerms)
  {
    BookItem[]items = this.Provider.Search(searchText, anyTerms, false);
    List < Book > books = new List < Book > ();
 
    foreach (BookItem item in items)
      books.Add(this.CreateBook(item));
    return books.ToArray();
  }
}

A few things to note is that the provider is lazy loaded, so that it is instantiated when needed.  In addition, these methods use the CreateBook method to perform the conversion, which we will see next. The Searching mechanism loops through the results, creating a Book object for each item found in the collection. The Book object has a slightly different interface, as shown below.

Listing 4

private Book CreateBook(BookItem item)
{
  Book book = new Book();
  book.Author = item.Author;
  book.Description = item.Description;
  book.ISBN = item.ISBN;
  book.Title = item.Title;
  //Nothing done with item.Keyword
  return book;
}

Because the API uses a business object, the parameters come over as a 1-to-1 mapping, however, the keyword parameter is ignored because the new API does not use it.


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-2024 ASPAlliance.com  |  Page Processed at 2024-03-29 12:46:37 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search