11. Hey, I’m building a chunk
of code that is used to
build an object which has
a lot of dependencies and
setup and each of those
has a few of their own so
this piece of code will take
care of putting together all
those pieces so that when
I need the object I just
have to ask this one object
to give me the other one
and then I can use it where
ever I need instead of
having to build a huge
dependency tree before
using this code…
21. Another pattern description
• I’m going to create a shopping cart where I can
choose how the discounts or promo codes will be
applied by injecting various objects, each of which can
include how to apply these discounts; for example
apply discounts only to items that don’t have other
discounts, apply discount percentage on the base
price not counting already applied discounts, apply
discount percentage after other applied discounts,
apply a flat amount of a discount for each item, apply
a discount on an item based on what type of item it is,
etc
22. Strategy pattern
• I’m going to create a shopping cart where I can
choose how the discounts or promo codes will be
applied by injecting various objects, each of which can
include how to apply these discounts; for example
apply discounts only to items that don’t have other
discounts, apply discount percentage on the base
price not counting already applied discounts, apply
discount percentage after other applied discounts,
apply a flat amount of a discount for each item, apply
a discount on an item based on what type of item it is,
etc
23. Strategy pattern
• Allow injection of object that provides some aspect of
the functionality
• Algorithm’s functionality can be decided at run-time
• Examples:
• usort - Provide comparison part of sort
• Car - brakingStrategy
24. Command Pattern
• I’m doing a bunch of stuff that I want to be able to
keep track of and potentially save and replay later,
possibly be able to undo. In order for this to work my
object will have to capture everything it needs to know
about as far as parameters and what-not in order to be
able to run later as well as potentially capturing state
so I can get back to where I was before.
26. Chain of Responsibility
• Each object can take care of something (or not), then it
passes to the next object
27. Chain of Responsibility
Examples
• Loggers - set next logger in chain, each calls the next
when it is done
• Managers with purchasing power - if amount is above
manager’s level, pass to the next object in the chain
28. Observer Pattern
• Notify interested objects of state changes in subject
• Interested objects subscribe to subject
• When subject changes, it calls update on all observers
29. MVC - Model View Controller
• Organizational pattern for web dev
• View code contains output
• Model code is for data/business logic
• Controller links model and view code
30. Front Controller
• Central entry point for handling requests for web
application
• Route all calls through index.php, then figure out what
to do with it