Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
You highlight the exact difference between a pattern and a class library. The Gang of Four who published the Design Patterns book decided to describe patterns, or recurring ideas in feature implementation, instead of publishing a book on some great classes to put into any language's built-in class library. Patterns are idioms, not parts.

It seems very neat and tidy to offer a class like Class::Singleton but the devil is in the details, and your exact scenario is where theory meets reality. Don't think "I have to glue on some premade code to adopt a pattern." Think "understanding the standard implementation of a standard pattern will help me effect the same feature in my own code quickly and robustly."

Another poster has already shown you how to make a typical singleton-compliant constructor. Make a hidden module variable that keeps track of which instance is regarded as the master instance, and check that variable before making a possibly redundant instance. That's the singleton pattern. It can be done in your own code in 30 seconds.

You don't need a class library to implement patterns for you, and any class library purporting to implement academic patterns for you is likely going to have some over-engineered non-elegant features bolted on to deal with real-world problems. They'll call it "flexibility."

--
[ e d @ h a l l e y . c c ]


In reply to Re: Making an Existing Class a Singleton by halley
in thread Making an Existing Class a Singleton by agianni

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-25 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found