Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Command Design Pattern and Rollbacks

by hackdaddy (Hermit)
on Jul 16, 2002 at 00:17 UTC ( [id://181971]=perlquestion: print w/replies, xml ) Need Help??

hackdaddy has asked for the wisdom of the Perl Monks concerning the following question:

I am working on the design of an upgrade framework for upgrading our database applications. We are writing this application in object-oriented Perl.

We need to install the upgrade in phases. I realized that our upgrade process is a candidate for the Command Design Pattern.

We perform the upgrade in a series of phases and need the ability to rollback phases. We need the phases to be able to execute SQL, makefiles, Perl scripts, Ksh scripts, etc. If we restart the upgrade it should keep its state and return to the last completed phase. We also need to be able to log every action.

Being new to Design Patterns, I am looking for examples of the Command Design Pattern in Perl. I am also looking for pointers on the best way to design this upgrade framework.

Futhermore, are there any pre-existing Perl upgrade tools that are open source? We are using Quest Schema Manager to find schema differences: http://www.quest.com/schema_manager/

Replies are listed 'Best First'.
Re: Command Design Pattern and Rollbacks
by djantzen (Priest) on Jul 16, 2002 at 03:56 UTC

    Implementing the Command pattern in Perl adequately really shouldn't require much work as it doesn't depend on qualities that one must coerce Perl into exhibiting, like private variables, or private subroutines without resorting to closures1. For this pattern however there is a particularly promising implementation available in Perl due to the fact that almost anything can be an object, including a subroutine. For example:

    package Action; sub new { my ($class, %args) = @_; my $coderef = sub { while (my ($k, $v) = each(%args)) { ... } }; return bless($coderef, $class); } sub foo { $_[0]->(); }
    In this case, when foo() is invoked the first element of @_ contains a reference to the blessed subroutine, which may then be called by dereferencing. More complex versions of foo() might allow parameters to be passed to the subroutine to vary behavior in whatever way you need, or the module might define additional methods that call the subroutine in various ways. Take a look at TheDamian's Object Oriented Perl chapter 5.2 for (much) more.

    And now a word of caution. It sounds like this is a first draft of a very complicated system. I think it's highly likely that you'll discover as you go many many problems and requirements that you had not anticipated. And though patterns can yield great elegance and capability, if implemented without a clear vision they will fast begin to feel like concrete shoes. (see Design Patterns Considered Harmful) I strongly suggest a prototype free of patterns. Once you've thoroughly mapped out what the system requirements are, then go back and rewrite using patterns (if they're still applicable at that point).


    1. Sometimes with design patterns translating between languages can be difficult due to differences in the design of the language. Take the Singleton pattern -- in C++ it's performed using a private static variable to ensure that only one instance of the class ever exists, a private constructor, and a single public class method to control creation and subsequent access. In Perl to get equivalent functionality you'd need to make use of persistent lexically scoped variables, as in:
      package SingletonObject; { my $instance; sub instance { unless (defined $instance) { $instance = bless ({}, $class); # other initialization stuff } return $instance; } }
    Update: Fixed a couple of typos, grammatical errors.

Re: Command Design Pattern and Rollbacks
by Abigail-II (Bishop) on Jul 16, 2002 at 10:17 UTC
    Your requirements for upgrading aren't Perl specific. Looking for a Perl specific answer isn't the right way of going. There are several package managers out there that will have the functionality that you require. Unfortunally, package managers tend to be very non-portable. Suns package manager will do what you want it to do, and so will HP's swinstall. There's a entire army of package managers for Linux systems, but whether they will have all the functionality that you require, I cannot answer.

    Abigail

Log In?
Username:
Password:

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

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

    No recent polls found