Tutorials

Design Patterns - Strategy and Bridge

by John Kleijn on Oct 9, 2008 9:51:16 AM

Introduction

The Strategy and Bridge patterns provide solutions to apply polymorphism in more flexible way than you can accomplish with only inheritance.

The patterns are almost identical, differing mostly in intent only. Though it is this difference that create implementation variations targeting the respective problems.

Problem

Strategy
How to use different algorithms in a flexible, polymorphic way.

Bridge
How to structurally abstract a varying concept and it's implementations, in a way that doesn't break encapsulation.

Comments

True true

1. S N on Nov 29, 2008 11:27:54 PM

Good stuff.

2. Josh Robison on Dec 2, 2008 8:35:11 PM

thank you..

3. Nous on Dec 5, 2008 1:18:30 AM

Interesting example, tho IMO overkill for an 'emailer' class. Explains the concept well though.

4. Nabeel on Jan 2, 2009 7:02:45 PM

Thank you. Very nice tutorial.

5. boha on Jan 25, 2009 1:18:44 PM

Thank you. Good job.

In Bridge Pattern Demo, the class 'Mail' lacks a attribute (or field, member),
private $_from;

6. huyi on Feb 9, 2009 12:21:28 AM

everything's good except when I was reading the code I noticed a small problem...

# /**
# * Get the sender address
# *
# * @return string
# */
# public function getFrom()
# {
# return $this->_from;
# }
#
# /**
# * Set the sender address
# *
# * @param string $address
# */
# public function setFrom($address)
# {
# return $this->_from;
# }

get and set are the same lol, they both return the sender...

second one should be

# public function setFrom($address)
# {
# $this->_from = $address;
# }

;) Not that it matters since this is just an example to teach this concept, but it did have me confused for a split second until I realized it was just a small mistake

Good read!

7. P-H-STEVE on Jun 9, 2009 11:20:36 PM
Login or register to post a comment.