Skip to main content

Questions tagged [delegates]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0 votes
3 answers
210 views

Are there any benefits to delegation over subclassing/inheritance in the case of a singleton?

Example: The MacOS API known as Cocoa uses Delegation to specify various behaviors within the app. A Delegate is a separate object that implements a set of methods that are called by an original ...
CPlus's user avatar
  • 1,189
-2 votes
1 answer
152 views

Resorted to Unconventional( i.e., hacky) "circuitous" programming code techniques when using populating a list of C# Action Delegates via a for loop [closed]

AWS Amazon.S3.Model.PutObjectRequest is merely a 3rd-party AWS Data Transfer Object (DTO) / Plain Old C# Object (POCO) type that can be used to build a request that can be used to send requests to an ...
user1338998's user avatar
2 votes
1 answer
174 views

Functions vs Classes for delegate pattern

Please consider the following sample #include <cstdio> #include <functional> #include <memory> #include <utility> class PaintDelegate { public: virtual ~...
Zoso's user avatar
  • 241
2 votes
1 answer
186 views

How to refactor same block of delegate code into a single private method?

I have these lines in multiple test methods. Now, trying to create a private method to clean up but not sure how to do it. Any suggestion or pointer would be really helpful. Thanks. var ...
Sharif Mamun's user avatar
38 votes
10 answers
10k views

Why do heavily object-oriented languages avoid having functions as a primitive type?

As has been covered to the point of parody, heavily object-oriented languages, such as C# or Java, tend to lack the feature of having functions as a primitive type. You can argue about whether or not ...
J. Mini's user avatar
  • 1,037
0 votes
2 answers
586 views

Turn an asynchronous delegate pattern into a blocking method

I have to use a built-in API that works like this class MyAPIWrapper { func callAPI() { apiObj.delegate = self apiObj.doWork() // returns immediately, eventually calls self.delegate....
Max's user avatar
  • 186
0 votes
2 answers
528 views

Design issue with delegation, inheritance and dependency injection

My question relates to usage of delegation together with inheritance and dependency injection. I have a MailerService class that requires a delegate in order to do its job. Furthermore, I have a ...
balteo's user avatar
  • 191
2 votes
2 answers
4k views

Fundamental differences between a List<Action> vs Action

Is there any fundamental difference between to using an action and a list of actions? Seeing that action is a delegate and therefore is a list itself. For instance: List<Action> ...
johnny 5's user avatar
  • 335
0 votes
1 answer
328 views

How to generalize which delegate is being used from an interface

I currently have the following set up where I have 2 or 3 classes implementing IZoneController and I have a set of conditions determining which function they need to execute when called. I first ...
Daniel Shank's user avatar
-1 votes
3 answers
432 views

Refactoring - Resolve dependencies between legacy code used by third parties

I am developing two libraries in .Net Firs one it's a library with core functionality (named it Library.Core.dll) Let's focus in User class public class User { //set of constructors and ...
X.Otano's user avatar
  • 622
4 votes
2 answers
1k views

Is there an Ideal way of storing namespace-related elements?

In the title, with namespace-related elements, I refer to Enums, Delegates and other elements that do not belong to a single class, but to the whole namespace, or application. I know that I can cram ...
oneManArmin's user avatar
19 votes
5 answers
7k views

Using Func instead of interfaces for IoC

Context: I am using C# I designed a class, and in order to isolate it, and make unit testing easier, I am passing in all its dependencies; it does no object instantiation internally. However, instead ...
TheCatWhisperer's user avatar
4 votes
1 answer
5k views

Is it possible to method-inject an object call?

Consider this example of method injection: static void SaveEmail(Func<string> getEmailFunction) { dbcontext.SaveEmail(getEmailFunction()); } static string GetEmail() { var frmUser = ...
John Wu's user avatar
  • 26.9k
1 vote
2 answers
192 views

What is 'better' - An event that tells you that something happened or accepting a delegate to be called when the event triggers internally?

I am writing a collection that accepts a time parameter, the purpose is that after that specified amount of time have passed the element won't be present in the collection. I want the user of this ...
Belgi's user avatar
  • 567
0 votes
0 answers
67 views

If a delegate contains a tail call request, what happens?

Simply put, what happens when I have a delegate, that delegate contains a method with a tail call request, and then I call it from a non-tail position? If I called the method directly, it would ...
Telastyn's user avatar
  • 110k
14 votes
3 answers
6k views

Why aren't properties implicitly convertible to delegates

We all know that properties in C# get compiled to actual plain old methods. But unlike method(-group)s, they can't be given as arguments to other methods that expect a delegate like a Func<T> or ...
sara's user avatar
  • 2,579
0 votes
1 answer
834 views

Is it a good practice to use a Service without Delegate?

I have a simple REST application with a single controller. The application needs to fetch data from a Database, convert it to JSON and return this to the caller. I need to decide what layers I should ...
Diyarbakir's user avatar
4 votes
1 answer
141 views

Using delegates as named lambdas

Suppose we have a domain in which we can generalise some operation by passing in a function, like the Select \ map functions. Now suppose that the function we pass in has some domain specific name, ...
Andy Hunt's user avatar
  • 6,036
6 votes
3 answers
5k views

Differences between "first-class function" mechanisms

Some languages (Javascript, Python) have the notion that a function is an object: //Javascript var fn = console.log; This means that functions can be treated like any other object (first-class ...
Zev Spitz's user avatar
  • 693
3 votes
1 answer
2k views

Strategy Pattern not sufficient for my problem?

Let me sketch the situation: I have multiple users, with certain properties (2 enums) For each user I need to fetch data, for some with some basic filtering, for some extended filtering (= basic ...
Alexander Derck's user avatar
2 votes
1 answer
5k views

What is the difference between Adapter and Delegation design pattern?

I am very confused about Adapter and Delegation design pattern. In Adapter pattern we bring an intermediate class to interact with another class. And in Delegation pattern we also bring an ...
Muztaba Hasanat's user avatar
2 votes
1 answer
184 views

how to split up a delegate class

I have a communication class that incapsulates all the apis of a remote application. This class happens to be a delegate class because it is interchangeable with another one (the remote application ...
Vittorio Zamparella's user avatar
1 vote
3 answers
2k views

Using delegates to avoid duplicate creation of resources

I'm writing a PCL that uses an HttpClient to go visit a few sites and extract data from them. My initial code looked like this: public static class Download { public async static Task<byte[]&...
James Ko's user avatar
  • 358
2 votes
3 answers
264 views

Many different classes that need similar functionality. Best approach?

I have many (at the moment around 30) different message classes in an application I am creating. Each of these messages need to be serialized and deserialized. However, the process of serializing and ...
Walkingsteak's user avatar
3 votes
2 answers
418 views

Why aren't field-like events implemented as a list of delegates?

tl;dr: Why are field-like events implemented as a single delegate field? Wouldn't it be more straight-forward to use a list of delegates, thereby eliminating the null special case and avoiding all the ...
Heinzi's user avatar
  • 9,838
2 votes
1 answer
2k views

Avoiding closures

I have a data structure in the form of a tree. Each node represents a cardboard box. The boxes can contain child boxes. public class CardboardBox { public int Id {get; set;} public int ...
LegendLength's user avatar
0 votes
1 answer
103 views

Is it ok for a for a View to dismiss itself?

Currently, I'm working on a project in which a view is dismissing itself. While talking to another programmer on the team for this project, he said that it's fine (in that it behaves correctly), but ...
narner's user avatar
  • 145
2 votes
1 answer
788 views

Generic delegates (C#) in Scala?

I wanted to know if there is an equivalent of the C# generic delegates in Scala. The reference website is: http://msdn.microsoft.com/en-us/library/dd233060.aspx I'm trying to make a complete ...
Jolien Neirynck's user avatar
2 votes
2 answers
2k views

Understanding C# Delegates Structure

I've been trying to understand C# delegates using Pro C# 5. In short, the author describes the motivation for delegates as a structured way of dealing with function pointers and callbacks or two-way ...
Isaac Kleinman's user avatar
0 votes
0 answers
59 views

Own Exception or Delegation design pattern to exit a program?

I am writing an application in Java which use MVC pattern design. I have 4 major classes: Main, Controller, Model, View. Main initialize the other three and run launchMenu() in a infinite loop, a ...
Jorge's user avatar
  • 101
0 votes
0 answers
412 views

Delegate method privately called from delegate itself?

One has established design patterns so ingrained in their brain, that when sometimes somebody does something unexpected, it is a revelation. My colleague in an iOS project did call the delegate ...
Earl Grey's user avatar
  • 628
0 votes
1 answer
79 views

Is it fine for a class that creates objects to delegate them?

I have a large list of intercommunicating components; so I decided to have one class create all of them and then subsequently delegate them to other classes to facilitate finer tweaking and wiring ...
user2738698's user avatar
9 votes
4 answers
3k views

C# Delegates are immutable - but why does that matter?

This is a followup question to this other question. Background Working from the MSDN Delegates Tutorial (C#), I see the following: Note that once a delegate is created, the method it is ...
shieldfoss's user avatar
29 votes
7 answers
21k views

Why would I ever use delegates if I'm not doing events? [duplicate]

I am trying to learn how to do events in C#, and according to the MSDN Events tutorial, Events are declared using delegates. If you have not yet studied the Delegates Tutorial, you should do so ...
shieldfoss's user avatar
5 votes
6 answers
3k views

Do delegates defy OOP

I'm trying to understand OOP so I can write better OOP code and one thing which keeps coming up is this concept of a delegate (using .NET). I could have an object, which is totally self contained (...
Dave's user avatar
  • 662
4 votes
3 answers
3k views

Why use protocol, not call the method directly?

I was asked this question in an interview. For eg: UITableviewDelegate protocol has CellForRowAtIndexpath. Why make it a delegate method in a protocol not a method in the UITableView class and call it ...
user96307's user avatar
12 votes
2 answers
22k views

What is Delegation and why is it important in iOS programming?

At the moment I am teaching myself iOS programming, and one concept I find really hard to wrap my head around is delegation. What is it? Why and how is it used? What is the advantage? The technical ...
TestinginProd's user avatar
3 votes
2 answers
1k views

Template method within one class without subclasses or inheritance

I have an algorithm with mostly invariant parts that needs to be reused within one class so as to stay DRY. Code duplication with repeating method structure public void save(String key, int value) {...
Cel's user avatar
  • 145
10 votes
5 answers
27k views

What are the advantages of the delegate pattern over the observer pattern?

In the delegate pattern, only one object can directly listen to another object's events. In the observer pattern, any number of objects can listen to a particular object's events. When designing a ...
JoJo's user avatar
  • 1,495
7 votes
2 answers
5k views

MVC and delegation

I am a beginning iOS programmer and use the Model-View-Controller model as a design pattern: my model doesn't know anything about my view (in order to make it compatible with any view), my view doesn'...
Tim Vermeulen's user avatar
4 votes
2 answers
4k views

Strategy vs Delegates

Can the Strategy design pattern entirely replace delegates? In Java, for example, there are no delegates. Is it possible to gain all the features of delegates by using Strategy design pattern? Edit:...
superM's user avatar
  • 7,353
5 votes
1 answer
2k views

Need to process 2 million 100k messages per second and route them to a particular event, delegate or multicast delegate

I need to process 2 million messages per second (perhaps in a scale out configuration) and route each message to a N delegates or multicast delegates. Question How should I structure my application in ...
makerofthings7's user avatar
6 votes
2 answers
638 views

Delegates and events

What is the difference between public delegate void SecondChangedHandler(Object clock, TimeInfoEventArgs ti); public event SecondChangedHandler OnSecondChanged; and public event EventHandler<...
user47646's user avatar
4 votes
2 answers
2k views

How do delegates fit into ASP.NET?

A developer told me they used delegates to bind most of their events in ASP.NET. Until then, I did not even know it was possible to use delegates in ASP.NET in a meaningful way. My understanding ...
P.Brian.Mackey's user avatar
7 votes
4 answers
975 views

Can you overuse delegates and events

I've just started getting to grips with event driven programming and I'm finding delegates and events to be very useful. Since starting to see the potential I've began using them all the time to solve ...
rainhamtown's user avatar
2 votes
5 answers
715 views

Delegate vs Method

Is there any advantige using delegates instead of methods? For example: public void LoginAuthenticate(object sender, AuthenticateEventArgs e) { if (Membership.ValidateUser(lgUserLogin.UserName, ...
Alex Maslakov's user avatar
2 votes
2 answers
816 views

Delegate pattern for retrieving data v.s. using simple setters

I am considering using the Delegate Pattern to get some data necessary for an operation. There are a few different pieces of data that the algorithm needs to run, but they all have default values that ...
drewag's user avatar
  • 269
17 votes
10 answers
57k views

Real world use of C# Delegates [closed]

I think that I conceptually understand C# delegates, however, I am struggling to find a real world example where they would be useful. Can you provide some answers detailing how C# delegates were ...
AlexC's user avatar
  • 1,337