4

I am curious to know if it is OK (Normal) to use Strategy pattern to change the behavior of a Builder object ?

This is an example. Lets say we have the following classes and I want to use the builder to create certain type of view model for my web page.

BuilderA

BuilderB

StrategyA

StrategyB

  1. We pass in Data1 and Data2 to BuilderA

  2. We pass in Data3 and Data4 to BuilderB

Lets say depending on Data that we passed in we want to create certain ViewModels using those strategies. StrategyA can make ViewModelA and StrategyB can make ViewModelB. I want to be able to modify those Strategies separately without changing my builders.

Please let me know if that is not clear and I will explain more.

Thanks in advance !

Raha
  • 1,959
  • 3
  • 19
  • 28
  • Normal is generally not my first consideration, but does it fit the circumstance. I don't want to commit to an answer YET, but if you could perhaps frame a simple example of what you are thinking about, I have some idea how I would answer. – Abstraction is everything. May 14 '17 at 22:16
  • @Abstractioniseverything. I updated my question with an example. Please let me know if that makes sense or not ;) – Raha May 15 '17 at 01:15

1 Answers1

2

Since Normal is the question, I would have to align categorically.

While there are no hard and fast rules, at least in my opinion there should not be, when it comes to the use of patterns, there is a convention based on motivations.

The Builder is a 'creational pattern', while Strategy is a 'behavioral pattern'. A couple of things to consider.

The Builders Motivation is to do precisely what you are describing as far as I can see from your example. However, I can see why you would be eyeing the Strategy, having prototypical(clue) objects which have some creational differences based on state. I can certainly see how this might work though it does a little violence to the intent of Strategy as a Pattern.

Have you considered using Prototype with Decorators?
One of the typical applications of the Prototype DP looks at objects differing in only a few
combinations of state.

  • 1
    I actually haven't tried Prototype and Decorators yet. I will take a look. The main reason why I asked this question was because I wanted to explore if there is a hard rule on whether if we can change the behavior of a builder pattern (or any other creational pattern) using any behavioral design patterns (in this case Strategy). In general I personally lean toward mixing different patterns as long as I can communicate my intention using those vocabulary. What are your thoughts on that ? – Raha May 15 '17 at 05:30
  • Yes, I can see how this would work, and I never discount creative ideas with any pattern blending, as long as conventional options are explored first. I do this all the time, though it doesn't always work out the way I first imagine. However, I never know until I try. Sometimes it does but it draws strange looks from the uncreative types. How else can we learn though, if were not willing to explore. If you do decide to try the Strategy, I would like to know how that turns out for you. – Abstraction is everything. May 15 '17 at 05:43
  • 1
    I actually did implement it and it worked pretty well for my scenario however one of my colleagues didn't enjoy this mix and I changed our design to use only a builder on its own. – Raha May 15 '17 at 15:23