Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: perl module structure (Façade choosing backend)

by LanX (Saint)
on Mar 30, 2018 at 10:52 UTC ( [id://1212025]=note: print w/replies, xml ) Need Help??


in reply to perl module structure

That's a rather abstract question. More concrete examples would be nice.

I was thinking bout examples which do "similar" things, and Term::ReadLine or JSON come to mind, which can switch between different backends.

My (maybe naive) approach would be to have a 2 modules for the start, a default implementation Foo::Bar::Standard for fall back and a façade Foo::Bar which exports the API functions which call those standard implementation functions.

Something like use Foo::Bar "Other"; would first import the functions of Foo::Bar::Other into package Foo::Bar instead of those from Standard before exporting the interface.

Another approach to access the implementation from Foo::Bar is of course to manipulate @ISA. The interface functions would access the implementation by inheritance.

I'm sure there are even more OOP patterns to do this and curious to read more replies.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Wikisyntax for the Monastery

update

Your mention of h2xs is puzzling me. Are you talking about C implementations?

Replies are listed 'Best First'.
Re^2: perl module structure (Façade choosing backend)
by pwagyi (Monk) on Mar 30, 2018 at 12:56 UTC

    Yes it’s rather abstract because I’m looking for general solution. I think what I’d like to do is something similar to java collection. List is an interface and there are multiple concrete implementations. So I might have pure perl and xs backend implementations. I just remember there’re some Perl modules if xs is available it’ll use xs backend implementation otherwise using pure perl.

    Update

    Design pattern wise, I am thinking of abstract factory pattern. There’ will be factory, products interfaces and of course actual concrete implementations.

      > I’m looking for general solution

      Sorry, this sounds like you are looking for a way to do Java in Perl. Many answers in the Java world don't apply to Perl because the questions don't arise.

      And it's not clear for me HOW the implementation should be chosen. automatically or by users choice.

      > List is an interface and there are multiple concrete implementations.

      Not an issue in Perl because it doesn't have static typing of the list elements.

      > I think what I’d like to do ...

      Please think harder what you want and show some Perl code exemplifying it.

      Most of us don't "speak" Java... ;-)

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1212025]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-23 21:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found