We can still iterate over the process collection but

Info icon This preview shows pages 177–179. Sign up to view the full content.

View Full Document Right Arrow Icon
We can still iterate over the process collection, but we no longer have a Process method on the object. The equivalent function on the delegate type is a method called Invoke which matches the signature of our delegated function: process.Invoke(doc); While this works just fine, it is such a common thing to need to do with a delegate that C# lets us dispense with .Invoke entirely and treat the delegate as though it really was the function to which it delegates: process(doc); Here’s the final version of our Process method: public void Process(Document doc) { foreach(DocumentProcess process in Processes) { process(doc); } } Functional Composition with delegate | 153
Image of page 177

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This can take a bit of getting used to, because our variable names are usually camelCased and our method names are usually PascalCased. Using function call syntax against a camelCased object can cause severe cognitive dissonance. I’ve still never really gotten used to it myself, and I always feel like I need a sit-down and a cup of coffee when it happens. Now we need to deal with the Configure method that sets up our processes. Rather than creating all those process classes, we need to create the delegate instances instead. You can construct a delegate instance just like any other object, using new , and passing the name of the function to which you wish to delegate as a constructor parameter: static DocumentProcessor Configure() { DocumentProcessor rc = new DocumentProcessor(); rc.Processes.Add( new DocumentProcess(DocumentProcesses.TranslateIntoFrench) ); rc.Processes.Add( new DocumentProcess(DocumentProcesses.Spellcheck) ); rc.Processes.Add( new DocumentProcess(DocumentProcesses.Repaginate) ); return rc; } However, C# has more syntactic shorthand that can do away with a lot of that boil- erplate code. It can work out which delegate type you mean from context, and you only need to provide the method name itself: static DocumentProcessor Configure() { DocumentProcessor rc = new DocumentProcessor(); rc.Processes.Add( DocumentProcesses.TranslateIntoFrench ); rc.Processes.Add( DocumentProcesses.Spellcheck ); rc.Processes.Add( DocumentProcesses.Repaginate ); return rc; } Not only have we achieved the same end in much less code, but we’ve actually reduced coupling between our subsystems still further—our DocumentProcessor doesn’t depend on any classes other than the Document itself; it will work with any class, static or oth- erwise, that can provide a method that conforms to the appropriate signature, as de- fined by our delegate. So far, we’ve only provided delegates to static functions, but this works just as well for an instance method on a class. Let’s imagine we need to provide a trademark filter for our document, to ensure that we pick out any trademarks in an appropriate typeface. Example 5-10 shows our TrademarkFilter class.
Image of page 178
Image of page 179
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern