+ 3
Hello, Elias ! Let's explain the strategy pattern the easy way: You have a class Car() with a method run(), so you use it this way in a pseudo language: mycar = new Car() mycar.run() Now, you may want to change the run()behavior on the fly, while the program is executing. For example, you might want to simulate a motor failure or the use of a "boost" button in a video game. There are several ways to do this simulation: using conditional statements and a flag variable is one way. The strategy pattern is another: it delegates the behavior of the run() method to a subclass: Class Car() { this.motor = new Motor(this); //passing "this" is important for the motor so it knows what it is running method run() { this.motor.run() } method changeMotor(motor) { this.motor = motor } } If you want to change the car's behavior, you can just change the motor.  https://stackoverflow.com/questions/91932/how-does-the-strategy-pattern-work
23rd Jun 2018, 12:24 PM
Alexander Sokolov
Alexander Sokolov - avatar