Click here to Skip to main content
Click here to Skip to main content

Factory Design Pattern for SearchFactory - A Practical Approach

, 12 Aug 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
This article explains a practical approach to employ the Factory design pattern using an example of a searching module in a Document Review System.

Introduction

Abstraction concretes almost everything!! The idea here is to promote the power of Factory design patterns in one of my latest personal projects. My project deals with content searching for documents and returning results that are displayed in a grid (typical methodology of showing search results, isn’t it? Smile | :) ). This code article deals with the architecture of the searching module which I have implemented to solve typical problems and to make the architecture more candid and tangible. This article might be helpful for those who are developing similar kinds of applications and are looking for similar abstractions. Please note that this article deals with design concepts, so the body of the methods in the code have been kept empty. You can insert your own code to make them work!!

Background

There are quite a few articles of abstract factory design available on the internet. Most of them include bookish examples like draw, run, move, play, etc… not for developers who demand cutting edge solutions (no offence meant).

“The Factory design pattern deals with the way objects are created. There is one parent class which defines the parameterized factory method to create the object of its child class based on the parameters passed”.

Using the code

The code structure is very simple. There are four classes viz. SearchFactory, ProximitySearch, SimilaritySearch, and SimpleSearch.

There are three types of searches in my module viz. Proximity, Similarity, and Simple. Detailed description on how these searches perform is out of the scope of this article (I will post it separately Smile | :) ).

The code here defines the SearchFactory class as an abstract class which has a factory method called GetSearchObject(). This method is also called parameterized factory method which takes a parameter for the type of object that is to be created. All the three searches have one class each, defined in the code. All these classes (ProximitySearch, SimilaritySearch, SimpleSearch) inherit the SearchFactory class to provide a concrete implementation.

Apart from the Factory method, SearchFactory (the base class of all searches) defines several other abstract functions which are common for all these three types of searches (simple search, proximity search, and similarity search). The idea is to make all of them abstract in SearchFactory and let each search override each of those abstract functions.

Those abstract functions are as follows:

public abstract void GetSearchResult(); 
public abstract void ExecuteSearch();
public abstract void SaveAndSearch();
public abstract void ValidateSearch();
public abstract void UpdateDisplayName();

You can refer to the code for this. The typical search factory method is given as follows. Based on the parameter searchType, an object will be created for ProximitySearch, SimpleSearch, and SimilaritySearch.

public static SearchFactory GetSearchObject(string searchType)
{
    SearchFactory objContentSearchObject = null;
    if(searchType.ToString().ToUpper() == "SIMPLE")
    {
        objContentSearchObject = new SimpleSearch();    
    }

    if(searchType.ToString().ToUpper() == "PROXIMITY")
    {
        objContentSearchObject = new ProximitySearch();    
    }

    if(searchType.ToString().ToUpper() == "SIMILARITY")
    {
        objContentSearchObject = new SimilaritySearch();    
    }

    return objContentSearchObject;
}

Conclusion

This article targets developers who are starving to get a practical usage of design patterns. This article is at the draft level, and may contain a few errors or bugs and thereby I welcome suggestions or criticism!! Developers who would like to challenge my code can write their comments on this code without any hesitation. Starving for perfection Smile | :) .

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Pratik.Patel
Architect
India India
He is a Bachelor of Engineering (Computer science) graduate with the total work experience of around 5 years on microsoft technologies primarily on C#.NET. Currently he is engaged in exploring new technologies like nHibernet, DDD, TDD, jQuery and ASP.NET MVC. His previous experience includes the development of the leading community website, C++/COM+ with active directory (He loved playing with AD), MS-CRM, .NET remoting, webservices and ASP.NET. He holds the MCP certification for 70-316.
 
Along with Software Development, he likes to play harmonica (Mouthorgan) and likes to spend the weekend researching on new coding techniques published on www.codeproject.com
Follow on   Twitter

Comments and Discussions

 
GeneralGetSearchObject PinmemberPete O'Hanlon2-Feb-07 2:55 
GeneralRe: GetSearchObject [modified] PinmemberPratik.Patel16-Aug-09 2:00 
GeneralFactory Method PinmemberMikeonn2-Feb-07 2:04 
AnswerRe: Factory Method [modified] PinmemberPratik.Patel18-Aug-09 21:23 
I have updated the article
Regards,
Pratik


modified 28-Sep-11 3:57am.

Generalnice one PinmemberAshish Basran3-Nov-06 4:18 
GeneralRe: nice one [modified] PinmemberPratik.Patel5-Nov-06 20:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 12 Aug 2009
Article Copyright 2006 by Pratik.Patel
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid