Advertisement

09.27.2007 at 09:53AM PDT, ID: 22857206
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Factory Pattern (How to use correctly)

Tags: use, pattern, factory, how
Hi all,

I am looking at Factory Pattern and I'm having a little difficulty working it out.

My basic problem is that all of the examples I have looked at only contain classes with one method and no properties in any of the classes.
Is this a requirement of the Factory Pattern that all classes must implement the exact same interface with no properties?

For example:

interface IDog {
 String Bark()
}

class Bulldog : IDog {
 String Bark()
 {
  return "WoooF";
 }
}

class KingCharles : IDog {
 String Bark()
 {
  return "squeeky woof";
 }
}


Then I use the Factory to create the dog I want based on a parameter passed to a createinstance method and return the newly created type of dog. Then I can call the Bark method through the interface. Perfect...

So what is my problem....
I think the real problem is that I cannot see when or how to use this in a real world situtation. Now I don't know if the next bit is way off the mark but this is what I am trying to do...

I have an interface called ICompany. This has properties like Id , Name Address1, Address2 and Methods Retrieve, Create, Update, Delete.

I have a second interface called ILeader. This has properties like Id , Title, FirstName, LastName and Methods Retrieve, Create, Update, Delete.

I have created the implementation of these interfaces which connects to the database and all works as it should.

What I was looking at doing was creating a class factory for these two interfaces.

So my questions are:
1: Is this a viable / possible solution?
2: How would I go about this?
3: If what I am asking s not doable what would you do to hide the creation of these objects from the business or client code?

Hope this makes a little sense.

Darren


Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: DarrenD
Solution Provided By: TheLearnedOne
Participating Experts: 2
Solution Grade: A
Views: 0
Translate:
Loading Advertisement...
09.27.2007 at 12:26PM PDT, ID: 19974037

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.27.2007 at 12:28PM PDT, ID: 19974049

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.27.2007 at 12:29PM PDT, ID: 19974057

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.27.2007 at 12:30PM PDT, ID: 19974070

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
09.27.2007 at 12:34PM PDT, ID: 19974112

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
09.27.2007 at 12:26PM PDT, ID: 19974037

Rank: Genius

Here is a real world example of a factory pattern:

DbProviderFactory
-----------------------
public abstract class DbProviderFactory
{
  // Methods
  protected DbProviderFactory();
  public virtual DbCommand CreateCommand();
  public virtual DbCommandBuilder CreateCommandBuilder();
  public virtual DbConnection CreateConnection();
  public virtual DbConnectionStringBuilder CreateConnectionStringBuilder();
  public virtual DbDataAdapter CreateDataAdapter();
  public virtual DbDataSourceEnumerator CreateDataSourceEnumerator();
  public virtual DbParameter CreateParameter();
  public virtual CodeAccessPermission CreatePermission(PermissionState state);

  // Properties
  public virtual bool CanCreateDataSourceEnumerator { get; }
}
Accepted Solution
 
09.27.2007 at 12:28PM PDT, ID: 19974049

Rank: Genius

There are 3 derived classes in the .NET 2.0 framework:

    System.Data.Odbc.OdbcFactory
    System.Data.OleDb.OleDbFactory
    System.Data.SqlClient.SqlClientFactory

The DbProviderFactory is an abstract class, an must be overriden by those 3 derived classes.
Assisted Solution
 
09.27.2007 at 12:29PM PDT, ID: 19974057

Rank: Genius

The SqlClientFactory defines methods like this:

public sealed class SqlClientFactory : DbProviderFactory
{
  // Fields
  public static readonly SqlClientFactory Instance = new SqlClientFactory();

  // Methods
  private SqlClientFactory()
  {
  }

  public override DbCommand CreateCommand()
  {
    return new SqlCommand();
  }

  public override DbCommandBuilder CreateCommandBuilder()
  {
    return new SqlCommandBuilder();
  }

  public override DbConnection CreateConnection()
  {
    return new SqlConnection();
  }

  public override DbConnectionStringBuilder CreateConnectionStringBuilder()
  {
    return new SqlConnectionStringBuilder();
  }

  public override DbDataAdapter CreateDataAdapter()
  {
    return new SqlDataAdapter();
  }

  public override DbDataSourceEnumerator CreateDataSourceEnumerator()
  {
    return SqlDataSourceEnumerator.Instance;
  }

  public override DbParameter CreateParameter()
  {
    return new SqlParameter();
  }

  public override CodeAccessPermission CreatePermission(PermissionState state)
  {
    return new SqlClientPermission(state);
  }

  // Properties
  public override bool CanCreateDataSourceEnumerator
  {
    get
    {
      return true;
    }
  }
}

 
Bob
Assisted Solution
 
09.27.2007 at 12:30PM PDT, ID: 19974070

Rank: Genius

1) Define an abstract class

2) Define a concrete class for each type that you need to implement

3) Override the creation methods defined in the abstract class in the concrete classes.

Bob
Assisted Solution
 
09.27.2007 at 12:34PM PDT, ID: 19974112

Rank: Sage

Your initial question about the classes having no properties: that is not a requirement for the factory pattern.  I think it's just easier to whip up examples using methods that properties with get/set methods, etc.

Your other questions:

1. It is certainly _possible_ in your situation, I would only ask if you _need_ it.  The purpose of using a class factory is to insulate the client from the actual class that is being created.  For most data-based classes, you can create instances by passing in an identifier:

ICompamy company = new Company(12345);

Since interfaces don't have immplemntation, you can't call
ICompany company = new ICompany(12345);

So if you want to be agnostic about the actual implementation class, you create a factory class so you can call:
ICompany company = CompanyFactory.CreateByID(12345);

The CompanyFactory knows how to create instances of Company, and does so, reterning a reference to the interface.

2. In it's simplest form it could be:

public ICompany CreateByID(int id)
{
  return new Company(id);
}

Of course if you have different classes that implement ICompany you'd need to have some logic in there to determine what type to create, etc.

3. An alternative is to create the objects directly, which is  perfectly viable solution.

Assisted Solution
 
 
20080236-EE-VQP-29 / EE_QW_1_20070628