F6: Delegater + Lambda-uttryck Flashcards

1
Q

Consider the scalability and maintainability of delegate-based approach compared to Strategy pattern.
What are the pros and cons of delegate-based approach?

A

Delegate-based approach:

Pros:

  • Simpler and more lightweight than the Strategy pattern.
  • Easier to understand and implement for small-scale applications.
  • Can be defined inline, making it more * flexible and easier to use in certain scenarios.
  • Can be used to implement a wide range of behaviors, not just those that fit a specific interface.

Cons:

  • Can become unwieldy and difficult to maintain as the number of behaviors and their complexity grows.
  • Can lead to code duplication if the same delegate is used in multiple places.
  • Can be harder to test in isolation, as the behavior is defined inline and may be tightly coupled to other code.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Strategy pattern vs delegate based approach
What are the pros and cons of Strategy pattern approach?

A

Strategy pattern:

Pros:

  • More scalable and maintainable than the delegate-based approach for larger-scale applications.
  • Encourages separation of concerns and modularity, making it easier to test and maintain.
  • Can be used to implement a specific interface, making it easier to understand and use in certain scenarios.
  • Can be used to implement a wide range of behaviors, not just those that fit a specific delegate signature.

Cons:

  • More complex and heavyweight than the delegate-based approach.
  • Requires more code and infrastructure to implement, which can be overkill for small-scale applications.
  • Can be less flexible than the delegate-based approach, as the behavior must be defined in a separate class and may require more setup and configuration.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Reflect on the refactoring process. How do delegates make the codebase more concise and intuitive compared to Strategy pattern?

A

With delegates, we can, taking the labb as en example:

  • Define a common signature for the weapon actions and pass methods that match that signature to the Character constructor. This allows us to encapsulate the behavior of each weapon in a separate method, without the need for a separate class for each behavior.
  • This approach is more lightweight and flexible than the Strategy pattern which requires a separate class for each behavior and a more complex infrastructure to manage them.
  • By using delegates, we can define the behavior inline and pass it directly to the Character constructor, making the code more concise and easier to understand. This approach also allows us to define the behavior in the same file as the Character class, which can make the codebase more organized and easier to navigate.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Vad är en delegat?

A
  • En datatyp vars värden är metoder
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Jag har följande kod, och vill skapa delegatinstanser och koppla dom till metoder och anropa metoderna via delegaterna.
Vad skriver jag och vart skriver jag koden?

// Delegatdefinition
public delegate void GreetDelegate(string name);

public class Program
{
public static void SayHello(string name)
{
Console.WriteLine($”Hej {name}!”);
}

public static void SayGoodbye(string name)
{
    Console.WriteLine($"Adjö {name}!");
} "
A

public static void Main()
{
// Skapar delegatinstanser och kopplar dem till metoder
GreetDelegate helloDel = SayHello;
GreetDelegate goodbyeDel = SayGoodbye;

    // Anropar metoder via delegater
    helloDel("Alice");
    goodbyeDel("Bob");
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why might one use a delegate instead of an interface in certain scenarios?

A

Delegates can provide a more lightweight and flexible way to represent single methods or behaviors without the need for defining separate classes or interfaces. They can make code more concise, especially when the behavior can be expressed as a simple function or as a lambda.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do delegates offer an alternative to the Strategy pattern in design?

A

Instead of defining an interface and multiple implementations (as in the Strategy pattern)
Delegates allow you to encapsulate a method directly, providing a more concise way to define and pass behavior.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly