{ |one, step, back| }

The Shape Example in DOS Batch

Contributed by Scott Anderson

Presenting the DOS BATCH FILE POLYMORPHISM EXAMPLE!!!!

Notes:

Again, the dynamic batch file calls are what allow the programming trick.

Also, note the lack of a relative move method on the shapes. There is no way to add two DOS environment variables together.

Code for DOS Batch

File: SHAPE.BAT

@echo off

echo @echo off > %1.bat

REM set up shape variables
echo set x=%2 >>%1.bat
echo set y=%3 >> %1.bat

REM set up a method for all shapes
echo set moveto=mvshape >> %1.bat

File: MVSHAPE.BAT

REM tack the new values on the end
REM since the "objects" are stored as batch files,
REM these lines will get executed last every time
REM the object is loaded.

echo set x=%2 >> %1.bat
echo set y=%3 >> %1.bat

File: RECT.BAT

@echo off

REM inherit the shape stuff
call shape %1 %2 %3

REM add rectangle-specific vars
echo set width=%4 >> %1.bat
echo set height=%5 >> %1.bat

REM add rectangle-specific methods
echo set draw=drawrec >> %1.bat
echo set setwidth=recsetw >> %1.bat

File: DRAWREC.BAT

REM load the rectangle
call %1

REM Draw the rectangle
echo Drawing rectangle at: (%x%, %y%) width=%width%, height=%height%

File: RECSETW.BAT

@echo off

REM set the width of the rectangle
echo set width=%2 >> %1.bat

File: CIRCLE.BAT

REM Make a circle

REM Inherit the shape stuff
call shape %1 %2 %3

REM set up circle-specific variables
echo set radius=%4 >> %1.bat

REM add a circle draw method
echo set draw=drawcirc >> %1.bat

File: DRAWCIRC.BAT

REM load the circle
call %1

REM draw the circle
echo Drawing circle at: (%x%, %y%) radius=%radius%

File: DOIT.BAT

@echo off

REM load the shape
call %1

REM call the shape's draw method
call %draw% %1

REM move the shape
call %moveto% %1 100 100

REM call the shape's draw method
call %draw% %1

File: FOO.BAT

@echo off

REM polymorphic batch file example
REM November 8, 1999 - Scott Anderson

REM Make a rectangle and a circle.
call rect rec1 10 20 5 6
call circle circ1 15 25 8

REM loop through the shapes and polymorphically call their methods
for %%V in (rec1 circ1) do call doit %%V

REM make a rectangle
call rect rec2 0 0 15 15
call rec2
call %setwidth% rec2 30
call %draw% rec2

Output

C:\SHAPES>foo
Drawing rectangle at: (10 , 20 ) width=5 , height=6
Drawing rectangle at: (100 , 100 ) width=5 , height=6
Drawing circle at: (15 , 25 ) radius=8
Drawing circle at: (100 , 100 ) radius=8
Drawing rectangle at: (0 , 0 ) width=30 , height=15