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

  • SEARCH
  • Home
  • 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 1031223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T13:53:38+00:00 2026-05-16T13:53:38+00:00

I’m having a hard time figuring out how to implement a factory pattern in

  • 0

I’m having a hard time figuring out how to implement a factory pattern in a DTO mapper I’m trying to create. I’m pretty sure I need to rethink my design. Here is a very small example of what I’m running in to:

    public abstract class Person
{
    public string Name { get; set; }
    public decimal Salary { get; set; }
}

public class Employee : Person
{
    public Employee()
    {
        this.Salary = 20000;
    }

}

public class Pilot : Person
{
    public string PilotNumber { get; set; }

    public Pilot()
    {
        this.Salary = 50000;
    }
}

public static class PersonFactory
{
    public static Person CreatePerson(string typeOfPerson)
    {
        switch (typeOfPerson)
        {
            case "Employee":
                return new Employee();
            case "Pilot":
                return new Pilot();
            default:
                return new Employee();
        }
    }
}

and to use the factory:

Person thePilot = PersonFactory.CreatePerson("Pilot");
        ((Pilot)thePilot).PilotNumber = "123ABC";

How do I get around loading the pilot number without typecasting it to Pilot?? is this the wrong way to do this? I could put the pilot number in the Person class, but then Employee would inherit the number and that’s not what I want. What can I do?

Thanks!

-Jackson

  • 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-05-16T13:53:39+00:00Added an answer on May 16, 2026 at 1:53 pm

    You could add methods for specific types to your PersonFactory class, or add a generic CreatePerson<T>() method, but that would only be useful if the caller already knows what type of person it should be receiving. Maybe this is the case, or maybe not.

    With this scenario, I’d expect that the code that is actually making the call to PersonFactory.CreatePerson would not know or care what kind of person is being returned. If you have some code after that point that already knows or figures out what type of person object you have, then you will simply have to cast it.

    Below is a code example that illustrates what you could do on your factory and different usage scenarios, attempting to explain when you simply need to cast or when you don’t.

    public static class PersonFactory
    {
        public static Person CreatePerson()
        {
            return new Person();
        }
    
        public static Employee CreateEmployee()
        {
            return new Employee();
        }
    
        public static Pilot CreatePilot()
        {
            return new Pilot();
        }
    
        public static T CreatePerson<T>()
            where T : Person
        {
            return (T)CreatePerson(typeof(T));
        }
    
        public static Person CreatePerson(Type type)
        {
            if (type == typeof(Person))
                return CreatePerson();
            else if (type == typeof(Employee))
                return CreateEmployee();
            else if (type == typeof(Pilot))
                return CreatePilot();
            else
                throw new ArgumentOutOfRangeException(string.Format(CultureInfo.InvariantCulture, "Unrecognized type [{0}]", type.FullName), "type");
        }
    
        public static Person CreatePerson(string typeOfPerson)
        {
            switch (typeOfPerson)
            {
                case "Employee":
                    return CreateEmployee();
                case "Pilot":
                    return CreatePilot();
                default:
                    return CreateEmployee();
            }
        }
    }
    
    
    
    class UsageExample
    {
        Person GetPerson()
        {
            Pilot p;
            p = (Pilot)PersonFactory.CreatePerson("Pilot"); // this code already knows to expect a Pilot, so why not just call CreatePilot or CreatePerson<Pilot>()?
            p = PersonFactory.CreatePilot();
            p = PersonFactory.CreatePerson<Pilot>();
            return p;
        }
    
        Person GetPerson(Type personType)
        {
            Person p = PersonFactory.CreatePerson(personType);
            // this code can't know what type of person was just created, because it depends on the parameter
            return p;
        }
    
        void KnowledgableCaller()
        {
            Type personType = typeof(Pilot);
    
            Person p = this.GetPerson(typeof(Pilot));
            // this code knows that the Person object just returned should be of type Pilot
    
            Pilot pilot = (Pilot)p;
            // proceed with accessing Pilot-specific functionality
        }
    
        void IgnorantCaller()
        {
            Person p = this.GetPerson();
            // this caller doesn't know what type of Person object was just returned
    
            // but it can perform tests to figure it out
            Pilot pilot = p as Pilot;
            if (pilot != null)
            {
                // proceed with accessing Pilot-specific functionality
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.