Advertisement
Ikem

chap9.txt

Jul 3rd, 2016
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. ------------------------------
  2. 9. Object Oriented Programming
  3. ------------------------------
  4.  
  5. ... is possible. In this simple batch extension, classes and objects
  6. are stored in an elaborate directory structure. The internal state of
  7. each object is kept on disk, so there is no need to use seperate
  8. databases for permanent data storage. Develop your classes from the
  9. command line, and use 'call create', 'call kill' and 'call send' from
  10. ordinary batch files to use objects.
  11.  
  12. First I'll demonstrate the functions; the implementation follows:
  13.  
  14. C:\>md aproject
  15. C:\>cd aproject
  16. C:\APROJECT>init [make necessary subdirectories]
  17. C:\APROJECT>class man [define class man]
  18. C:\APROJECT>static man name [declare static variable for man]
  19. C:\APROJECT>method man set-name [edit method for man]
  20. {in the editor type SET NAME=%1 %2 %3 %4 %5}
  21. C:\APROJECT>method man get-name
  22. {in the editor type SET RESULT=%NAME%}
  23. C:\APROJECT>method man prt-name
  24. {in the editor type ECHO.%NAME%}
  25. C:\APROJECT>create man John [create a test instance of man...]
  26. C:\APROJECT>send John set-name John Van Halen [... and use it]
  27. C:\APROJECT>create man Fred
  28. C:\APROJECT>send Fred set-name Fred Ford
  29. C:\APROJECT>send John prt-name
  30. John Van Halen [it works !]
  31. C:\APROJECT>send Fred prt-name
  32. Fred Ford
  33. C:\APROJECT>class employee man [define a subclass of man]
  34. C:\APROJECT>static employee salary [extra static variable]
  35. C:\APROJECT>method employee set-sal [extra methods; it is also
  36. possible to override
  37. methods of the superclass]
  38. ...
  39. C:\APROJECT>create employee Bill
  40. C:\APROJECT>send Bill set-name Bill Buddy [use an inherited method]
  41. ...
  42.  
  43. METHOD.BAT assumes that there is an editor named EDIT, which takes a file
  44. name as its command line argument, in a directory of the system path.
  45. Class, object, method and (static) variable names are restricted to eight
  46. characters. Use tree /f to get a good idea of how the objects and classes
  47. are represented on disk.
  48.  
  49. WARNING: This implementation does no error checking, which makes the
  50. structure of the programs very clear; but when using them, an infinite
  51. loop is only a typo away.
  52.  
  53.  
  54. A. User functions:
  55.  
  56. INIT.BAT
  57.  
  58. @echo off
  59. mkdir classes
  60. mkdir objects
  61.  
  62. CLASS.BAT
  63.  
  64. @echo off
  65. mkdir classes\%1
  66. mkdir classes\%1\methods
  67. mkdir classes\%1\statics
  68. if [%2]==[] goto end
  69. rem > classes\%1\%2
  70. xcopy classes\%2\statics\*.* classes\%1\statics > nul
  71. :: ordinary copy doesn't copy empty files !
  72. :end
  73.  
  74. STATIC.BAT
  75.  
  76. @echo off
  77. rem > classes\%1\statics\%2
  78.  
  79. METHOD.BAT
  80.  
  81. @echo off
  82. edit classes\%1\methods\%2.bat
  83.  
  84. CREATE.BAT
  85.  
  86. @echo off
  87. set _class=%1
  88. set _object=%2
  89. cd classes\%_class%\statics
  90. for %%a in (*) do set %%a=(nil)
  91. cd ..\..\..
  92. call save
  93. cd classes\%_class%\statics
  94. for %%a in (*) do set %%a=
  95. cd ..\..\..
  96. set _object=
  97. set _class=
  98.  
  99. KILL.BAT
  100.  
  101. @echo off
  102. del objects\%1.bat
  103.  
  104. SEND.BAT
  105.  
  106. @echo off
  107. if [%_object%]==[] goto no-saving
  108. call push %_object%
  109. call save
  110. :no-saving
  111. set _object=%1
  112. set _parms=%2
  113. :loop
  114. shift
  115. if not [%2]==[] set _parms=%_parms% %2
  116. if not [%3]==[] goto loop
  117. call restore
  118. call methods %_parms%
  119. call save
  120. set _object=
  121. set _parms=
  122. cd classes\%_class%\statics
  123. for %%a in (*) do set %%a=
  124. cd ..\..\..
  125. set _class=
  126. call pop
  127. if [%_object%]==[] goto no-restoring
  128. call restore
  129. :no-restoring
  130.  
  131. B. Internal procedures:
  132.  
  133. SAVE.BAT
  134.  
  135. echo set _class=%_class%>objects\%_object%.bat
  136. cd classes\%_class%\statics
  137. for %%a in (*.*) do echo call save-sub %%a %%%%a%% >> ..\..\..\temp.bat
  138. cd ..\..\..
  139. for %%a in (call del) do %%a temp.bat
  140.  
  141. SAVE-SUB.BAT
  142.  
  143. set _varname=%1
  144. set _to-save=%2
  145. :loop
  146. shift
  147. if not [%2]==[] set _to-save=%_to-save% %2
  148. if not [%2]==[] goto loop
  149. echo set %_varname%=%_to-save%>>objects\%_object%.bat
  150. set _to-save=
  151. set _varname=
  152.  
  153. METHODS.BAT
  154.  
  155. set _searchclass=%_class%
  156. set _method=%1
  157. set _mparms=%2
  158. :parmloop
  159. shift
  160. if not [%2]==[] set _mparms=%_mparms% %2
  161. if not [%2]==[] goto parmloop
  162. :loop
  163. if not exist classes\%_searchclass%\methods\%_method%.bat goto next
  164. call classes\%_searchclass%\methods\%_method% %_mparms%
  165. goto end
  166. :next
  167. cd classes\%_searchclass%
  168. for %%a in (*) do set _searchclass=%%a
  169. cd ..\..
  170. goto loop
  171. :end
  172. set _searchclass=
  173. set _method=
  174. set _mparms=
  175.  
  176. RESTORE.BAT
  177.  
  178. objects\%_object%.bat
  179.  
  180. PUSH.BAT
  181.  
  182. @echo off
  183. set to-push=%1
  184. :loop
  185. shift
  186. if not [%1]==[] set to-push=%to-push% %1
  187. if not [%1]==[] goto loop
  188. echo set _object=%to-push%>_tmp
  189. if exist _stack type _stack>>_tmp
  190. if exist _stack del _stack
  191. ren _tmp _stack
  192. set to-push=
  193.  
  194. POP.BAT
  195.  
  196. @echo off
  197. set _object=
  198. if not exist _stack goto end
  199. copy _stack _tmp.bat > nul
  200. echo 1,1d;e | edlin _stack > nul
  201. echo 2,65500d;e | edlin _tmp.bat > nul
  202. call _tmp
  203. for %%a in (_tmp.bat _tmp.bak _stack.bak) do del %%a
  204. :end
  205.  
  206. I call this OOBL -- Object Oriented Batch Language.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement