Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an example of object oriented program #681

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

jpellegrini
Copy link
Contributor

Hi @egallesio !
One more example. This one has a mock ansi-color? procedure, but it would work better after PR #679 :)

This is the usual "geometric shapes" example, but one can actually draw the shapes on a terminal-based system! With colors!

  • This is an example of medium difficulty. It illustrates several object orientation concepts as they are implemented in STklos.
  • The geometric shapes are not pretty. It's an example of a object oriented program, and the focus is not on doing ASCII prettyfication of the figures.
  • I have added some exercises at the end of the file. :)
  • Not the best possible OO design, but I was focusing more on trying to exemplify several different CLOS concepts. :)

Concepts illustrated:

  1. Class definition, #:accessor, #:init-keyword, #:init-form
  2. Class inheritance
  3. Generic methods, including the use of next-method inside initialize
  4. Using only the class of an object to get different behavior (the initializers for <triangle> and <parallelogram> are mostly the same, but the objects will be drawn differently because they are instances of different classes)
  5. display-object and object-equal?
  6. One case in which syntax-rules would get much more complex than low-level macros
  7. Specific STklos procedures: 1+, 1-, key-get

This is the uusal "geometric shapes" example, but one can actually
draw the shapes on a terminal-based system! With colors!

* This is an example of medium difficulty. It illustrates several
  object orientation concepts *as they are implemented in STklos*.
* The geometric shapes are not pretty. It's an example of a object
  oriented program, and the focus is not on doing ASCII prettyfication
  of the figures.

Concepts illustrated:

1. Class definition, #:accessor, #:init-keyword, #:init-form
2. Class inheritance
3. Generic methods, including the use of next-method inside
   initialize
4. Using *only* the class of an object to get different behavior
   (the initializers for <triangle> and <parallelogram> are mostly
   the same, but the objects will be drawn differently *because
   they are instances of different classes*)
5. display-object and object-equal?
6. One case in which syntax-rules would get much more complex than
   low-level macros
7. Specific STklos procedures: 1+, 1-, key-get
@jpellegrini
Copy link
Contributor Author

  • Not the best possible

In particular, I don't like the #:points list-of-points idea... But anyway, it's already there.

@jpellegrini
Copy link
Contributor Author

To see how it works, do this:

(load "ascii-draw.stk")

(define v (make <canvas> #:rows 20 #:columns 40))
(define p (make <point>  #:x 10 #:y 25))
(define i (make <circle> #:center p #:radius 8 #:color 'red))

(define q (make <point>  #:x 5  #:y 8))
(define r (make <point>  #:x 10  #:y 7))
(define s (make <point>  #:x 7  #:y 4))
(define a (make <parallelogram> #:points (list q r s) #:color 'blue))

(draw v i)
(display v)
(draw v a)
(display v)

(reset! v)
(display v)

(define g (make <polygon> #:points (list q s r p) #:color 'blue))
(draw v g)
(display v)

(set! (canvas-background v) white)
(display v)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant