Design Mode (adapter)

Source: Internet
Author: User
Design Mode (adapter)
Personal Understanding: The adapter serves as the medium between the original function and the new function. that is to say, the new function must call some of the original functions. This improves code reusability. (If you do not understand this, please give me some advice. thank you)




  1. Class errorObject {
  2. Private $ __error;
  3. Public function _ construct ($ error)
  4. {
  5. $ This->__ error = $ error;
  6. }
  7. Public function getError ()
  8. {
  9. Return $ this->__ error;
  10. }
  11. }
  12. Class logToConsole {
  13. Private $ __errorobject;
  14. Public function _ construct ($ errorObject)
  15. {
  16. $ This->__ errorObject = $ errorObject;
  17. }
  18. Public function write ()
  19. {
  20. Fwrite ("errorlog.txt", $ this->__ errorObject-> getError ());
  21. }
  22. }
  23. Class logToCVS
  24. {
  25. Const log_location1_'log.csv ';
  26. Private $ __errorobject;
  27. Public function _ construct ($ errorObject)
  28. {
  29. $ This->__ errorObject = $ errorObject;
  30. }
  31. Public function write ()
  32. {
  33. $ Line = $ this->__ errorObject-> getErrorNumber ();
  34. $ Line. = ',';
  35. $ Line. = $ this->__ errorObject-> getErrorText ();
  36. $ Line. = "\ n ";
  37. File_put_contents (self: LOG_LOCATION, $ line, FILE_APPEND );
  38. }
  39. }
  40. Class logToCSVAdapter extends errorObject {// adapter
  41. Private $ _ errorNumber, $ _ errorText;
  42. Public function _ construct ($ error)
  43. {
  44. Parent: _ construct ($ error );
  45. $ Parts = explode (":", $ this-> getError ());
  46. $ This-> _ errorNumber = $ parts [0];
  47. $ This-> _ errorText = $ parts [1];
  48. }
  49. Public function getErrorNumber ()
  50. {
  51. Return $ this-> _ errorNumber;
  52. }
  53. Public function getErrorText ()
  54. {
  55. Return $ this-> _ errorText;
  56. }
  57. }
  58. // Write error information to txt
  59. $ Error = new errorObject ("404: not fuond ");
  60. $ Log = new logToConsole ($ error );
  61. $ Log-> write ();
  62. // Write error information into csv
  63. $ Error = new logToCSVAdapter ("404: not fuond ");
  64. $ Log = new logToCVS ($ error );
  65. $ Log-> write ();

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.