static void Main(string[] args) { var processor = new ImageProcessor(); var filters = new ImageFilters(); Action <Image> imageFilterHandler = filters.ApplyBrightness; imageFilterHandler += filters.ApplyContrast; imageFilterHandler += ResizeImage; processor.Process("C:/Photos/sample.jpg", imageFilterHandler); }
//Other developers won't be able to apply a filter that hasn't been defined //To make a new filter, he's going to have to decompile and redeploy the code //With delegates a developer create his own filter without relying on this code public void ApplyFiltersWithoutDelegate(string path) { var image = Image.Create(path, "img"); var filters = new ImageFilters(); filters.ApplyBrightnessFilter(image); filters.ApplyContrastFilter(image); filters.ApplyResizeFilter(image); image.Save(); }
static void ClientWithDelegate() { ImageProcessor imageProcessor = new ImageProcessor(); ImageFilters imageFilters = new ImageFilters(); ImageProcessor.ImageFilterHandler filterHandler = imageFilters.ApplyBrightnessFilter; //To add another filter //Those methods must have the same return type as well as the same parameters filterHandler += imageFilters.ApplyContrastFilter; //Add created filter filterHandler += ApplyRedEyeFilter; imageProcessor.ApplyFiltersWithDelegate(@"C:\img.jpg", filterHandler); }