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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:43:24+00:00 2026-05-15T05:43:24+00:00

I want to create a generic class, whose builder would not return an instance

  • 0

I want to create a generic class, whose builder would not return an instance of this generic class, but an instance of a dedicated child class.

As Moose does automatic object building, I do not get to understand if this something possible, and how to create a Moose class with Moose syntax and having this behaviour.

e.g.:
The user asks: $file = Repository->new(uri=>'sftp://blabla') …. and is returned an `Repository::_Sftp“ instance

User would use $file as if it is a Repository instance, without the need to know the real subclass (polymorphism)

Note:
As requested, maybe i should have been more clear about what i was trying to achieve:
The purpose of my class is to be able to add new Repository schemes (eg over sftp), by simply creating an “hidden” Repository::_Stfp class, and adding a case in the Repository constructor to factory the correct specialized object depending of url. Repository would be like a virtual base class, providing an interface that specialized objects would implement.
All of this is for adding new repository schemes without having the rest of the program to be modified: it would unknowingly deal with the specialized instance as if it is a Repository instance.

  • 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-15T05:43:24+00:00Added an answer on May 15, 2026 at 5:43 am

    new builds the builder. You want some other method to actually return the built object.

    Here’s an example:

      class RepositoryBuilder {
         has 'allow_network_repositories' => (
             is       => 'ro',
             isa      => 'Bool',
             required => 1,
         );
    
         method build_repository(Uri $url) {
            confess 'network access is not allowed'
                if $url->is_network_url && !$self->allow_network_repositories;
    
            my $class = $self->determine_class_for($url); # Repository::Whatever
            return $class->new( url => $url );
         }
      }
    
      role Repository { <whatever }
    
      class Repository::File with Repository {}
      class Repository::HTTP with Repository {}
    

    Here, the builder and the built object are distinct. The builder is a
    real object, complete with parameters, that can be customized to build
    objects as the situation demands. Then, the “built” objects are
    merely return values of a method. This allows you to build other
    builders depending on the situation. (A problem with builder
    functions is that they are very inflexible — it’s hard to teach them
    a new special case. This problem still exists with a builder object,
    but at least your application can create a subclass, instantiate it,
    and pass this object to anything that needs to create objects. But
    dependency injection is a better approach in this case.)

    Also, there is no need for the repositories you build to inherit from
    anything, they just need a tag indicating that they are repositories.
    And that’s what our Repository role does. (You will want to add the
    API code here, and any methods that should be reused. But be careful
    about forcing reuse — are you sure everything tagged with the
    Repository role will want that code? If not, just put the code in
    another role and apply that one to the classes that require that
    functionality.)

    Here’s how we use the builder we created. If, say, you don’t want to
    touch the network:

    my $b = RepositoryBuilder->new( allow_network_repositories => 0 );
    $b->build_repository( 'http://google.com/' ); # error
    $b->build_repository( 'file:///home/whatever' ); # returns a Repository::Foo
    

    But if you do:

    my $b = RepositoryBuilder->new( allow_network_repositories => 1 );
    $b->build_repository( 'http://google.com/' ); # Repository::HTTP
    

    Now that you have a builder that builds the objects the way you like,
    you just need to use these objects in other code. So the last piece
    in the puzzle is referring to “any” type of Repository object in other
    code. That’s simple, you use does instead of isa:

    class SomethingThatHasARepository {
        has 'repository' => (
           is       => 'ro',
           does     => 'Repository',
           required => 1,
        );
    }
    

    And you’re done.

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

Sidebar

Related Questions

I want to create a KeyValue class but in generic manner and this is
i want to create a generic class that would accept T. T is an
I want to create a generic method to serizlize a class to text (for
Hi i want to create a generic style for pin button. <Window x:Class=TooglePinButtonStyle.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
I want to create a generic class that takes a type parameter and restrict
I'm trying to create a generic Controller Class for generic events. But these these
I wrote a generic class and want to crate its instance from a static
I want to create a class using reflection for a generic class. Could someone
I want to create a Generic DataGrid to use on all my Views/UserControls. This
I want to create a generic function with signature like this : void funcName<T>()

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.