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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:37:53+00:00 2026-05-27T01:37:53+00:00

I have an OO design question. I’ve written the (pseudo)-pseudocode below to help illustrate

  • 0

I have an OO design question. I’ve written the (pseudo)-pseudocode below to help illustrate my question. (I say “pseudo-pseudocode” because it’s mostly correct, with only a few bits of nonsense…)

I’m using a Factory pattern to create objects of a class appropriate to the attributes I pass the Factory::new method. However, there are some attributes that I can only get after object creation which I want to then use to further subclass or “specialize” the type of object. I want to do this so I can use the same interface to all of the objects in main independent of the object class (I guess this is polymorphism).

First, the Factory class:

use strict;
use warnings;

package Vehicle::Factory;
sub new {
    my ( $class, $args ) = @_;
    if ( $args->{class} =~ /car/i ) {
        return Vehicle::Car->new($args);
    } else {
    # other possible subclasses based on attributes
    }
}
1;

Now for the associated classes:

package Vehicle;
sub new {
    my ( $class, $args ) = @_;
    bless $self, $class;
    $self->color( $args->color );
}

sub color {
    $_[1] ? $_[0]->{_color} = $_[1] : return $_[0]->{_color};
}

sub wheels {
    $_[1] ? $_[0]->{_wheels} = $_[1] : return $_[0]->{_wheels};
}

1;

And a subclass:

package Vehicle::Car;
use base qw( Vehicle );
sub get_fueltype {
    my ( $self, $args ) = @_;
    $self->fueltype = check_fuel_type;
}

sub fueltype {
    $_[1] ? $_[0]->{_fueltype} = $_[1] : return $_[0]->{_fueltype};
}

1;

Now for the “stage 2” subclasses. I can only create these when I know more about the object that’s already been created…

package Vehicle::Car::Gas;
use base qw( Vehicle::Car );
sub fill_her_up {
    # Make sure it's Gas.
    # ...
}
1;

package Vehicle::Car::Diesel;
use base qw( Vehilce::Car );
sub fill_her_up {
    # Make sure it's Diesel.
    # ...
}
1;

package Vehicle::Car::Electric;
use base qw( Vehicle::Car );
sub fill_her_up {
    # Find a socket.
    # ...
}
1;

And the main body of code:

package main;

my $thing = Vehicle::Factory->new( color => "red", wheels => 4 );

$thing->get_fueltype;

# Somehow convert $thing to be an object of the appropriate subclass based on 
# the "fueltype" attribute

$thing->fill_her_up;

(I hope my horribly contrived example makes sense!)

Now, I’m not sure… Should I create a new object using instance data from $thing?
Is there a way to subclass an object without destroying and recreating it?

Maybe I should I use the following approach, and re-use the Vehicle factory?

package Vehicle::Factory;

sub new {
    my ( $class, $args ) = @_;
    if ( $args->{class} =~ /car/i ) {
        return Vehicle::Car->new($args);
    }

    if ( $self->fueltype eq "gas" ) {
        return Vehicle::Car::Gas->new($args);
    }

    if ( $self->fueltype eq "diesel" ) {
        return Vehicle::Car::Diesel->new($args);
    }

    if ( $self->fueltype eq "electric" ) {
        return Vehicle::Car::Electric->new($args);
    }
}

At this point in my real code – unlike my example – there’s alot of instance data to then pass to a new object. I think it could be a little ugly if I need to pass all data between old and new object explicitly.

In my real code, there may be hundreds / thousands of such objects fed from a config file, all requiring the same treatment but with some differences on how to do it. It’s the difference between using Expect and SSH to get data from a remote device, or using SNMP. The second “level” of info is based on information I get when I query a remote device and get it’s device type (among other things)…

Final point is: I’m almost complete writing the software, but a very “late” and important requirement has come up which necessitates this change. I really want to accomodate the late req as simply and elegantly as possible. I don’t want to “hack” it in and change the interface in main.

Thanks in advance for any pointers.

  • 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-27T01:37:54+00:00Added an answer on May 27, 2026 at 1:37 am

    Changing the type of an object is very easy in Perl, even after it has been created (easy enough to get yourself in big trouble).

    $car = Vehicle::Factory->new( ... );
    ... stuff happens to $car ...
    
    # Oh! Now I have decided that $car should be a Vehicle::RustBucket::Fiat
    bless $car, 'Vehicle::RustBucket::Fiat';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Java design question. I have an object that needs to maintain sets of say
I've a general design question: I have a mailserver, written in C#. Then I
So this is more of a design question. I have one primary key (say
Just wanted opinions on a design question. If you have a C++ class than
More of a design/conceptual question. At work the decision was made to have our
I have a question about design winforms. Should I use, or not, group boxes
I have posted a question about multilanguage database design here, [] What are best
An idle question on language design, see Does C# have a right hand if
I have a pretty simple question which perhaps someone familiar with Server/Client design &
I have a simple question. I am trying to design a simple Android app,

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.