Delegate Pattern extends inheritance

Tram Ho

1. What is Delegate Pattern?

The Delegate Design Pattern removes decisions and complex functionality from the core object by distributing or delegating them to other objects. The Delegate Design Pattern removes complex decisions and functions from core objects by delegating them to other objects.

Delegate is a Design Pattern not included in the patterns given by the Gang of Four but it is also used quite a lot. Delegate Pattern works relatively similar to inheritance in object-oriented programming . However, between Delegate and inheritance there are some differences:

  • Inheriting a complete copy of a class and delegate only partially copies the class’s features.
  • Delegate is often used to copy features of multiple classes.

Let’s take a look at a very real example of Delegate, to transport goods we can transport by passenger car, train and plane, each transporting a different kind of cargo, light and necessary goods. urgently transport by air, bulky goods and need fast transport by passenger car, goods can be transported slowly by train. So you have 3 classes: RailShipper, BusShipper and PlaneShipper, all three classes have a delivery method, with normal handling we need to go through conditional options to choose the type of transportation. . You will authorize a ShipperHandler class, which will help you call the delivery methods of the RailShipper, BusShipper and PlaneShipper classes without knowing the classes. That is, ShipperHandler class has copied methods from 3 classes.

2. Delegate Pattern UML

3. Delegate Pattern application example

In this example, we look at a digital music management application, the songs are saved with the path and the title, we have two types of playlists: M3U and PLS. The Playlist class can provide and manage playlists.

The Playlist class contains an array of songs, the addSong () method to add songs to the playlist with two parameters, the MP3 file path and the song name. Playlist can be provided in both formats M3U and PLS through two methods, respectively getM3U () and getPLS (). The next code snippet creates a Playlist, adds two songs and, depending on the playlist format, retrieves the playlist in the correct format.

However, there are many other playlist formats, programmers need to develop applications that get many different types of playlists. A NewPlayList class is implemented using Delegate Pattern.

The NewPlaylist class has a constructor with the $ type parameter, so it can create custom objects. The getPlaylist () method will delegate the getPlaylist () method of the dynamic instance. The getM3U () and getPLS () methods will be passed to the authorization object:

Meanwhile, taking the playlist will not be hard-coded:

4. Standardize Delegate Pattern

The above examples we have applied Delegate Pattern, but it is difficult to apply simultaneously to other applications. Let’s create a Delegate class that can be applied anywhere:

With the Delegate class above, we can reuse it anywhere.

5. Conclusion

Delegate Pattern has many similarities with inheritance in object-oriented programming but it is more extended, it also has similarities with Proxy Pattern but each pattern is useful in different situations. Design patterns help you develop applications faster with the experience built into the patterns that will solve many common problems. The use of desgin pattern also needs to be flexible, not applying rigid, rigid to get the best effect.

Share the news now

Source : Viblo