Why is this important and why you should use them? The answer is so simple – Design patterns will make your life easier and allow you to write better code faster.
Oleg will talk briefly about usage of design patterns in PHP with emphasis on implementation in Symfony framework.
Gooding evening everyone. My name is Oleg.
Today I’ll tell you about Design Patterns and examples from Symfony Framework.
But before we start I’d like say few words about me and our company.
I’m coding for about 8 years
I started my career at Intexsys in 2012, and for the last three years I’m Software Architects in my team.
I’m working with Symfony since 2.0.
Mostly I specialize in e-commerce.
We at Intexsys are working on developing e-commerce and business automation systems.
On this slide you can see some of our clients.
One of important client is ecentria group, which operates with around ~16 stores, and larges store sell over 1mil products, and companies annual turnover is more then $300mil
We are working with more then 100 developers, and almost 50 of developers are located here in Riga, and you’ll be able to meat some of them later tonight at our afterparty.
How uses PHP? How uses Symfony?
For those who does not work with PHP, or Symfony.
READ SLIDE
Since when I started working with Symfony it has evolved a lot, they developed Symfony Flex which is a composer plugin which helps developers to start Symfony in micro-style, they’ve developed Auto-wiring for Dependency Container, so you do not need to spend time on writing YAML and XML.
Who does not know what Design Patterns are? [pause]
The father of the pattern language movement Christopher Alexander wrote: READ SLIDE
To keep it simple Design Patterns are well-tested solutions for common problems.
Design patterns are not a silver bullet to all your problems.
Do not try to force them
Bad things may happen, if you do so.
and keep in mind that design patterns are solutions to problems, not solutions finding problems.
They help you get design ”right” faster
They can speed up development process, because they provider clear picture on how you are implementing the design
They imply code reuse, therefore better software design, better maintainability of your code
They provider common language same for programmers.
Today I’ll cover three main classifications, Behavioral, Creational, Structural, that can be applied to any language, but there are other classification like Security patterns, Concurrency patterns, Real Time patterns.
READ SLIDE
The main idea behind Chain of Responsibility, is to avoid coupling sender of a request to its receiver by giving more than one object a chance to handle the request.
more than one object may handle a request, and the handler isn’t known a beforehand.
You want to execute handlers in a specific order
I’ll be showing you “pseudocode” on how logic could be implemented without COR, and then I’ll showing example from Symfony Framework with implemented pattern.
Who can answer what's wrong with this code? [pause]
SRP – it’s doing everything
OCP – you’ll have to modify it to add new behavior
it’s will be hard to test and maintain such code, because before covering handleAccess() you’ll need to cover all methods before.
Such code is not flexible, because you cannot easily inject in handle
Define a family of algorithms, encapsulate each one, and make them interchangeable.
it's not uml
many related classes differ only in their behavior.
you need different variants of an algorithm.
an algorithm uses data that clients shouldn’t know about.
a class defines many behaviors, and these appear as multiple conditional statements in its operations
Who can answer what's wrong with this code? [pause]
DIP (Dependency inversion principle)
SRP
OCP
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
When a change to one object requires changing others, and you don’t know how many objects need to be changed.
When an object should be able to notify other objects without making assumptions about who these objects are.
Who can answer what's wrong with this code? [pause]
OCP
SRP
DIP (if you extract methods into separate classes)
Separate the construction of a complex object from its representation so that the same construction process can create different representations.
the algorithm for creating a complex object should be independent of the parts that make up the object and how they’re assembled.
the construction process must allow different representations for the object that’s constructed.
Who can answer what's wrong with this code? [pause]
OPC
DIP
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
a class can’t anticipate the class of objects it must create.
a class wants its subclasses to specify the objects it creates.
Who can answer what's wrong with this code? [pause]
SRP
OCP
DIP
Ensure a class only has one instance, and provide a global point of access to it.
Unlike global variables, Singleton guarantees that there is just one class' instance. No one, except Singleton itself, can replace the cached instance.
there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point.
For example, a single database object, shared by different parts of the program.
when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.
Who can answer what's wrong with this code? [pause]
Global state makes program state unpredictable, no control over global variable
If your code has an external dependency that isn't explicitly introduced into the code then whoever gets the job of maintaining your code will have to go looking for it to figure out where it came from.
structural patterns which focuses on building relations between classes
Attach additional responsibilities to an object dynamically.
Decorators provide a flexible alternative to subclassing for extending functionality.
to add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects.
for responsibilities that can be withdrawn.
when extension by subclassing is impractical. Sometimes a large number of independent extensions are possible and would produce an explosion of subclasses to support every combination. Or a class definition may be hidden or otherwise unavailable for subclassing.
Who can answer what's wrong with this code? [pause]
Why bad:
Losing flexibility
Coupling
Making fragile base-class
Provide a surrogate or placeholder for another object to control access to it.
Who can answer what's wrong with this code? [pause]
DataCollector is something that collects information in development environment for toolbar
Why bad:
Losing flexibility
Coupling
Making fragile base-class
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.
you want to use an existing class, and its interface does not match the one you need.
you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don’t necessarily have compatible interfaces. (object adapter only)
you need to use several existing subclasses, but it’s impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.
Who can answer what's wrong with this code? [pause]
DIP
Code Coupled