Re: Writing a simple function in C#
- From: Moty Michaely <Moty.Mi@xxxxxxxxx>
- Date: 26 May 2007 01:37:25 -0700
On May 26, 4:59 am, Jon Harrop <j...@xxxxxxxxxxxxxxxxx> wrote:
Moty Michaely wrote:
Use the Command Design Pattern to encapsulate lot's of parameters.
Can you elaborate on this?
I've read this description of the pattern:
http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/command.htm
and I can't see how it is relevant. Is this for when you have a set of
optional arguments that are common to several different functions?
--
Dr Jon D Harrop, Flying Frog Consultancy
The F#.NET Journalhttp://www.ffconsultancy.com/products/fsharp_journal/?usenet
Hi,
When you need to pass common arguments (not necessarily optional) you
can encapsulate the arguments with a class and implement a function in
that class..
For example (very simple one, you can always elevate it to much more
complex one):
To copy a file we can do this:
void CopyFile(string src, string dest)
Or we can encapsulate the CopyFile operation to a FileOperation class:
class FileOperationContext
{
string source;
string destination;
public FileOperationContext(string source, string destination)
{
this.source = source;
this.destination = destination;
}
}
abstract class FileOperation {
FileOperationContext context;
public FileOperation(FileOperationContext context)
{
this.context = context;
}
void Operate();
}
class FileCopy : FileOperation
{
public FileCopy (FileOperationContext context) : base(context)
{ }
override Operate() {
System.IO.File.Copy(context.source, context.destination);
}
}
You can of course not implement methods and just use the class as an
argument list..
for example:
void CopyFile(FileOperationContext context) {
System.IO.File.Copy(context.source, context.destination);
}
void MoveFile(FileOperationContext context) {
System.IO.File.Move(context.source, context.destination);
}
etc....
Hope this clears things up.. =)
Moty
.
- References:
- Writing a simple function in C#
- From: Jon Harrop
- Re: Writing a simple function in C#
- From: Jon Skeet [C# MVP]
- Re: Writing a simple function in C#
- From: Jon Harrop
- Re: Writing a simple function in C#
- From: Jon Skeet [C# MVP]
- Re: Writing a simple function in C#
- From: Moty Michaely
- Re: Writing a simple function in C#
- From: Jon Harrop
- Writing a simple function in C#
- Prev by Date: Re: checking for Open Files
- Next by Date: Re: FileSystemWatcher
- Previous by thread: Re: Writing a simple function in C#
- Next by thread: Re: Writing a simple function in C#
- Index(es):
Relevant Pages
|