# Strategy design pattern vs partial

**URL:** <https://www.ruby-forum.com/t/strategy-design-pattern-vs-partial/129290>\
**Category:** Rails\
**Created:** [February 16, 2008, 5:26am UTC](https://www.ruby-forum.com/t/strategy-design-pattern-vs-partial/129290 "2008-02-16T05:26:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 16, 2008, 5:26am UTC](https://www.ruby-forum.com/t/strategy-design-pattern-vs-partial/129290/1 "2008-02-16T05:26:04Z")

</div>

I have a Rails application (something like an online catalog) that needs  
to  
behave differently for different types of clients. For example, boat  
dealers  
and car dealers.

- When the application displays a list of cars, it needs to list car  
attributes, like horsepower and gas milage.
- When the application displays a list of boats, in needs to list boat  
attributes, like LWL and sail area.

I would normally use the Strategy design pattern to implement these  
different types of behavior.

But with Rails, it seems much more natural to implement the different  
views  
of the data as partials, with the car-data view in one partial and the  
boat-data view in another partial. The application can then include the  
boat  
partial for boat dealers, and the car partial for car dealers. The  
application can be extended for other types of dealers simply by adding  
a  
new partial.

This solution separates out the stuff that changes (the view of the  
data)  
from the stuff that doesn’t (the stuff that creates the list of  
inventory  
‘items’), and keeps the car-view code separate from the boat-view code.

Furthermore, using a partial doesn’t seem hugely different from using a  
Proc  
object, which was the first Strategy solution I thought of.

In other words, using partials seems a more Rails way of implementing a  
Strategy-like solution, at least when viewing data. However, it’s not  
very  
OO, and I feel guilty using this easy ‘cheat’.

Has anyone else run into this issue? Your thoughts much appreciated.

Brgds: John

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 16, 2008, 8:42am UTC](https://www.ruby-forum.com/t/strategy-design-pattern-vs-partial/129290/2 "2008-02-16T08:42:52Z")

</div>

On Feb 15, 10:25 pm, Identry [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This solution separates out the stuff that changes (the view of the data)  
> from the stuff that doesn’t (the stuff that creates the list of inventory  
> ‘items’), and keeps the car-view code separate from the boat-view code.
> 
> Furthermore, using a partial doesn’t seem hugely different from using a Proc  
> object, which was the first Strategy solution I thought of.
> 
> In other words, using partials seems a more Rails way of implementing a  
> Strategy-like solution, at least when viewing data. However, it’s not very  
> OO, and I feel guilty using this easy ‘cheat’.

Why? Who are you cheating? Sounds like somewhere along the line you  
absorbed some OO dogma. Most OO dogma is about going to extra trouble  
today to save trouble tomorrow. Forget about it. Save tomorrow for  
tomorrow, think about today instead. Just write the code in the  
simplest possible way. If becomes awkward later, change it. Your  
functional tests will validate the change.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 16, 2008, 7:18pm UTC](https://www.ruby-forum.com/t/strategy-design-pattern-vs-partial/129290/3 "2008-02-16T19:18:12Z")

</div>

Agreed!

I am assuming that somewhere along the line you have some way of  
determining the dealer type (STI?). Why wouldn’t it be ‘OO’ to do  
something like:

render :partial=\>“show\_#{dealer\_type.underscore}”

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 16, 2008, 9:26pm UTC](https://www.ruby-forum.com/t/strategy-design-pattern-vs-partial/129290/4 "2008-02-16T21:26:02Z")

</div>

Well the OO-Rails-Way to solve this is to have a model for Cars and  
one for Boots. These Models would typically inherit from some vehicle-  
class if u have common attributes and functions.  
You can then display Boots and Cars in their Views and not in some  
other View where they don’t belong. Just redirect to the appropriate  
view for the your dealer

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [February 18, 2008, 1:01am UTC](https://www.ruby-forum.com/t/strategy-design-pattern-vs-partial/129290/5 "2008-02-18T01:01:58Z")

</div>

I don’t think there’s any confusion about the different models to use,  
the question is the best wasy to “display Boats and Cars in their  
Views…” Strategy Pattern answers that problem by giving a mechanism  
to perform that view selection.

On Feb 16, 3:25 pm, “removed\_email\_address@domain.invalid”
