EzDevInfo.com

delegation interview questions

Top delegation frequently asked interview questions

What is a C++ delegate?

What is the general idea of a delegate in C++? What are they, how are they used and what are they used for?

I'd like to first learn about them in a 'black box' way, but a bit of information on the guts of these things would be great too.

This is not C++ at its purest or cleanest, but I notice that the codebase where I work has them in abundance. I'm hoping to understand them enough, so I can just use them and not have to delve into the horrible nested template awfulness.

These two The Code Project articles explain what I mean but not particularly succinctly:


Source: (StackOverflow)

Implementing multiple interfaces with Java - is there a way to delegate?

I need to create a base class that implements several interfaces with lots of methods, example below.

Is there an easier way to delegate these method calls without having to create a horde of duplicate methods?

public class MultipleInterfaces implements InterFaceOne, InterFaceTwo {

    private InterFaceOne if1;
    private InterFaceTwo if2;

    public MultipleInterfaces() {
      if1 = new ImplementingClassOne();
      if2 = new ImplementingClassTwo();
    }

    @Override
    public void classOneMethodOne { if1.methodOne(); }
    @Override
    public void classOneMethodTwo { if1.methodTwo(); }
    /** Etc. */


    @Override
    public void classTwoMethodOne { if2.methodOne(); }
    @Override
    public void classTwoMethodTwo { if2.methodTwo(); }
    /** Etc. */

}

Source: (StackOverflow)

Advertisements

jQuery .on() and .delegate() doesn't work on iPad

If you try this snippet on desktop, everything works.
Whenever you try it on iPad, it won't do anything.

$('body').on('click', '#click', function() {
    alert("This alert won't work on iPad");
});
div { 
  font-size: 24px; 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="click">Click here</div>

Simple .click() handler works, but it isn't what I want. The same applies for .delegate(); and .live()

Is it a bug or something?


Source: (StackOverflow)

When to use Ruby DelegateClass instead of SimpleDelegator? (DelegateClass method vs. SimpleDelegator class)

Probably i am missing something simple, but i do not understand how to use Ruby's DelegateClass method, i mean when to use it instead of SimpleDelegator class. For example, all of the following seem to work mostly identically:

require 'delegate'

a = SimpleDelegator.new([0])
b = DelegateClass(Array).new([0])
c = DelegateClass(String).new([0])
a << 1
b << 2
c << 3
p a # => [0, 1]
p b # => [0, 2]
p c # => [0, 3]

Note that it does not seem to matter which class is passed to DelegateClass.


Source: (StackOverflow)

Long delegation chains in C++

This is definitely subjective, but I'd like to try to avoid it becoming argumentative. I think it could be an interesting question if people treat it appropriately.

In my several recent projects I used to implement architectures where long delegation chains are a common thing.

Dual delegation chains can be encountered very often:

bool Exists = Env->FileSystem->FileExists( "foo.txt" );

And triple delegation is not rare at all:

Env->Renderer->GetCanvas()->TextStr( ... );

Delegation chains of higher order exist but are really scarce.

In above mentioned examples no NULL run-time checks are performed since the objects used are always there and are vital to the functioning of the program and explicitly constructed when execution starts. Basically I used to split a delegation chain in these cases:

1) I reuse the object obtained through a delegation chain:

{ // make C invisible to the parent scope
   clCanvas* C = Env->Renderer->GetCanvas();
   C->TextStr( ... );
   C->TextStr( ... );
   C->TextStr( ... );
}

2) An intermediate object somewhere in the middle of the delegation chain should be checked for NULL before usage. Eg.

clCanvas* C = Env->Renderer->GetCanvas();

if ( C ) C->TextStr( ... );

I used to fight the case (2) by providing proxy objects so that a method can be invoked on non-NULL object leading to an empty result.

My questions are:

  1. Is either of cases (1) or (2) a pattern or an antipattern?
  2. Is there a better way to deal with long delegation chains in C++?

Here are some pros and cons I considered while making my choice:

Pros:

  • it is very descriptive: it is clear out of 1 line of code where did the object came from
  • long delegation chains look nice

Cons:

  • interactive debugging is labored since it is hard to inspect more than one temporary object in the delegation chain

I would like to know other pros and cons of the long delegation chains. Please, present your reasoning and vote based on how well-argued opinion is and not how well you agree with it.


Source: (StackOverflow)

What is the purpose of a delegation pattern?

I was looking through the source to SensorManager in Android and found that when you register a SensorEventListener the SensorManager passes control of the listener to a ListenerDelegate.

I only bring this up as an example. I read the Wikipedia article on delegate programming but I am still not sure of its purpose. Why would one use a 'delegate'? How does it help the control flow of a program? What are the disadvantages of using (or not) one? Is it most practical for use with listeners?

Edit: ListenerDelegate is on line 487 and the methods in question are around line 1054.


Source: (StackOverflow)

Difference between @Delegate and @Mixin AST transformations in Groovy

What's the difference between @Delegate and @Mixin AST transformations in Groovy.

Maybe my question has to do with OO and when apply different patterns, but I use both and I can achieve the same behavior.

class Person {
    String name = "Clark"
    def walk() { "Walk" }
}

@Mixin(Person)
class Superhero {
    def fly() { "Fly" }
}

def superman = new Superhero()
assert superman.name == "Clark"
assert superman.walk() == "Walk"
assert superman.fly() == "Fly"

class Person {
    String name = "Clark"
    def walk() { "Walk" }
}

class Superhero {
    @Delegate Person person
    def fly() { "Fly" }
}

def superman = new Superhero(person: new Person())
assert superman.name == "Clark"
assert superman.walk() == "Walk"
assert superman.fly() == "Fly"

Source: (StackOverflow)

Can I use OpenID delegation with a standard Google account? [duplicate]

This question already has an answer here:

I'm currently using ClaimID and have the following data on my website to allow delegation:

<link rel="openid.server" rel='nofollow' href="http://openid.claimid.com/server" />
<link rel="openid.delegate" rel='nofollow' href="http://openid.claimid.com/tjrobinson" />

Are there equivalent URLs for Google? If not, has there been any mention of support in future?

Before anyone marks this as a duplicate, I am aware of this existing question: How do you delegate your OpenId to Google Apps. However, that question refers specifically to Google Apps, I just want to know if I can use OpenID delegation with a normal @gmail.com account?


Source: (StackOverflow)

delegating into private parts

Sometimes, C++'s notion of privacy just baffles me :-)

class Foo
{
    struct Bar;
    Bar* p;

public:

    Bar* operator->() const
    {
        return p;
    }
};

struct Foo::Bar
{
    void baz()
    {
        std::cout << "inside baz\n";
    }
};

int main()
{
    Foo::Bar b;   // error: 'struct Foo::Bar' is private within this context

    Foo f;
    f->baz();     // fine
}

Since Foo::Bar is private, I cannot declare b in main. Yet I can call methods from Foo::Bar just fine. Why the hell is this allowed? Was that an accident or by design?


Oh wait, it gets better:

Foo f;
auto x = f.operator->();   // :-)
x->baz();

Even though I am not allowed to name the type Foo::Bar, it works just fine with auto...


Noah wrote:

type names defined within a class definition cannot be used outside their class without qualification.

Just for fun, here is how you can get at the type from outside:

#include <type_traits>

const Foo some_foo();

typedef typename std::remove_pointer<decltype( some_foo().operator->() )>::type Foo_Bar;

Source: (StackOverflow)

Difference between Decorator pattern and Delegation pattern

What is the difference between Decorator pattern and Delegation pattern (if there is any) ? I don't want to know just about implementation details but also about use case differencies and subjective point of view how to use them.

EDIT : Can you point to source code (in OS project) where these pattern (especially Delegation, because Decoration is used in Java IO classes) are used. I'm looking for some real usage not just dummy example. Maybe these patterns are the same differs only in title. Feel free to write this opinion.


Source: (StackOverflow)

ASP.Net web application trying to use Impersonation and Delegation to connect to SQL Server

I'm trying to use Impersonation and Delegation in an intranet ASP.Net web-app in order to pass authenticated users' credentials onto a SQL Server.

The web server and SQL server are two separate machines, but in the same domain, so Delegation is required.

I've done the following:

  • set <authentication mode="Windows"/> and <identity impersonate="true"/> in my web-app's web.config.
  • enabled Constrained Delegation from the web server to the MSSQLSvc service on the SQL Server, in Active Directory.
  • enabled only Windows Authentication in the website, through IIS.

Apparently this should all work, but it doesn't (the SQL Server is denying access to the anonymous user - "Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'").

In IIS7, the Application Pool is set to use Integrated Pipleline Mode and is running with the NetworkService Identity. The website only has Windows Authentication enabled, Extended Protection is Off, Kernel-mode authentication is enabled, and NTLM is the provider.

All the web pages I've read seem to indicate that my setup should work. What am I missing?


Source: (StackOverflow)

Javascript event delegation, handling parents of clicked elements?

http://jsfiddle.net/walkerneo/QqkkA/

I've seen many questions here either asking about or being answered with event delegation in javascript, but I've yet to see, however, how to use event delegation for elements that aren't going to be the targets of the click event.

For example:

HTML:

<ul>
    <li><div class="d"></div></li>
    <li><div class="d"></div></li>
    <li><div class="d"></div></li>
    <li><div class="d"></div></li>
    <li><div class="d"></div></li>
    <li><div class="d"></div></li>
</ul>​

CSS:

ul{
    padding:20px;
}
li{
    margin-left:30px;
    margin-bottom:10px;
    border:1px solid black;

}
.d{
    padding:10px;
    background:gray;
}
​

What if I want to add a click event to handle the li elements when they're clicked? If I attach an event handler to the ul element, the divs will always be the target elements. Apart from checking every parent of the target element in a click function, how can I accomplish this?

edit:

I want to use event delegation instead of:

var lis = document.getElementsByTagName('li');
for(var i=0;i<lis.length;i++){
    lis[i].onclick = function(){};
}

But if I do:

document.getElementsByTagName('ul')[0].addEventListener('click',function(e){

    // e.target is going to be the div, not the li
    if(e.target.tagName=='LI'){

    } 
},false);

EDIT: I'm not interested in how to use Javascript libraries for this, I'm interested in how they do it and how it can be done with pure js.


Source: (StackOverflow)

Composition vs. Delegation

Is there any difference in terms of implementation as how a composition design can be different from delegation. For example the code below seems to be doing delegation since the user cannot access the composed object (i.e "a") without using b. Hence, the user would need to invoke interfaces of class b and then "class b" invoke appropriate interfaces of "class a" making it delegation. Does this make sense ?

Class A {
friend class B;
private: 
A(){}; //dont want user to instantiate this class object since it wont sense without any context. Just like a room with no house.
void PrintStructure(){};
};

Class B{
public:
void PrintStructure(){a.PrintStructure();} //delegate

private:
A a; //composition
};

Source: (StackOverflow)

In OOP, what is forwarding and how is it different from delegation?

Would someone please explain the difference between forwarding and delegation? They seem similar, but I haven't been able to find a good definition of forwarding, so I'm not sure I really understand.


Source: (StackOverflow)

Access denied impersonating current user accessing network folder

Trying to list the directories and files within a specific folder. This folder will depend on the current user (Page.User) which logs in by Windows Authentication (NTLM) and is retrieved from the Active Directory (homedirectory property).

I am using a domain user to access the AD and retrieve the folder location, this works fine.

What fails is retrieving the sub folders using System.IO.DirectoryInfo.GetDirectories() even with impersonation.

Here's the code I'm using for impersonation:

System.Security.Principal.WindowsImpersonationContext impersonationContext;
impersonationContext =  ((System.Security.Principal.WindowsIdentity)User.Identity).Impersonate();

I have checked that the user being impersonated has access to the folder.

From what I have found so far it seems that I either need to set up delegation or Kerberos authentication, is this true? Are these the only ways to achieve this? Shouldn't impersonation be enough?


Source: (StackOverflow)