93
www.xsolve.pl Agile Software House Design patterns

Xlab #2: wzorce projektowe

Embed Size (px)

Citation preview

Page 1: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Design patterns

Page 2: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Goals

Page 3: Xlab #2: wzorce projektowe

1. 10:00 Przywitanie

2. 10:30 Dobre praktyki programowania

3. 11:00 Wzorce projektowe

○ Creational design patterns

○ Structural patterns

○ Behavioral design patterns

4. 12:00 Przerwa na pizzę

5. 12:30 Code Kata - Supermarket

6. 14:00 Analiza rozwiązań

7. 14:30 Podsumowanie

na żywych przykładach wspólnie przygotujemy SOLIDny kod

xlab #2: Wzorce projektowe agenda

www.xsolve.plAgile Software House

Page 4: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Speakers

Page 5: Xlab #2: wzorce projektowe

PHP Software Developer

Tomasz Łysek

www.xsolve.plAgile Software House

Masterzu developerzu i architekturzu dla rozwiązań biznesowych od ok. 10 lat.Woli backend i DB niż elementy frontu. Pilnuje by studenci pisali porządny kod.W wolnym czasie gotuje, bije innych na treningach i relaksuje się przy zmywaniu naczyń.

"Wąż powiedział: choć ze mna pokażę Ci zalety JS-a. (...) Projekt osunął się powoli, jak pada drzewo. Team Leader stłumił nawet odgłos upadku moralnego programisty."Antoine de Saint-Exupéry

Page 6: Xlab #2: wzorce projektowe

Senior PHP Software Developer

Mateusz Rosiek

www.xsolve.plAgile Software House

Backend developer PHP od 5 lat. Aktualnie zajmuję się aplikacją wystawiającą API RESTowe dla systemu e-commerce.Prywatnie właściciel jeża.

“W nienawiści do JS tak zostałem wychowanyHardcor PHP, ogień PHP”~Paulo Coelho

Page 7: Xlab #2: wzorce projektowe

PHP Software Developer

Ion Bazan

www.xsolve.plAgile Software House

PHP Developer, w XSolve od 9 miesięcy. Aktualnie zajmuje się projektem platformy B2B/B2C dla branży oponiarskiej.

“They told me JS was fun but all I got was promises.” ~Paulo Coelho

Page 8: Xlab #2: wzorce projektowe

PHP Software Developer

Piotr Leśniowski

www.xsolve.plAgile Software House

Jestę developerę. Programuję w PHP i JS w celu zarobkowym od 5 lat a hobbystycznie od 8-ej klasy podstawówki. Romansowałem m. in. z C# i Pythonem, aczkolwiek przygody te nie zakończyły się zdrowym i długotrwałym związkiem. Gdy nie programuję, oddaję się słuchaniu muzyki, kolekcjonowaniu płyt, brawurowej jeździe samochodem marki Cinquecento oraz denerwowaniu się na marne, kinowe adaptacje gier. W młodości chciałem zostać śmieciarzem albo kierowcą rajdowym. Skończyło się na wyłapywaniu śmieci w oprogramowaniu oraz na prawku kat. B. Aktualnie jestem odpowiedzialny za projektowanie i utrzymywanie RESTowego API dla aplikacji SPA.

PS.Lubię placki... i przemyślany kod.

PSS.Czasami coś machnę w Angularze.

Page 9: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Your turn

Page 10: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Contract

Page 11: Xlab #2: wzorce projektowe

● We would like to write SOLID code, so:

○ We need to know and use best practices

○ We need to know and use basic design patterns

○ We need to write tests

● We would like to have fun, eat pizza and drink beers! :)

What we want to achieve

Contract

www.xsolve.plAgile Software House

Page 12: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

SOLID

Page 13: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

What is it?

Page 14: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

“[SOLID] is a mnemonic acronym introduced by Michael Feathers for the "first five principles" named by Robert C. Martin in the early 2000s that stands for five basic principles of object-oriented programming and design”

- Wikipedia

What is it?

Page 15: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Page 16: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

● Single responsibility principle

What is it?

Page 17: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

● Single responsibility principle

● Open/Closed principle

What is it?

Page 18: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

● Single responsibility principle

● Open/Closed principle

● Liskov substitution principle

What is it?

Page 19: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

● Single responsibility principle

● Open/Closed principle

● Liskov substitution principle

● Interface segregation principle

What is it?

Page 20: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

What is it?

● Single responsibility principle

● Open/Closed principle

● Liskov substitution principle

● Interface segregation principle

● Dependency inversion principle

Page 21: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Does my code stink?

Page 22: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Probably yes.

Page 23: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

How do I know if my code stinks?

Page 24: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

How do I know if my code stinks?

● Rigidity - design is hard or nearly impossible to change

● Fragility - design is easily breakable

● Solution is non-reusable

● Design makes it nearly impossible to do the right thing

Page 25: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Single responsibility principle

● If something is good for everything then it’s pretty much for nothing

● A class should only have one responsibility

● There shouldn’t be more than one reason for a class to change

● TL;DR - a class should focus on doing one thing and one thing only

Page 26: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Single responsibility principle

Page 27: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Single responsibility principle

Page 28: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Open/closed principle

● A class should be open for extending

● A class should be closed for changes

● TL;DR - when requirements change we should extend the existing code,

not change it

Page 29: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Open/closed principle

Page 30: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Open/closed principle

Page 31: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Liskov substitution principle

● A class implementing an abstraction should do it properly. In a way that

the code that is based on the abstraction should not be dependant on

concrete implementations, but it should allow for substituting different

implementations of formentioned abstraction

● TL;DR - Subclasses should behave properly when substituted for base

class

Page 32: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Liskov substitution principle

Page 33: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Liskov substitution principle

Page 34: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Interface segregation principle

● Many client-specific interfaces are better than one general-purpose

interface

● A class should not be dependant on methods that it doesn’t use

● TL;DR - Avoid fat interfaces

Page 35: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Interface segregation principle

Page 36: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Interface segregation principle

Page 37: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Dependency inversion principle

● A base class should not be dependant on subclass, both should be

dependant on abstraction

● TL;DR - Use interfaces and abstractions

Page 38: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Dependency inversion principle

Page 39: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Law of Demeter

Page 40: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Law of Demeter

● A principle of least knowledge

● A method of an object can call another method of:

○ The same object

○ Any parameter passed to it

○ Any object created by it

○ Direct component of an object to which it belongs

● TL;DR - Don’t talk to strangers

Page 41: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Design Patterns

Page 42: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Creational Design Patterns

Page 43: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Creational design patterns

● They are all about class instantiation

● They can be divided into class and object-creational patterns

● They encapsulate using new keyword to create objects

Page 44: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Can’t we just use constructor?

Page 45: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

No.

Page 46: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Abstract Factory

Provide a level of indirection that abstracts the creation of families of related or

dependent objects without directly specifying their concrete classes. The

"factory" object has the responsibility for providing creation services for the

entire platform family. Clients never create platform objects directly, they ask

the factory to do that for them.

Page 47: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Abstract Factory

Page 48: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Abstract Factory

● AbstractPizzaFactory provides a Factory Method for Pizza object.

● CapricciosaPizzaFactory and ProsciuttoPizzaFactory are concrete Pizza

factories extending/implementing AbstractPizzaFactory.

● Client does not have idea which concrete object the factory returns

Page 49: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Abstract Factory

Page 50: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Factory Method

Factory Method is to creating objects as Template Method is to implementing

an algorithm. A superclass specifies all standard and generic behavior (using

pure virtual "placeholders" for creation steps), and then delegates the creation

details to subclasses that are supplied by the client.

Page 51: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Factory Method

Page 52: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Factory Method

● Evaluates arguments and decides which derived object to create and

return

● Defines an interface for creating objects, but lets subclasses decide which

classes to instantiate

Page 53: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Factory Method

Page 54: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Builder

The Builder pattern separates the construction of a complex object from its

representation so that the same construction process can create different

representations. The focus/distinction is on creating complex aggregates.

Page 55: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Builder

Page 56: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Builder

● Separate the construction of a complex object from its representation so that the same construction process can create different representations.

● Parse a complex representation, create one of several targets.

Page 57: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Builder

Page 58: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Builder

Page 59: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Test results

Page 60: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Structural Design Patterns

Page 61: Xlab #2: wzorce projektowe

Private Data Class● The private class data design pattern seeks to reduce exposure of attributes by

limiting their visibility. It reduces the number of class attributes by encapsulating them in single Data object. It allows the class designer to remove write privilege of attributes that are intended to be set only during construction, even from methods of the target class.

Source: wikipedia

● Separates data from business logic.● Encapsulates class data within two objects.● Can be combined with Value Object pattern.

TL;DR: Get fields from your class and put them in separate (new) class.

Page 62: Xlab #2: wzorce projektowe

Flyweight● A flyweight is an object that minimizes memory use by sharing as much data as

possible with other similar objects.Source: wikipedia

● Minimal structure.● Simple interface.● Easy to implement.● Should implement Private Data Class or at least Value Object.

TL;DR: make multiple small classes from your big data business logic class (get the core and put it into different class).

Page 63: Xlab #2: wzorce projektowe

Flyweight

Page 64: Xlab #2: wzorce projektowe

Flyweight

Page 65: Xlab #2: wzorce projektowe

Facade● A facade is an object that provides a simplified interface to a larger body of code,

such as a class library. The Facade design pattern is often used when a system is very complex or difficult to understand because the system has a large number of interdependent classes or its source code is unavailable. This pattern hides the complexities of the larger system and provides a simpler interface to the client.

Source: wikipedia

● Simplify interface of complex class/classes.● Provide new interface for class/classes.

TL;DR: If you have very complex class or subsystem just do one class facade for simply to use interface.

Page 66: Xlab #2: wzorce projektowe

Facade

Page 67: Xlab #2: wzorce projektowe

Facade

Page 68: Xlab #2: wzorce projektowe

Adapter● Allows the interface of an existing class to be used as another interface. An adapter

helps two incompatible interfaces to work together. This is the real world definition for an adapter. Interfaces may be incompatible but the inner functionality should suit the need. The Adapter design pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients.

Source: wikipedia

● Create brand new interface for an existing class.● Adapt system interface to clients needs.● Best way to create new system with old system interface.

TL;DR: When there is a strong need to adapt created interface to new requirements - make new class with needed methods and run in them the old one’s

Page 69: Xlab #2: wzorce projektowe

AdapterFacade vs Adapter:● Make new interface for existing one,● Adapter uses already created interface,● Facade create new interface from the scratch.

Adapter vs Bridge:● If you use aggregation then make it in abstract and create bridge but if you use

inheritance then make adapter.

Page 70: Xlab #2: wzorce projektowe

Adapter

Page 71: Xlab #2: wzorce projektowe

Proxy● A proxy, in its most general form, is a class functioning as an interface to something

else. The proxy could interface to anything: a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate. In short, a proxy is a wrapper or agent object that is being called by the client to access the real serving object behind the scenes.

Source: wikipedia

● Protects complex and sensitive objects from undesirable calling.● Additional level for control data flow.● Complex Proxy can be use as Service Stub.

TL;DR: If you need to control flow through part of system or service - add class between that part that will be mocking functionality.

Page 72: Xlab #2: wzorce projektowe

Proxy

Page 73: Xlab #2: wzorce projektowe

Decorator● Allows behavior to be added to an individual object, either statically or dynamically,

without affecting the behavior of other objects from the same class.[1] The decorator pattern is often useful for adhering to the Single Responsibility Principle, as it allows functionality to be divided between classes with unique areas of concern.

Source: wikipedia● Adding new functionality to already existing classes without changing code of that

classes.● Recursive wrapping core objects with new functionalities.● Strong SOLID.

TL;DR: If you want to add new features to already existing class - don’t change code of that class but add new class and pass core class instance to it.

Page 74: Xlab #2: wzorce projektowe

Decorator● Proxy vs Decorator: Proxy creates instance of class within own object, Decorator

gets the class instance in constructor.

Page 75: Xlab #2: wzorce projektowe

Composite● Describes that a group of objects is to be treated in the same way as a single

instance of an object. The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies. Implementing the composite pattern lets clients treat individual objects and compositions uniformly

Source: wikipedia● Manage pool of objects with one objects with same interface.● Creates tree structure.● Solid hierarchy structure in code.● DP Composite ≠ UML Composite

TL;DR: Use it when you need to create collection of one class objects.

Page 76: Xlab #2: wzorce projektowe

Composite

Page 77: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Behavioral Design Patterns

Page 78: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Strategy

In computer programming, the strategy pattern (also known as the policy pattern) is a software design pattern that enables an algorithm's behavior to be selected at runtime. The strategy pattern● defines a family of algorithms,● encapsulates each algorithm, and● makes the algorithms interchangeable within that family.

source: wikipedia.com

Page 79: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Strategy

https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/Strategy

Page 80: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Strategy

https://sourcemaking.com/design_patterns/strategy

Page 81: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Strategy

https://sourcemaking.com/design_patterns/strategy

Page 82: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Iterator

In object-oriented programming, the iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements. The iterator pattern decouples algorithms from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled.

source: wikipedia.com

Page 83: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Iterator

https://sourcemaking.com/design_patterns/iterator

Page 84: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Iterator

https://sourcemaking.com/design_patterns/iterator

Page 85: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Mediator

In software engineering, the mediator pattern defines an object that encapsulates how a set of objects interact.With the mediator pattern, communication between objects is encapsulated with a mediator object. Objects no longer communicate directly with each other, but instead communicate through the mediator. This reduces the dependencies between communicating objects, thereby lowering the coupling.

source: wikipedia.com

Page 86: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Mediator

Page 87: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Mediator

Page 88: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Mediator

Page 89: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Mediator

Page 90: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Recapitulation

Page 91: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Recapitulation

● What does Solid mean?

● What does Demeter law mean?

● How to work with creational design patterns?

● How to work with structural design patterns?

● How to work with behavioral design patterns?

Page 92: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

Recapitulation

Page 93: Xlab #2: wzorce projektowe

www.xsolve.plAgile Software House

xsolve.pl/career

Thank you for your time!