Questions tagged [delegates]
The delegates tag has no summary.
63 questions
0
votes
2
answers
301
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 ...
-2
votes
1
answer
168
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 ...
2
votes
1
answer
215
views
Functions vs Classes for delegate pattern
Please consider the following sample
#include <cstdio>
#include <functional>
#include <memory>
#include <utility>
class PaintDelegate {
public:
virtual ~...
2
votes
1
answer
203
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 ...
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 ...
0
votes
2
answers
63
views
Should an Express error handler be used to send HTTP 4xx responses?
In other words, should the raising of an HTTP 4xx code be considered an error, and should the job of sending an HTTP 4xx code to a client be delegated to an error handler?
Or is it simpler to just ...
4
votes
1
answer
2k
views
Delegate vs Forwarding in Java OOP
I'm reading some article about "prefer composition over inheritance", and heard about Forwarding and Delegation. After search for the different I found some source:
https://en.wikipedia.org/wiki/...
0
votes
2
answers
611
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....
13
votes
1
answer
5k
views
Delegation pattern in Python: is it unpopular? Is it considered not Pythonic?
As a Ruby/Rails person, I often deal with method delegation. It's often convenient to delegate a method to a composed object or a constant. Ruby even has a def_delegator helper method to easily build ...
0
votes
2
answers
548
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 ...
-1
votes
1
answer
75
views
Should we delegate user input sanitization/validation?
Consider this code:
new FrameworkClass( [ 'query' => $_POST[ 'input' ] ] );
FrameworkClass is supposed to do input sanitization and validation. Should we just trust 3rd party code to do it's job?
...
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> ...
0
votes
1
answer
342
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 ...
-1
votes
3
answers
450
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 ...
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 ...
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 ...
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 = ...
1
vote
2
answers
227
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 ...
0
votes
0
answers
76
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 ...
3
votes
2
answers
2k
views
Alternatives to the delegation pattern allowing blind messages between components
Delegates and delegation exist to enable passing specific messages to an observer, regardless of that observer's type. In a coordinator-type architecture, where coordinators manage and run the flow of ...
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 ...
0
votes
1
answer
896
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 ...
4
votes
1
answer
154
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, ...
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 ...
9
votes
1
answer
563
views
Code Design: Delegation of arbitrary functions
On PPCG, we frequently have King of the Hill challenges, which pit different code bots against each other. We don't like limiting these challenges to a single language, so we do cross-platform ...
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 ...
4
votes
1
answer
1k
views
Extension versus Delegation
I am using Swift and am wondering about the concepts of Extension and Delegation. Recently I found a case where either one of these two concepts could be applied. This reminded of the "composition ...
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 ...
3
votes
1
answer
385
views
How to DRY decorator pattern and other delegation in java
I was refactoring some java to use decorators. All of the decorators inherited from a class ThingDecorator, let's say. This consisted entirely of:
SomeType methodName(OtherType otherThing) {
...
2
votes
1
answer
205
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 ...
6
votes
1
answer
989
views
Best practice to authenticate third party to a website?
I've a website built with ASP.NET, and uses Cookie based Forms Authentication to protect it self from unauthorized access.
It also has a REST based API which uses API key based Authentication. (Key ...
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[]&...
2
votes
3
answers
299
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 ...
3
votes
2
answers
437
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 ...
3
votes
1
answer
194
views
Do conditional delegation or different member types break the Composite pattern?
Here's how I understand the composite pattern:
In the composite pattern, a root object is composed of objects which may be further composed. Moreover, to be considered composite, all those objects ...
2
votes
2
answers
165
views
Delegating work and programming to component interfaces
I have a MessageHandler class which receives and validates messages before determining which components in the architecture they should be delegated to so they can be processed. This involves calling ...
3
votes
1
answer
322
views
Language support for (syntactic) delegation in Java
Composition over inheritance is an old trend or even accepted state of the art in object oriented programming. It would be even easier to use in Java, if there were language support for delegation. ...
2
votes
1
answer
3k
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 ...
0
votes
1
answer
115
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 ...
3
votes
2
answers
707
views
How to use scala case classes when delegation is needed
Let's assume in our application we want to model cars. We also want to model a car repository where we store some registered cars. How should that be modeled in scala?
Here comes my approach: First, ...
2
votes
1
answer
802
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 ...
7
votes
1
answer
897
views
Why are Scala's Either and Option types not interfaces/traits but classes?
I wanted to create a class CompileResult, that can be treated like an Either type but has some additional useful methods. It should be a CompileSuccess or a CompileFailure (which, too, has some ...
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 ...
0
votes
0
answers
68
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 ...
0
votes
0
answers
425
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 ...
0
votes
1
answer
85
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 ...
9
votes
4
answers
4k
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 associated ...
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 ...
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 (...