Strategy Pattern

Tram Ho

This article describes the process of optimizing the behavior of objects according to strategy pattern

First we have class Gau with only one method hien_thi to show the shapes of the bears

Due to problem requirements, we add the “go” action to the bears.

Way 1

But because not all bears are the same, we have to override the subclass

=> The supclass inherited from the superclass “Gau” causes complications, difficult to maintain and expand later

Method 2

=> Although the transforms are separated into independent modules, the expansion is still complicated due to having to create subclasses inherited from the superclass.

Method 3

=> Here we see no longer inheritance between classes anymore and the creation of an object is clearer. If you want to expand just add the necessary classes in the module “Type”. Maintenance and expansion become very easy.

Share the news now

Source : Viblo