design pattern concrete
implementation
“A software design pattern is a general, reusable solution to a
commonly occurring problem within a given context in software
design.”
Photo by rawpixel on Unsplash
Bad design (Robert Martin)
•Rigidity
•Fragility
•Immobility
Bad design (Robert Martin)
•Rigidity
•Fragility
•Immobility
Bad design (Robert Martin)
•Rigidity
•Fragility
•Immobility
Bad design example
Solution
• Creational patterns
• (singleton, factory method, dependency injection, …)
Solution
• Creational patterns
• (singleton, factory method, dependency injection, …)
• Structural pattern
• (adapter, decorator, façade, …)
Solution
• Creational patterns
• (singleton, factory method, dependency injection, …)
• Structural pattern
• (adapter, decorator, façade, …)
• Behavioral pattern
• (chain of responsibility, command, observer, template, strategy, …)
About the search project
Purpose:
• External fee
• Possible interchangeable sources
• Same output to the end user
Transform this
Souce 1 Source 2
Into this
Souce 1 Source 2
Implementation
1. No transformation from
source to destination
(sending to output directly
Bing json)
2. One class that will deal with the transformation (something like
fetchSearchResoults)
• 3. Decouple fetching the search stream from transforming it into a
readable solution for the output (separation of concerns … )
Factory
Image from: https://www.tutorialspoint.com/design_pattern/factory_pattern.htm
The factory class
Fetching Bing search feed
Adapter
Image from: https://www.tutorialspoint.com/design_pattern/adapter_pattern.htm
Adapter
Decorator
The Decorator
Making code consistent
Use an interface that convey all required functionality of the end object
Conclusions
• Rigidity
• Fragility
• Immobility
Expandable=> not rigid
Expandable=> not rigid
Flexible=> not fragile
Use for other purposes => not immobile
Observations => it can be seen as an anti-corruption layer
Image from: https://docs.microsoft.com/en-us/azure/architecture/patterns/anti-corruption-layer
References
• Design Patterns - Elements of Reusable Object-Oriented Software by
Erich Gamma, John Vlissides, Ralph Johnson, and Richard Helm
• https://en.wikipedia.org/wiki/Software_design_pattern
• http://www.oodesign.com/
• https://sourcemaking.com/design_patterns
• http://www.blackwasp.co.uk/gofpatterns.aspx
• https://www.tutorialspoint.com/design_pattern
• https://docs.microsoft.com/en-us/azure/architecture/patterns/anti-corruption-layer

Design pattern concrete implementation

Editor's Notes

  • #3 Oop design principles represent a set of guidelines Assist us in avoiding bad design How do I see design patterns role: have the problem, divide it (more like a jigsaw puzzle) and fit each design pattern into its own place
  • #4 according to Robert Martin there are 3 important characteristics of a bad design that should be avoided Rigidity - It is hard to change because every change affects too many other parts of the system. Fragility - When you make a change, unexpected parts of the system break. Immobility - It is hard to reuse in another application because it cannot be disentangled from the current application.
  • #5 according to Robert Martin there are 3 important characteristics of a bad design that should be avoided Rigidity - It is hard to change because every change affects too many other parts of the system. Fragility - When you make a change, unexpected parts of the system break. Immobility - It is hard to reuse in another application because it cannot be disentangled from the current application.
  • #6 according to Robert Martin there are 3 important characteristics of a bad design that should be avoided Rigidity - It is hard to change because every change affects too many other parts of the system. Fragility - When you make a change, unexpected parts of the system break. Immobility - It is hard to reuse in another application because it cannot be disentangled from the current application.
  • #7 One search function that would aggregate and search for different campaigns. Function is being used in many parts of the application serving different purposes Function would accept various length of parameters (like: startDate, endDate, includeEventCampaigns, eventCampaigns, nonEventCampaigns, ….) Is rigid; would have 500 to 1000 lines, any small change would have big impact If fragile: first page campaign tiles (that are not in a search) are pulled from this function Immobile: hard to use it again, let’s say search for approved/unapproved event campaigns
  • #8 Creational patterns - the creational patterns aim to separate a system from how its objects are created, (composed, and represented). - they increase the system's flexibility Singleton – can have only one object Structural pattern - defining a consistent way of modelling relations between different application components Façade – unified interface to a set of interfaces “Facade defines a new interface, whereas Adapter reuses an old interface” Behavioral pattern - Deals with responsibilities between objects. chain of responsibility - send a command without knowing what object will receive and handle it Command Observer - a subject has to be observed by one or more observers
  • #9 Creational patterns - the creational patterns aim to separate a system from how its objects are created, (composed, and represented). - they increase the system's flexibility Singleton – can have only one object Structural pattern - defining a consistent way of modelling relations between different application components Façade – unified interface to a set of interfaces “Facade defines a new interface, whereas Adapter reuses an old interface” Behavioral pattern - Deals with responsibilities between objects. chain of responsibility - send a command without knowing what object will receive and handle it Command Observer - a subject has to be observed by one or more observers
  • #10 Creational patterns - the creational patterns aim to separate a system from how its objects are created, (composed, and represented). - they increase the system's flexibility Singleton – can have only one object Structural pattern - defining a consistent way of modelling relations between different application components Façade – unified interface to a set of interfaces “Facade defines a new interface, whereas Adapter reuses an old interface” Behavioral pattern - Deals with responsibilities between objects. chain of responsibility - send a command without knowing what object will receive and handle it Command Observer - a subject has to be observed by one or more observers
  • #11 How do I deal with the search (use external source) How that would be presented to the user (same output) Need to interchange sources due to high changes in their policy and how they are being offered
  • #12 In the initial feed we have more pieces of information than we need Structure of it might be inappropriate for our app Different naming for items that we already have
  • #13 In the initial feed we have more pieces of information than we need Structure of it might be inappropriate for our app Different naming for items that we already have
  • #14 (Explore this option and why this is not a good option) Highly coupled with an exterior factor => highly dependent on the external source Hard to support multiple versions or to transit from one version to the next Hard to add and support a new independent source
  • #15 (Explore this option and why this is not a good option) Hard to support different versions of external feed Adding new sources complicates thinks High coupling Potential for creating a big/ugly and hard to read class All processes of transformation will become more and more complex over time
  • #16 Possibilities of having multiple sources Possibilities of easily supporting multiple versions of the external feed Low impact over the application if external feed fails Clear and readable classes Easy to maintain and update
  • #18 This class creates and returns required object. In order to further enforce consistency a certain class type is required as of returned object.
  • #19 BingSearchV5 class does: Set search options connect to external api Fetch the feed and return it Does not Parse the result into a meaningful json for app Add any extra pieces of information on top of it
  • #20 Problems with this design: Each source has different formats
  • #22 BingSearchV5Adapter does Take BingSearchV5 result and pares it component by component, element by element Transform each into something readable for the application BingSearchV5Adapter does not Add any extra content
  • #27 Advantages Enforce that all objected created by this factory have similar structure This enable the segregation of factory object from the rest of the application