Witajcie,

Postanowiłem, że już pora, aby poznać wzorce projektowe. Poprosiłbym o sprawdzenie czy poprawnie zastosowałem wzorzec Metoda Fabrykująca. Jeżeli potrzeba dodatkowych informacji - pytajcie, bo na chwile obecną nic nie przychodzi mi do głowy smile.gif

Interfejsy:

  1. //FactoryAbstract.php
  2. abstract class FactoryAbstract
  3. {
  4. /**
  5.   * @param $name
  6.   *
  7.   * @return object
  8.   * @throws Exception
  9.   */
  10. public function createProduct( $name )
  11. {
  12. if ( file_exists( $this->getProductPath() . $name . '.php' ) ) {
  13. include_once($this->getProductPath() . $name . ".php");
  14. } else {
  15. throw new Exception("Plik '$name.php' nie istnieje.");
  16. }
  17.  
  18. if ( class_exists( $name ) ) {
  19. return new $name();
  20. } else {
  21. throw new Exception("Klasa '$name' nie istnieje.");
  22. }
  23. }
  24.  
  25. /**
  26.   * @return string
  27.   */
  28. protected abstract function getProductPath();
  29. }
  30.  
  31. //ProductAbstract.php
  32. abstract class ProductAbstract
  33. {
  34. protected $name;
  35. protected $price;
  36.  
  37. /**
  38.   * @return string
  39.   */
  40. public abstract function getName();
  41.  
  42. /**
  43.   * @return string
  44.   */
  45. public abstract function getPrice();
  46. }



Fabryki:

  1. //FoodFactory.php
  2. include_once(ROOT . "FactoryAbstract.php");
  3.  
  4. class FoodFactory extends FactoryAbstract
  5. {
  6. /**
  7.   * @return string
  8.   */
  9. protected function getProductPath()
  10. {
  11. return ROOT . 'food/';
  12. }
  13. }
  14.  
  15. //GlassFactory.php
  16. include_once(ROOT . "FactoryAbstract.php");
  17.  
  18. class GlassFactory extends FactoryAbstract
  19. {
  20. /**
  21.   * @return string
  22.   */
  23. protected function getProductPath()
  24. {
  25. return ROOT . 'glass/';
  26. }
  27. }


Produkty:

  1. //glass/Kieliszek.php
  2. include_once(ROOT . "ProductAbstract.php");
  3.  
  4. class Kieliszek extends ProductAbstract
  5. {
  6. public function __construct()
  7. {
  8. $this->name = "Kieliszek gran party";
  9. $this->price = 9.91;
  10. }
  11.  
  12. public function getName()
  13. {
  14. return $this->name;
  15. }
  16.  
  17. public function getPrice()
  18. {
  19. return $this->price;
  20. }
  21. }
  22.  
  23. //food/Pierogi.php
  24. include_once(ROOT . "ProductAbstract.php");
  25.  
  26. class Pierogi extends ProductAbstract
  27. {
  28. public function __construct()
  29. {
  30. $this->name = "Pierogi ruskie delux, 10 sztuk.";
  31. $this->price = 12.99;
  32. }
  33.  
  34. public function getName()
  35. {
  36. return $this->name;
  37. }
  38.  
  39. public function getPrice()
  40. {
  41. return $this->price;
  42. }
  43. }



Kod wysyłający żądanie:

  1. ini_set( 'display_errors', '1' );
  2. ERROR_REPORTING( E_ALL );
  3.  
  4. define('ROOT', __DIR__ . '/');
  5. include_once(ROOT . "GlassFactory.php");
  6. include_once(ROOT . "FoodFactory.php");
  7.  
  8. class Client
  9. {
  10. private $glassFactory;
  11. private $foodFactory;
  12. private $products;
  13.  
  14. public function __construct()
  15. {
  16. //stawiamy fabryki - ich "produkty" posiadają wspólny interfejs (getName, getPrice)
  17. $this->glassFactory = new GlassFactory();
  18. $this->foodFactory = new FoodFactory();
  19.  
  20. try {
  21. //tu dodać nowy produkt
  22. $this->products = array(
  23. $this->glassFactory->createProduct( 'Kieliszek' ),
  24. $this->foodFactory->createProduct( 'Pierogi' )
  25. );
  26.  
  27. } catch ( ErrorException $e ) {
  28. die($e->getMessage());
  29. }
  30. }
  31.  
  32. public function displayProducts()
  33. {
  34. foreach ( $this->products as $product ) {
  35. printf( "Nazwa: %s<br/>Cena: %.2f PLN<br/><br/>", $product->getName(), $product->getPrice() );
  36. }
  37. }
  38. }
  39.  
  40. $client = new Client();
  41. $client->displayProducts();