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 4080740
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:58:08+00:00 2026-05-20T17:58:08+00:00

I have a class built from a hierarchy of other classes, and I had

  • 0

I have a class built from a hierarchy of other classes, and I had created a bunch of the same classes that looked identical other than the class name

for example

class Class1 extends BaseClass
{
  public function __construct()
  {
     parent::__construct();
  }
}

class Class2 extends BaseClass
{
  public function __construct()
  {
     parent::__construct();
  }
}

class Class3 extends BaseClass
{
  public function __construct()
  {
     parent::__construct();
  }
}

But I realize (hopefully rightfully) that I could create Class[1-3] using a factory pattern, and just pass a class name and have the factory return one of these classes. This would require much less code, thus more maintainable. Also, how would you suggest implementing if say I wanted to do this :

class Class4 extends AnotherBaseClass
{
  public function __construct()
  {
     parent::__construct();
  }
}

note that class 4 extends another base class, but it is the exact same other than a small bit of information that AnotherBaseClass holds.

right now i am thinking something like this (my thoughts are fuzzy and incomplete at this point):

//where i am putting the repeating code

class Skeleton extends BaseClass
{
  public function __construct()
  {
     parent::__construct();
  }
}


class ClassFactory
{
   public function __construct( $className , $classType )
   {
      <pseudocode>
      Class5 = new $className
      ???and some how make $className extend $classType ???
      return class5
      </pseudocode>
   }

}

Is the factory pattern the correct answer?

Cheers!

  • 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-20T17:58:09+00:00Added an answer on May 20, 2026 at 5:58 pm

    Yes – factory pattern is the correct answer. In addition you may also check out Abstract Factory, specifically for the scenario you mentioned where your some classes will have different attributes and may derive from some other base class.

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

Sidebar

Related Questions

I have built up an array of objects, created from a class, I wrote
I have a multidimensional array built from Strings that is initially created with the
I have a POCO class that is mostly built through a service. I hit
I have a custom class called NoteView that extends FrameLayout . Previously, I had
I have a small hierarchy of classes that all implement a common interface. Each
Sending objects to ASP.NET from JSON works fine if you have a class built
I have a WCF service that I have built from an XSD from a
I have a problem with Class.getSuperclass(). I want to generate a hierarchy of classes
In my Qt-based application (built using PyQt 4.8.6), I have a class that is
I'd like to have a BaseViewController class (that inherits from UIViewController ) for my

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.