dimanche 8 novembre 2015

What Design Pattern to use to implement transaction or chaining mechanism

I implemented a simple Factory class in C# and Java. This class builds instances of concrete working classes that have one and the same interface. In particular all these classes have such methods as:

create
select
alter
etc.

Now I want to have a mechanism (built on top of some classical/non-classical Pattern) that will allow me to create "chains" of these methods or to encapsulate them in a kind of transaction. In a pseudo-code I expect to see something like:

Transaction tnx = create(...args...).alter(...args_2...);
//tnx.Execute();

Or something like:

Transaction tnx;
tnx.Start();
tnx.Add(method_name, ... variable list of arguments ...);
tnx.Add(another_method_name, ... variable list of arguments ...);
tnx.Execute();

I'm not that good at design patterns and I'm not sure what pattern to use. I hope someone can share and drop a couple of lines of code (in C# or in Java) that will demonstrate how this can be implemented. Thanks!

Aucun commentaire:

Enregistrer un commentaire