Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A Strategy against Policy and a Policy against Strategy

When I first discovered the Strategy pattern, I was amazed of the seemingly endless possibilities it offered to me and my programs. I could better encapsulate my models' behaviour and even exchange this behaviour on the fly. But the strategy could also be used to to provide traits and payload to the containing object - data that was declared in a superclass. Life was fine.

class MyMonsterAI   { float const see_radius_; virtual void attack () = 0; /* .. */ };
class ElveAI        { ElveAI() : see_radius_(150.0f) {} /* ... */ };
class CycloneAI     { CycloneAI() : see_radius_(50.0f) {} /* ... */ };
class Monster       { MyMonsterAI* ai_; };

And along came the Policy pattern and it would allow me even greater flexibility in supplying parameters to a containing class - whole classes, outfitted however I liked, albeit dynamically exchanging the behaviour... that was not too easy (unless part of the policy was to have a strategy!).

class MyMonsterTrait { typedef typename ElveAI AI; };

template< class MonsterTrait >
class Monster : public MonsterTrait::AI
{
    void idle (void) { attack(); }
};

Both patterns seem to be very powerful to me and I like to use both, in different circumstances. But I'm not sure if there are particular/typical/more-practical applications for either at some situations.

I am wondering: where do you use strategies and where policies? Where are either better suited?

like image 457
mstrobl Avatar asked Oct 23 '08 20:10

mstrobl


People also ask

What is the difference between a strategy and a policy?

Strategy is a comprehensive plan, made to accomplish the organizational goals. Policy is the guiding principle, that helps the organization to take logical decisions.

What comes first between policy and strategy?

First you formulate a policy which is the principles or the protocols to guide decisions and next we can make a strategy and finally a detailed plan to achieve the strategy. It is a difficult question, indeed. They are all closely inter-related, and are all products of the process of planning.

What is the relationship between policies and strategies?

Policy deals with routine/daily activities essential for effective and efficient running of an organization. While strategy deals with strategic decisions. Policy is concerned with both thought and actions. While strategy is concerned mostly with action.

What is the similarity between strategy and policy?

A key similarity between strategy and policy is that both are often set at the top-management level of an organization. A management team usually collaborates to set goals and strategy for how to operate the company in a profitable way.


2 Answers

Policies are largely set at compile time, while strategies are set at runtime. Further, policies are generally a C++ concept, and apply only to a minority of other languages(for example D), while strategy pattern is available to many (most?) object oriented languages, and languages that treat functions as first class citizens like python.

That being said:

  • A policy, being determined at compile time, is generally only useful for special situations where you want different application logic on a per-binary basis. For instance you might develop software that is slightly customized for each customer, whether via a web interface, or by hand, this would be a policy-based pattern.

  • A strategy is determined at runtime, and in fact can be changed on the fly. For instance you might have software which implements a different user interface and logic for the salesforce than for the support group, but they all have to deal with the same customer and licensing info so rather than having two separately maintained apps you simply have one app whose interface changes as needed.

-Adam

like image 155
Adam Davis Avatar answered Oct 30 '22 02:10

Adam Davis


I thought they were the same thing.

like image 40
Bill the Lizard Avatar answered Oct 30 '22 01:10

Bill the Lizard