[tsmith] If you and I both know what the Factory Pattern is, you can say “We should use a Factory” and I’ll know instantly that you mean, “We should use an independent complex object creator and linker instead of trying to build the objects manually, explicitly, all over the place.” [tsmith] Does that make sense? * tcorley has quit (Quit: Linkinus - http://linkinus.com) [companyhen] Building objects “by hand”? And anywhere in the program? Lol. [tsmith] Would you like to see an example of a factory? [companyhen] YES! But I’m about to leave work :[ [companyhen] Adios. Be back soon. * companyhen has quit (Quit: companyhen) [tsmith] http://repo.phpexperts.pro/source/driving_app/annotate/head:/lib/Cars/misc/CarFactory.inc.php [tsmith] there’s the Car Factory. [tsmith] It’s meant for building complex Car objects that are part of a driving app that simulates real-world MPG rates for real-world cars across various real-world routes * tcorley (~tcorley@c-76-27-139-64.hsd1.al.comcast.net) has joined #learnphp [tsmith] look at line 60: function loadCar() [tsmith] Err I mean line 82: buildCar() [tsmith] Now, you could explicitly write up lines 90 through 125 every time you want a new car object [tsmith] or you could do: [tsmith] CarFactory::buildCar(‘HondaInsightCar’); [tsmith] or [tsmith] $prius = CarFactory::buildCar(‘ToyotaPriusCar’); [tsmith] Because the class types are dynamic, it’s what’s called an Abstract Factory Pattern. [tsmith] And that’s 1/2 the Factory Pattern lesson already, lol. [tsmith] Actually, 3/4 [Arth] wpic [Arth] epic* [tsmith] OK To make a factory all you have to do is follow this *ahem* PATTERN: [Arth] thats why i want to learn oop php it makes things so much faster and easier [tsmith] Yeah, seriously, right? [tsmith] Here’s a factory pattern in like 10 lines:

  class Shirt {
      protected $buttons;
      public function __construct(array $buttons) { $this-]buttons = $buttons; }
  class Button { }
  class ShirtFactory {
      public static function build() {
      // Let's say right now we only make 1 size shirts that have 10 buttons.
              $buttons = array();
          for ($a = 0; $a [ 10; ++$a) { $buttons[] = new Button; }
          $shirt = new Shirt($buttons);
          return $shirt;

[tsmith] OK NOw, pop quiz: What is a benefit of building every shirt object in your app via a Factory this way? [Arth] it will always be consistent [Arth] ? [tsmith] Yeah, you can control how they will be made. [tsmith] #2 You could add multiple sized shirts far easily. Just create a parameter called $shirtSize like this: [tsmith] public static function build($shirtSize = ‘medium’) { } [tsmith] Then you can keep backward compatible code and still produce new code w/ awareness that your company can now offer multiple sized shirts. [tsmith] Also, you can add pockets, etc., all w/o having to change any of the end-dev code that relies on Shirt objects. [tsmith] To recap, the main advantages of factories are: [tsmith] #1 Uniformity: You know your objects will all be more or less of the same finite patterns. [tsmith] #2 Simplicity: You can create really complex objects with a single line of largely-uniform code. [tsmith] #3 Extensibility: Extending one place will let the entire app benefit. [tsmith] #4 Maintainability: You can be far more sure that tweaking functionality will remain backward compatible. [tsmith] This is stuff procedural coding simply can’t touch.