Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Xlab #2: wzorce projektowe

395 views

Published on

Podczas drugiego XSolve Laboratory (xLab), wspólnie z doświadczonymi programistami XSolve, poznacie dobre praktyki programowania.

Celem warsztatu będzie przejście przez wzorce projektowe i architektoniczne oraz narzędzia wspomagające wytwarzanie oprogramowania odpornego na zmiany.

Poznacie realne problemy z prowadzonych przez nas projektów i na ich przykładach wspólnie przygotujemy SOLIDny kod, działający zgodnie z przyjętymi założeniami, przedstawionymi jako przypadki testowe.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Xlab #2: wzorce projektowe

  1. 1. www.xsolve.plAgile Software House Design patterns
  2. 2. www.xsolve.plAgile Software House Goals
  3. 3. 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
  4. 4. www.xsolve.plAgile Software House Speakers
  5. 5. 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
  6. 6. 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 wychowany Hardcor PHP, ogień PHP” ~Paulo Coelho
  7. 7. 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
  8. 8. 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.
  9. 9. www.xsolve.plAgile Software House Your turn
  10. 10. www.xsolve.plAgile Software House Contract
  11. 11. ● 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
  12. 12. www.xsolve.plAgile Software House SOLID
  13. 13. www.xsolve.plAgile Software House What is it?
  14. 14. 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?
  15. 15. www.xsolve.plAgile Software House
  16. 16. www.xsolve.plAgile Software House ● Single responsibility principle What is it?
  17. 17. www.xsolve.plAgile Software House ● Single responsibility principle ● Open/Closed principle What is it?
  18. 18. www.xsolve.plAgile Software House ● Single responsibility principle ● Open/Closed principle ● Liskov substitution principle What is it?
  19. 19. www.xsolve.plAgile Software House ● Single responsibility principle ● Open/Closed principle ● Liskov substitution principle ● Interface segregation principle What is it?
  20. 20. www.xsolve.plAgile Software House What is it? ● Single responsibility principle ● Open/Closed principle ● Liskov substitution principle ● Interface segregation principle ● Dependency inversion principle
  21. 21. www.xsolve.plAgile Software House Does my code stink?
  22. 22. www.xsolve.plAgile Software House Probably yes.
  23. 23. www.xsolve.plAgile Software House How do I know if my code stinks?
  24. 24. 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
  25. 25. 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
  26. 26. www.xsolve.plAgile Software House Single responsibility principle
  27. 27. www.xsolve.plAgile Software House Single responsibility principle
  28. 28. 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
  29. 29. www.xsolve.plAgile Software House Open/closed principle
  30. 30. www.xsolve.plAgile Software House Open/closed principle
  31. 31. 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
  32. 32. www.xsolve.plAgile Software House Liskov substitution principle
  33. 33. www.xsolve.plAgile Software House Liskov substitution principle
  34. 34. 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
  35. 35. www.xsolve.plAgile Software House Interface segregation principle
  36. 36. www.xsolve.plAgile Software House Interface segregation principle
  37. 37. 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
  38. 38. www.xsolve.plAgile Software House Dependency inversion principle
  39. 39. www.xsolve.plAgile Software House Law of Demeter
  40. 40. 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
  41. 41. www.xsolve.plAgile Software House Design Patterns
  42. 42. www.xsolve.plAgile Software House Creational Design Patterns
  43. 43. 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
  44. 44. www.xsolve.plAgile Software House Can’t we just use constructor?
  45. 45. www.xsolve.plAgile Software House No.
  46. 46. 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.
  47. 47. www.xsolve.plAgile Software House Abstract Factory
  48. 48. 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
  49. 49. www.xsolve.plAgile Software House Abstract Factory
  50. 50. 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.
  51. 51. www.xsolve.plAgile Software House Factory Method
  52. 52. 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
  53. 53. www.xsolve.plAgile Software House Factory Method
  54. 54. 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.
  55. 55. www.xsolve.plAgile Software House Builder
  56. 56. 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.
  57. 57. www.xsolve.plAgile Software House Builder
  58. 58. www.xsolve.plAgile Software House Builder
  59. 59. www.xsolve.plAgile Software House Test results
  60. 60. www.xsolve.plAgile Software House Structural Design Patterns
  61. 61. 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.
  62. 62. 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).
  63. 63. Flyweight
  64. 64. Flyweight
  65. 65. 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.
  66. 66. Facade
  67. 67. Facade
  68. 68. 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
  69. 69. Adapter Facade 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.
  70. 70. Adapter
  71. 71. 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.
  72. 72. Proxy
  73. 73. 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.
  74. 74. Decorator ● Proxy vs Decorator: Proxy creates instance of class within own object, Decorator gets the class instance in constructor.
  75. 75. 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.
  76. 76. Composite
  77. 77. www.xsolve.plAgile Software House Behavioral Design Patterns
  78. 78. 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
  79. 79. www.xsolve.plAgile Software House Strategy https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/Strategy
  80. 80. www.xsolve.plAgile Software House Strategy https://sourcemaking.com/design_patterns/strategy
  81. 81. www.xsolve.plAgile Software House Strategy https://sourcemaking.com/design_patterns/strategy
  82. 82. 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
  83. 83. www.xsolve.plAgile Software House Iterator https://sourcemaking.com/design_patterns/iterator
  84. 84. www.xsolve.plAgile Software House Iterator https://sourcemaking.com/design_patterns/iterator
  85. 85. 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
  86. 86. www.xsolve.plAgile Software House Mediator
  87. 87. www.xsolve.plAgile Software House Mediator
  88. 88. www.xsolve.plAgile Software House Mediator
  89. 89. www.xsolve.plAgile Software House Mediator
  90. 90. www.xsolve.plAgile Software House Recapitulation
  91. 91. 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?
  92. 92. www.xsolve.plAgile Software House Recapitulation
  93. 93. www.xsolve.plAgile Software House xsolve.pl/career Thank you for your time!

×