0
Separation of Concerns
Separation of concerns About Joshua  Joshua Thijssen, NoxLogic / Techademy  Freelance Consultant, Developer, Trainer  D...
Separation of concerns About Stephan  Stephan Hochdörfer, bitExpert AG  Department Manager Research Labs  enjoying PHP ...
Separation of concerns It is what I sometimes have called the separation of concerns, which [...] is   yet the only availa...
Separation of concerns   SoC […] is the process of breaking a     computer program into distinct   features that overlap i...
Separation of concerns It`s all about focusing!
Separation of concerns One step a time. Focus on the details!
Separation of concerns<?php$items = array();$token = file_get_contents("https://graph.facebook.com/oauth/access_token?..."...
Separation of concerns<?php                                                    Controller part!$items = array();$token = f...
Separation of concerns<?php$items = array();$token = file_get_contents("https://graph.facebook.com/oauth/access_token?..."...
Separation of concerns Very unstable. Not safe for changes....
Separation of concerns „Make everything as simple as possible...“
Separation of concerns Establish boundaries
Separation of concerns What are the boundaries?                   Presentation Layer
Separation of concerns What are the boundaries?                   Presentation Layer                         Business Layer
Separation of concerns What are the boundaries?                   Presentation Layer                         Business Laye...
Separation of concerns Isolate functionality, break it apart!
Separation of concerns<?phpnamespace AcmeDemoBundleController;use SymfonyBundleFrameworkBundleControllerController;use Sen...
Separation of concerns    $feed = file_get_contents("http://search.twitter.com/search.json?...");    $feed = json_decode($...
Separation of concerns Focus on one responsibility a time!
Separation of concerns<?phpnamespace AcmeDemoBundleController;use SymfonyBundleFrameworkBundleControllerController;use Sen...
Separation of concerns                                                 Twitter connector    $feed = file_get_contents("htt...
Separation of concerns Interfaces as a contract
Separation of concerns<?phpinterface ConnectorInterface {  /**   * Will return an array of the latest posts.   * return ar...
Separation of concerns<?phpclass FacebookConnector implements ConnectorInterface {  protected $handle;    public function ...
Separation of concerns<?phpclass FacebookConnector implements ConnectorInterface {  protected $handle;    public function ...
Separation of concerns<?phpclass FacebookConnectorV2 extends FacebookConnector {    protected function getAccessToken() { ...
Separation of concerns<?phpnamespace AcmeDemoBundleController;use SymfonyBundleFrameworkBundleControllerController;use Sen...
Separation of concerns<?phpnamespace AcmeDemoBundleController;use SymfonyBundleFrameworkBundleControllerController;use Sen...
Separation of concerns Will improve the testability!
Separation of concerns Will reduce tight coupling!
Separation of concerns Will increase component reuse!
Separation of concerns The value of separation                Why should I do it?
Separation of concerns The value of separation      1. Getting rid of code duplication
Separation of concerns The value of separation   2. Application becomes more stable        and easier to understand
Separation of concerns The value of separation    3. Single responsibility offers clear              extension points
Separation of concerns The value of separation        4. Increases the reusability of                 components
Separation of concerns How to separate the concerns?
Separation of concerns How to separate? Horizontal Separation                   Presentation Layer                        ...
Separation of concerns How to separate? Horizontal Separation ./symfony    |-app    |---cache    |---config    |---Resourc...
Separation of concerns How to separate? Service Separation                 Service Interface Layer                        ...
Separation of concerns How to separate? Service Separation                Frontend / UI Webservers            REST API    ...
Separation of concerns How to separate? Vertical Separation        Module A         Module B   Module C
Separation of concerns How to separate? Vertical Separation ./symfony    |-app    |---cache    |---config    |---Resources...
Separation of concerns How to separate? Vertical Separation       Presentation      Presentation   Presentation          L...
Separation of concerns How to separate? Vertical Separation ./symfony    |-app    |---cache    |---config    |---Resources...
Separation of concerns Cross-cutting concerns?     How to deal with security or logging                  aspects?
Separation of concerns How to separate? Aspect Separation Aspects                         Presentation Layer              ...
Separation of concerns How to separate? Aspect Separation <?php namespace AcmeProductsAspects; /**  * @FLOW3Aspect  */ cla...
Separation of concerns Dependency Direction
Separation of concerns Dependency Direction        "High-level modules should not         depend on low-level modules.    ...
Separation of concerns Inverting Concerns       Presentation          Layer        Business         Layer        Resource ...
Separation of concerns Inverting Concerns       Presentation      UI     Presentation          Layer       Component    La...
Separation of concerns Inverting Concerns      The goal of Dependency Injection        is to separate the concerns of     ...
Separation of concerns What are the benefits? Let`s recap....
Separation of concerns What are the benefits?             1. Facilitate reusability
Separation of concerns What are the benefits?        2. Ensure the maintainability
Separation of concerns What are the benefits?        3. Increasing the code quality
Separation of concerns What are the benefits?   4. Enables everyone to understand             the application
Separation of concerns What are the benefits?        5. Allows developers to work         on components in isolation
Thank you!
http://joind.in/6244
Upcoming SlideShare
Loading in...5
×

Separation of concerns - DPC12

1,641

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,641
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
15
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Separation of concerns - DPC12"

  1. 1. Separation of Concerns
  2. 2. Separation of concerns About Joshua  Joshua Thijssen, NoxLogic / Techademy  Freelance Consultant, Developer, Trainer  Development in PHP, Python, Perl, C, Java  jthijssen@noxlogic.nl  @jaytaph
  3. 3. Separation of concerns About Stephan  Stephan Hochdörfer, bitExpert AG  Department Manager Research Labs  enjoying PHP since 1999  S.Hochdoerfer@bitExpert.de  @shochdoerfer
  4. 4. Separation of concerns It is what I sometimes have called the separation of concerns, which [...] is yet the only available technique for effective ordering of ones thoughts. Edsger W. Dijkstra, "On the role of scientific thought" (1974)
  5. 5. Separation of concerns SoC […] is the process of breaking a computer program into distinct features that overlap in functionality as little as possible. http://en.wikipedia.org/wiki/Separation_of_concerns
  6. 6. Separation of concerns It`s all about focusing!
  7. 7. Separation of concerns One step a time. Focus on the details!
  8. 8. Separation of concerns<?php$items = array();$token = file_get_contents("https://graph.facebook.com/oauth/access_token?...");$feed = file_get_contents("https://graph.facebook.com/ident/feed?".$token);$feed = json_decode($feed, true);foreach($feed[data] as $post) { if(123456789012 === $post[from][id]) { $items[] = array( date => strtotime($post[created_time]), message => $post[message]); }}$feed = file_get_contents("http://search.twitter.com/search.json?q=from:ident");$feed = json_decode($feed, true);foreach($feed[results] as $tweet) { $items[] = array( date => strtotime($tweet[created_at]), message => $tweet[text]);}foreach($items as $item) { echo $item[message];}
  9. 9. Separation of concerns<?php Controller part!$items = array();$token = file_get_contents("https://graph.facebook.com/oauth/access_token?...");$feed = file_get_contents("https://graph.facebook.com/ident/feed?".$token);$feed = json_decode($feed, true);foreach($feed[data] as $post) { if(123456789012 === $post[from][id]) { $items[] = array( date => strtotime($post[created_time]), message => $post[message]); }}$feed = file_get_contents("http://search.twitter.com/search.json?q=from:ident");$feed = json_decode($feed, true);foreach($feed[results] as $tweet) { $items[] = array( date => strtotime($tweet[created_at]), message => $tweet[text]);}foreach($items as $item) { echo $item[message];}}
  10. 10. Separation of concerns<?php$items = array();$token = file_get_contents("https://graph.facebook.com/oauth/access_token?...");$feed = file_get_contents("https://graph.facebook.com/ident/feed?".$token);$feed = json_decode($feed, true);foreach($feed[data] as $post) { if(123456789012 === $post[from][id]) { $items[] = array( date => strtotime($post[created_time]), message => $post[message]); }}$feed = file_get_contents("http://search.twitter.com/search.json?q=from:ident");$feed = json_decode($feed, true);foreach($feed[results] as $tweet) { $items[] = array( date => strtotime($tweet[created_at]), message => $tweet[text]);}foreach($items as $item) { echo $item[message]; View part!}
  11. 11. Separation of concerns Very unstable. Not safe for changes....
  12. 12. Separation of concerns „Make everything as simple as possible...“
  13. 13. Separation of concerns Establish boundaries
  14. 14. Separation of concerns What are the boundaries? Presentation Layer
  15. 15. Separation of concerns What are the boundaries? Presentation Layer Business Layer
  16. 16. Separation of concerns What are the boundaries? Presentation Layer Business Layer Resource Access Layer
  17. 17. Separation of concerns Isolate functionality, break it apart!
  18. 18. Separation of concerns<?phpnamespace AcmeDemoBundleController;use SymfonyBundleFrameworkBundleControllerController;use SensioBundleFrameworkExtraBundleConfigurationRoute;use SensioBundleFrameworkExtraBundleConfigurationTemplate;class DemoController extends Controller { /** * @Route("/", name="_demo") * @Template() */ public function indexAction() { $items = array(); $token = file_get_contents("https://graph.facebook.com/oauth/access_to..."); $feed = file_get_contents("https://graph.facebook.com/ident/feed?".$token); $feed = json_decode($feed, true); foreach($feed[data] as $post) { if(123456789012 === $post[from][id]) { $items[] = array( date => strtotime($post[created_time]), message => $post[message]); } }
  19. 19. Separation of concerns $feed = file_get_contents("http://search.twitter.com/search.json?..."); $feed = json_decode($feed, true); foreach($feed[results] as $tweet) { $items[] = array( date => strtotime($tweet[created_at]), message => $tweet[text]); } return array(items => $items); }}
  20. 20. Separation of concerns Focus on one responsibility a time!
  21. 21. Separation of concerns<?phpnamespace AcmeDemoBundleController;use SymfonyBundleFrameworkBundleControllerController;use SensioBundleFrameworkExtraBundleConfigurationRoute;use SensioBundleFrameworkExtraBundleConfigurationTemplate;class DemoController extends Controller { /** * @Route("/", name="_demo") * @Template() */ public function indexAction() { $items = array(); Facebook connector $token = file_get_contents("https://graph.facebook.com/oauth/access_to..."); $feed = file_get_contents("https://graph.facebook.com/ident/feed?".$token); $feed = json_decode($feed, true); foreach($feed[data] as $post) { if(123456789012 === $post[from][id]) { $items[] = array( date => strtotime($post[created_time]), message => $post[message]); } }
  22. 22. Separation of concerns Twitter connector $feed = file_get_contents("http://search.twitter.com/search.json?..."); $feed = json_decode($feed, true); foreach($feed[results] as $tweet) { $items[] = array( date => strtotime($tweet[created_at]), message => $tweet[text]); } return array(items => $items); }}
  23. 23. Separation of concerns Interfaces as a contract
  24. 24. Separation of concerns<?phpinterface ConnectorInterface { /** * Will return an array of the latest posts. * return array */ public function getLatestPosts() { }}
  25. 25. Separation of concerns<?phpclass FacebookConnector implements ConnectorInterface { protected $handle; public function __construct($handle) { $this->handle = $handle; } public function getLatestPosts() { $items = array(); $token = file_get_contents("https://graph.facebook.com/oauth/access..."); $feed = file_get_contents("https://graph.facebook.com/. $this->handle."/feed?".$token); $feed = json_decode($feed, true); foreach($feed[data] as $post) { if(123456789012 === $post[from][id]) { $items[] = array( date => strtotime($post[created_time]), message => $post[message]); } } return $items; }}
  26. 26. Separation of concerns<?phpclass FacebookConnector implements ConnectorInterface { protected $handle; public function __construct($handle) { $this->handle = $handle; } public function getLatestPosts() { $token = $this->getAccessToken(); return $this->readPosts($token); } protected function getAccessToken() { return file_get_contents("https://graph.facebook.com/oauth/access_?..."); } protected function readPosts($token) { // read the post, filter all relevant and return them... }}
  27. 27. Separation of concerns<?phpclass FacebookConnectorV2 extends FacebookConnector { protected function getAccessToken() { return md5($this->handle); }} Easy to extend, will not influence the behaviour of other methods!
  28. 28. Separation of concerns<?phpnamespace AcmeDemoBundleController;use SymfonyBundleFrameworkBundleControllerController;use SensioBundleFrameworkExtraBundleConfigurationRoute;use SensioBundleFrameworkExtraBundleConfigurationTemplate;class DemoController extends Controller { /** * @Route("/", name="_demo") * @Template() */ public function indexAction() { $items = array(); $fb = $this->get(FbConnector); $items += $fb->getLatestPosts(); $feed = file_get_contents("http://search.twitter.com/search.json?..."); $feed = json_decode($feed, true); foreach($feed[results] as $tweet) { $items[] = array( date => strtotime($tweet[created_at]), message => $tweet[text]); }
  29. 29. Separation of concerns<?phpnamespace AcmeDemoBundleController;use SymfonyBundleFrameworkBundleControllerController;use SensioBundleFrameworkExtraBundleConfigurationRoute;use SensioBundleFrameworkExtraBundleConfigurationTemplate;class DemoController extends Controller { /** * @Route("/", name="_demo") * @Template() */ public function indexAction() { $items = array(); $fb = $this->get(FbConnector); $items += $fb->getLatestPosts(); $twitter = $this->get(TwitterConnector); $items += $twitter->getLatestPosts(); return array(items => $items); }}
  30. 30. Separation of concerns Will improve the testability!
  31. 31. Separation of concerns Will reduce tight coupling!
  32. 32. Separation of concerns Will increase component reuse!
  33. 33. Separation of concerns The value of separation Why should I do it?
  34. 34. Separation of concerns The value of separation 1. Getting rid of code duplication
  35. 35. Separation of concerns The value of separation 2. Application becomes more stable and easier to understand
  36. 36. Separation of concerns The value of separation 3. Single responsibility offers clear extension points
  37. 37. Separation of concerns The value of separation 4. Increases the reusability of components
  38. 38. Separation of concerns How to separate the concerns?
  39. 39. Separation of concerns How to separate? Horizontal Separation Presentation Layer Business Layer Resource Access Layer
  40. 40. Separation of concerns How to separate? Horizontal Separation ./symfony |-app |---cache |---config |---Resources |-src |---Acme |-----DemoBundle |-------Controller |-------Resources |---------config |---------public |-----------css |-----------images |---------views |-----------Demo |-----------Welcome |-------Tests |---------Controller |-web
  41. 41. Separation of concerns How to separate? Service Separation Service Interface Layer Business Layer Resource Access Layer
  42. 42. Separation of concerns How to separate? Service Separation Frontend / UI Webservers REST API Solr Search Service Datastore
  43. 43. Separation of concerns How to separate? Vertical Separation Module A Module B Module C
  44. 44. Separation of concerns How to separate? Vertical Separation ./symfony |-app |---cache |---config |---Resources |-src |---Acme |-----AdminBundle |-------Controller |-------Resources |-------Tests |-----ProductsBundle |-------Controller |-------Resources |-------Tests |-----CustomersBundle |-------Controller |-------Resources |-------Tests |-web
  45. 45. Separation of concerns How to separate? Vertical Separation Presentation Presentation Presentation Layer Layer Layer Business Business Business Layer Layer Layer Resource Resource Resource Access Layer Access Layer Access Layer
  46. 46. Separation of concerns How to separate? Vertical Separation ./symfony |-app |---cache |---config |---Resources |-src |---Acme |-----AdminBundle |-------Controller |-------Resources |-------Tests |-----ProductsBundle |-------Controller |-------Resources |-------Tests |-----CustomersBundle |-------Controller |-------Resources |-------Tests |-web
  47. 47. Separation of concerns Cross-cutting concerns? How to deal with security or logging aspects?
  48. 48. Separation of concerns How to separate? Aspect Separation Aspects Presentation Layer Business Layer Resource Access Layer
  49. 49. Separation of concerns How to separate? Aspect Separation <?php namespace AcmeProductsAspects; /** * @FLOW3Aspect */ class LoggingAspect { /** * @FLOW3Inject * @var AcmeLoggerLoggerInterface */ protected $logger; /** * @param TYPO3FLOW3AOPJoinPointInterface $joinPoint * @FLOW3Before("method(AcmeProductsModelProduct->delete())") */ public function log(TYPO3FLOW3AOPJoinPointInterface $jp) { $product = $jp->getMethodArgument(product); $this->logger->info(Removing . $product->getName()); } }
  50. 50. Separation of concerns Dependency Direction
  51. 51. Separation of concerns Dependency Direction "High-level modules should not depend on low-level modules. Both should depend on abstractions." Robert C. Martin
  52. 52. Separation of concerns Inverting Concerns Presentation Layer Business Layer Resource Access Layer
  53. 53. Separation of concerns Inverting Concerns Presentation UI Presentation Layer Component Layer Business Business Layer Layer Resource Resource Access Layer Access Layer
  54. 54. Separation of concerns Inverting Concerns The goal of Dependency Injection is to separate the concerns of obtaining the dependencies from the core concerns of a component.
  55. 55. Separation of concerns What are the benefits? Let`s recap....
  56. 56. Separation of concerns What are the benefits? 1. Facilitate reusability
  57. 57. Separation of concerns What are the benefits? 2. Ensure the maintainability
  58. 58. Separation of concerns What are the benefits? 3. Increasing the code quality
  59. 59. Separation of concerns What are the benefits? 4. Enables everyone to understand the application
  60. 60. Separation of concerns What are the benefits? 5. Allows developers to work on components in isolation
  61. 61. Thank you!
  62. 62. http://joind.in/6244
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×