Ray by Day JS

Day 7 - Making Grid Libraries Interchangeable

On Day 7, we take a significant step in the design of our graphing tools by introducing the concept of abstraction through the creation of a new class: GenericGridGrapher.

Abstract Classes in JavaScript?
While JavaScript doesn't have true abstract classes like some other languages, GenericGridGrapher serves as an "abstract" base class in practice. It defines a common interface and shared logic for different types of grid graphers, but isn't meant to be used directly. Instead, specific implementations inherit from it and provide their own rendering logic.

Polymorphism in Action
To demonstrate the power and flexibility of this abstraction, you can use the toggle below to switch between two different implementations:

Why does this matter?
This exercise highlights the benefits of object-oriented programming (OOP) principles like abstraction and polymorphism. By coding to an abstract interface, you can easily extend or modify your program's behavior without changing its core logic.

Try toggling between the two implementations and observe how the same function is rendered in completely different ways. Consider how this approach could be used to add new rendering methods in the future.

SVG dynamically created ...

SVG should be produced here on script load.

Resources used in this page

Prev Home Next