I have a talk that talks about the misuse of so-called design patterns . The first problem is that people do not know what they are just such DPs, and who they serve, can be applied, what can be called PD and that they are everywhere, as far as you do not see them. The second problem is that people study them, learn the solution and then look for a problem to apply it. This is wrong, you must have a problem and find the best solution for it. You may happen to be one of the DPs.
If you do not know how to apply the Strategy project pattern, you do not need it. At least not where you plan on using it. If it is suitable its use is natural and does not need selectors, you create an object according to a demand and in it passes the strategy to be used, the standard is just this. Depending on the context of the application will be a different strategy. Eventually one arrives in this context with a if
or switch
, but this is tangential and secondary, it is not what defines the strategy to be used but rather how to operate properly in that context.
With a question not very clear you can not know what is most appropriate. For an appropriate decision you need to understand all the requirements and know the entire organization of the application. DP can not be used as a cake recipe without context. The question is about different vehicle strategies, I do not even know if it makes sense to use this DP in a case like this.
To tell you the truth, I doubt that is the case of using Strategy there. It seems to me that there is a completely different object that must deal with the tariffs, it makes no sense to try to insert the tariff inside the motorcycle or car. There is a relationship between the fare and the vehicle and the time used, but it is not the same object, a fare is a property of a vehicle, unless the specific class of the vehicle is not even a vehicle and is poorly named. p>
Here comes the criticism I make of object-orientation. If people can not give certain names and classify objects correctly, there is no methodology or paradigm that makes the software well written. Without good understanding of ontology and taxonomy you can not program OO. This code seems to fail at these points, so any penduricalho that tries to put in the code is already wrong because it is using a wrong base.
I do not guarantee these things that I am putting here because the question is not clear. When the problem is not clear to the point of not being able to describe the problem in Portuguese it is more complicated to write in Java or another language. All solutions have the potential to be wrong, using DP or not.
The other answer says how to use Strategy DP, but does not say that this is the right thing to do and hope you do not think it is. This form violates cohesion forcing a class to take care of things that do not concern you.
Another conceptual error is putting Strategy as a temporary means of doing something. This DP was created to create objects that will have the same strategy throughout their life time and not change according to the situation. Again, this could even be the case if it is not a vehicle but a completion of a vehicle, which is completely different from being a vehicle or even a stay.
Most of the problems we deal with are composition and OOP has inheritance, polymorphism, or grouping things on the same object, so OOP does not work well in most cases that people think it is the solution.