Notes on three common design patterns in php

Source: Internet
Author: User
Notes on three common design patterns in php

  1. Class SingetonBasic {

  2. Private static $ instance;

  3. // Other vars ..

  4. Private function _ construct (){

  5. // Do construct ..
  6. }

  7. Private function _ clone (){}

  8. Public static function getInstance (){

  9. If (! (Self: $ instance instanceof self )){
  10. Self: $ instance = new self ();
  11. }
  12. Return self: $ instance;
  13. }

  14. // Other functions ..

  15. }

  16. $ A = SingetonBasic: getInstance ();

  17. $ B = SingetonBasic: getInstance ();
  18. Var_dump ($ a ===$ B );

II. factory modelThe factory mode allows you to create a class dedicated to implementation and return other classes based on input parameters or application configurations. Example of Factory mode:

  1. Class FactoryBasic {

  2. Public static function create ($ config ){

  3. }

  4. }

For example, this is a factory that describes shape objects. it wants to create different shapes based on the number of input parameters.

  1. // Define the common function of the shape: obtain the length and area of the week.

  2. Interface IShape {
  3. Function getCircum ();
  4. Function getArea ();
  5. }

  6. // Define the rectangle class

  7. Class Rectangle implements IShape {
  8. Private $ width, $ height;

  9. Public function _ construct ($ width, $ height ){

  10. $ This-> width = $ width;
  11. $ This-> height = $ height;
  12. }

  13. Public function getCircum (){

  14. Return 2 * ($ this-> width + $ this-> height );
  15. }

  16. Public function getArea (){

  17. Return $ this-> width * $ this-> height;
  18. }
  19. }

  20. // Define the circle class

  21. Class Circle implements IShape {
  22. Private $ radii;

  23. Public function _ construct ($ radii ){

  24. $ This-> radii = $ radii;
  25. }

  26. Public function getCircum (){

  27. Return 2 * M_PI * $ this-> radii;
  28. }

  29. Public function getArea (){

  30. Return M_PI * pow ($ this-> radii, 2 );
  31. }
  32. }

  33. // Create different shapes based on the number of input parameters.

  34. Class FactoryShape {
  35. Public static function create (){
  36. Switch (func_num_args ()){
  37. Case 1:
  38. Return new Circle (func_get_arg (0 ));
  39. Break;
  40. Case 2:
  41. Return new Rectangle (func_get_arg (0), func_get_arg (1 ));
  42. Break;

  43. }

  44. }
  45. }

  46. // Rectangular object

  47. $ C = FactoryShape: create (4, 2 );
  48. Var_dump ($ c-> getArea ());
  49. // Circle object
  50. $ O = FactoryShape: create (2 );
  51. Var_dump ($ o-> getArea ());

Using the factory mode makes it easier to call a method because it only has one class and one method. if the factory mode is not used, it is necessary to decide which class and method should be called during the call; using the factory mode makes it easier to change the application in the future, such as adding a shape of support, you only need to modify the create () method in the factory class, instead of using the factory mode, you need to modify the code block that calls the shape.

3. Observer ModeThe Observer Mode provides another way to avoid close coupling between components. This mode is very simple: an object becomes observability by adding a method (this method allows another object, that is, the Observer registers itself. When an observed object changes, the message is sent to the registered observer. The operations performed by these observers using this information are irrelevant to the observed objects. The result is that objects can communicate with each other without understanding the cause.

A simple example: when a listener is listening to a station (that is, a new listener is added to the station), it sends a prompt message, which can be observed by the log observer who sends the message.

  1. // Observer interface

  2. Interface IObserver {
  3. Function onListen ($ sender, $ args );
  4. Function getName ();
  5. }

  6. // Interface to be observed

  7. Interface IObservable {
  8. Function addObserver ($ observer );
  9. Function removeObserver ($ observer_name );
  10. }

  11. // Observer class

  12. Abstract class Observer implements IObserver {
  13. Protected $ name;

  14. Public function getName (){

  15. Return $ this-> name;
  16. }
  17. }

  18. // Class to be observed

  19. Class Observable implements IObservable {
  20. Protected $ observers = array ();

  21. Public function addObserver ($ observer ){

  22. If (! ($ Observer instanceof IObserver )){
  23. Return;
  24. }
  25. $ This-> observers [] = $ observer;
  26. }

  27. Public function removeObserver ($ observer_name ){

  28. Foreach ($ this-> observers as $ index => $ observer ){
  29. If ($ observer-> getName () ===$ observer_name ){
  30. Array_splice ($ this-> observers, $ index, 1 );
  31. Return;
  32. }
  33. }
  34. }
  35. }

  36. // Simulate a class that can be observed: RadioStation

  37. Class RadioStation extends Observable {

  38. Public function addListener ($ listener ){

  39. Foreach ($ this-> observers as $ observer ){
  40. $ Observer-> onListen ($ this, $ listener );
  41. }
  42. }
  43. }

  44. // Simulate an observer class

  45. Class RadioStationLogger extends Observer {
  46. Protected $ name = 'logger ';

  47. Public function onListen ($ sender, $ args ){

  48. Echo $ args, 'Join the radiostation.
    ';
  49. }
  50. }

  51. // Simulate another observer class

  52. Class OtherObserver extends Observer {
  53. Protected $ name = 'other ';
  54. Public function onListen ($ sender, $ args ){
  55. Echo 'other observer ..
    ';
  56. }
  57. }

  58. $ Rs = new RadioStation ();

  59. // Inject the Observer

  60. $ Rs-> addObserver (new RadioStationLogger ());
  61. $ Rs-> addObserver (new OtherObserver ());

  62. // Remove the Observer

  63. $ Rs-> removeObserver ('other ');

  64. // You can see the observed information.

  65. $ Rs-> addListener ('CCTV ');
  66. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.