Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8313091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:22:47+00:00 2026-06-08T20:22:47+00:00

Factory creating only correct type So here is an example of a Factory only

  • 0

Factory creating only correct type

So here is an example of a Factory only returning the correct type:

public class BirdFactory
{
    public static IBird CreateBird(string birdType)
    {
        switch (birdType)
        {
            case "Eagle":
                return new Eagle();
            case "Penguin":
                return new Penguin();
        }
    }
}

Used like:

IBird bird = BirdFactory.CreateBird("eagle");
bird.FlyToLocation(new Location("London"));

Factoring creating correct type and setting state

Here is an example of a factory being used to set state and type:

public class BirdFactory
{
    public static IBird CreateBird(string birdType, Location locationToFlyTo)
    {
        switch (birdType)
        {
            case "Eagle":
                return new Eagle(locationToFlyTo);
            case "Penguin":
                return new Penguin(locationToFlyTo);
        }
    }
}

Used like:

IBird bird = BirdFactory.CreateBird("eagle", new Location("London"));
bird.FlyToLocation();

The question

How acceptable is the second practice? Which is more maintainable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:22:48+00:00Added an answer on June 8, 2026 at 8:22 pm

    There is no cardinal rule that you cannot set state along with type in a factory pattern. And indeed combining patterns is an acceptable practice. So if you are pretty sure that the second method fits your needs perfectly, go for it.

    I preffer method 1 over method two becuase it is less ambiguous.

    IBird bird = BirdFactory.CreateBird("eagle", new Location("London"));
    

    may lead a reader to think that the eagles “lives”(or was born) in London.

    The next line:

    bird.FlyToLocation();
    

    will perplex them: “where is it flying to? Oh! You mean the bird has to fly to London?”

    bird.FlyToLocation(new Location("London"));
    

    avoids this ambiguity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What if I have few factory methods returning non-public type and pairing set of
Consider a theoretical factory class containing over a thousand of attributes (models, by example,
First, I have a dbcontext factory which is defined public class DatabaseFactory : Disposable,
I have the following spring bean configuration <bean id=fileBean class=java.io.File> <constructor-arg type=java.lang.String value=$prop{file.path.property} />
Here's my issue, I'm using a namespace to remove ambiguity from a factory class
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'PNLResultDAOImpl' defined in file [C:\EclipseWS1\SpringJDBCExample\target\classes\com\synechron\tom\pnl\dao\impl\PNLResultDAOImpl.class]: Instantiation of bean
I'm using the Ninject Factory Extension and creating a custom instance provider explained in
I'm trying to register a factory method for creating instances of an open generic
Does anybody recognize this error? org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory': Post-processing of
I'm having the following issue with my hibernate 3.6.10 project: org.springframework.beans.factory.BeanCreationException: Error creating bean

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.