How Efficient is Delegation Design Pattern in Angular
Tags: javascript, angular, typescript, node
Published: 2023-01-09 06:21:57

A quick introduction to Delegation Design Pattern. As the name implies, it delegates the context to a Delegate.

---
## “You may not need State Management”
---

**Intent**

- To reduce chaotic dependencies between the context.

- A team of developers can work simultaneously on what they want to do with the context.

- Prevents direct public method calls from the Delegator since it’s only an internal class used by the Event class.

- Prevents changing of the original context.

- Also, it resolves communication in micro frontend architecture.

**How to Implement**

- Declare an interface of the Delegator and define the desired method between the Delegator and Delegate.

![ICovid19TrackerDelegateEvent](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y69qdleclnfyhgqk5fzd.png)

- Declare the Delegator class and implement the interface. Make sure the Delegator class accepts a [Map Object](https://dev.to/kptengco/advantages-of-using-dictionary-or-map-object-in-angular-1ben) of Delegate from the constructor.

![Covid19TrackerDelegator](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1uxanakfz73paa07x5ve.png)

- Declare the Event class as Injectable and Singleton. Then in the constructor, instantiate the Delegator class and assign it to a private property.

![Covid19TrackerEventService](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0frkf6x2j1firv3dfhd7.png)

- Make sure the Event class has:

![A Map Object of Delegate, so we can pass it when instantiating the Delegator class.
](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jr5a0ol8u972kp60ghtt.png)
> A Map Object of Delegate, so we can pass it when instantiating the Delegator class.

![A public method to register and unregister the Delegate.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/spc7kafb2xu8z7ub1x3x.png)
> A public method to register and unregister the Delegate.

- Declare the Delegate and implement the Delegator interface. To register the Delegate, use the Event class as a Dependency Injection of the Delegate and call the register method.

![Covid19FeedComponent](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rp6w72pdxulwshr0vd73.png)

---

![This is the Abstract class and implements the interface of the Delegator for code reuse.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v8jw81rj83tyydvkpmhz.png)
> This is the Abstract class and implements the interface of the Delegator for code reuse.

![In the constructor, register the Delegate using the Event class and save the Delegate ID. We will use the Delegate ID to unregister when the component is going to be destroyed.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5xw7xhqmogurhqz0k2n3.png)
> In the constructor, register the Delegate using the Event class and save the Delegate ID. We will use the Delegate ID to unregister when the component is going to be destroyed.

![These are the methods that can be reused for the Concrete class.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v7he1ksycwjnff0g9pht.png)
> These are the methods that can be reused for the Concrete class.

Below is an example of Concrete classes that serve as the Delegate.

![This Concrete class is used to display the total number of active cases and the last update on Covid19.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/flaqe2s91lf8a9v73mgx.png)
> This Concrete class is used to display the total number of active cases and the last update on Covid19.

![Covid19FeedSummaryComponent](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/001b48py6y8oxi06xqww.png)
> Covid19FeedSummaryComponent

![This Concrete class is used to display the top five countries with the highest number of active cases of Covid19.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0jl2i9x9r8cbbqlgdecs.png)
> This Concrete class is used to display the top five countries with the highest number of active cases of Covid19.

![Covid19FeedCountryRankComponent](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/em8tx0nt9alj50z0w5df.png)
> Covid19FeedCountryRankComponent

Below is a Concrete class used to display the list of countries grouped by continent with the highest number of active cases of Covid19.

```typescript
import { Component, ChangeDetectionStrategy } from "@angular/core";

import { Covid19Statistic } from "../../covid19-tracker/model";
import { Covid19GroupStatisticAdapter } from "../../model";
import { Covid19FeedComponent } from "../covid19-feed.component";

type GroupKey = string;

@Component({
    selector: "covid19-feed-list-component",
    templateUrl: "./covid19-feed.list.component.html",
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class Covid19FeedListComponent extends Covid19FeedComponent {

    private readonly listDictionary: Map<GroupKey, Covid19Statistic[]> = new Map();
    private readonly groupDictionary: Map<GroupKey, Covid19GroupStatisticAdapter> = new Map();

    public groups: Covid19GroupStatisticAdapter[] = [];

    public override statisticUpdate(statistic: Covid19Statistic[]): void {
        this.updateGroupList(statistic);
        this.sortGroupList();
    }

    public getList(group: Covid19Statistic): Covid19Statistic[] {
        const groupKey = this.generateGroupKey(group);
        const list = this.listDictionary.get(groupKey);

        if (list === undefined) {
            throw new Error(`'${groupKey}' groupKey is not found in the dictionary.`);
        }

        return list;
    }

    private updateGroupList(statistic: Covid19Statistic[]) {
        for (const stat of statistic) {
            if (stat.continent === "All") {
                continue;
            }

            const groupKey = this.generateGroupKey(stat);
            const group = this.listDictionary.get(stat.continent);

            if (stat.continent === stat.country) {
                this.groupDictionary.set(groupKey, new Covid19GroupStatisticAdapter(stat));

                if (group === undefined) {
                    this.listDictionary.set(groupKey, []);
                }

                continue;
            }

            if (group === undefined) {
                this.listDictionary.set(groupKey, [stat]);
            } else {
                group.push(stat);
            }
        }
    }

    private generateGroupKey(statistic: Covid19Statistic): string {
        return statistic.continent;
    }

    private sortGroupList(): void {
        this.groups = Array.from(this.groupDictionary.values())
            .sort((compareA: Covid19GroupStatisticAdapter, compareB: Covid19GroupStatisticAdapter) => {
                return compareB.statistic.cases.active - compareA.statistic.cases.active;
            });

        for (const list of this.listDictionary.values()) {
            list.sort((compareA: Covid19Statistic, compareB: Covid19Statistic) => {
                return compareB.cases.active - compareA.cases.active;
            });
        }
    }
}
```
![Covid19FeedListComponent](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/usma7i70e05cwlbvr5y5.png)
> Covid19FeedListComponent

---

- Finally, we will use the Event class to delegate some context.

![This component has three Delegate components in the template, see the figure below.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v6mwpouia6m13k07ffpy.png)
> This component has three Delegate components in the template, see the figure below.

![Component template](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fdawlwwiyvfltjasym2r.png)

On initialization of the component, it will call the load statistics method. This is an event method that delegates some context. Let’s review the method quickly.

![Class method review](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xbiz3sgfckdp4nmn2ut4.png)

- The statistic loading method accepts three states. LOADING, SUCCESS, and ERROR and delegate this context to the (concrete class) Delegate. The Delegate will receive this state and do what it needs to be done.

- The statistic update method accepts an Array of “Covid19Statistic” (the model) class instances and delegates this context to the (concrete class) Delegate. The same thing, the Delegate will receive this state and do what needs to be done.

---

That technically sums up how Delegation Design Pattern works. Below is the output of the application.

![The API used is for testing purposes only, so these numbers are not real.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fdr3f12jtxq6zow5mxn1.png)
> The API used is for testing purposes only, so these numbers are not real.

**Pros**

- The Delegate has clear method definitions of the Delegator to handle the context being delegated.

- The files are easy to track because of the implementation of the interface of the Delegator.

- Open/Closed Principle – Easy to introduce a new Delegate without breaking the existing client code.

- Single Responsibility Principle – The communication is in one place that comes from the Event class, and using the Delegator interface makes it much easier to comprehend and maintain.

- Can reduce coupling between various components or classes.

- Can reuse the individual components or classes much more manageable.

**Cons**

- When the interface of the Delegator is somehow abused, meaning it does not have segregation of interfaces or different behaviors are in one place.

**Conclusion**

- Delegator Interface – used to describe the methods to communicate between the Delegator and Delegate class.

- Delegator Class – implements the Delegator interface and delegates the context to the Delegate and is only used internally by the Event class.

- Event Class – uses the Delegator class as internal and calls its method to delegate the context.

- Delegate Class – implements the Delegator interface and uses the Event class to register or unregister itself. Also, it can call some event method from the Event class to trigger some Delegator method.

---

By the way, did you notice the Adapter model? If you don’t know how it works or need more explanation about it, subscribe now to get more updates. 😉

![Adapter model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8lyhyae1d0jfragl98xg.png)

---

Thanks for reading!