Re: Writing a simple function in C#

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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

.



Relevant Pages

  • Re: WaitForSingleObject and Thread
    ... Putting the cast in such context is *ALWAYS* a coding error. ... why did you manually expand LPVOID to void*? ... MainProc will block until the ReadThread is finished. ... reasonably certain that if all you do is launch a single thread and wait for it ...
    (microsoft.public.vc.mfc)
  • [PATH -mm -v2] Fix a race condtion of oops_in_progress
    ... extern int cause_of_death; ... void machine_restart ... context or a bust_spinlocks cleared the way for us. ...
    (Linux-Kernel)
  • Re: Warning to newbies
    ... alone "bogus" science. ... In that context, I believe the minotaurs should abandon C ... I particularly like "assign a pointer to void", ... NaN or either infinity is a trap representation. ...
    (comp.lang.c)
  • Generic Repository
    ... I am trying to create a generic repository over EF4. ... void Attach; ... Int64 Count(Func<T, Boolean> predicate); ... private ObjectContext _context; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [PATCH 6/6] AIO wait page and AIO lock page
    ... return status (which may be non-zero when the wait queue ... queue parameter is the running task's default io wait context. ... * Get a lock on the page, assuming we need to sleep to get it. ... -extern void FASTCALL); ...
    (Linux-Kernel)