Click here to Skip to main content
15,904,986 members
Articles / Programming Languages / C#

OOP in the Real World - Creating an Equation Editor

Rate me:
Please Sign up or sign in to vote.
4.87/5 (106 votes)
21 Apr 2015MIT11 min read 139.6K   10.5K   220   93
Object Oriented Design and Programming process using a real world example

Update July 11, 2019: Latest installer added.

Math Editor (the full version) is Free and Open Source. You can get the full source code from In this article, I will use a strippped down version, which we will call Math Editor Mini, for pedagogical purpose.


An equation or formula editor is a computer software that helps us typeset mathematical content. In this article I will try to provide the readers with a real world application of Object Oriented design and programming techniques as we build our equation editor.

The language used is C# and we are going to use WPF as our GUI framework. However, the techniques provided are NOT dependent on the programming language or the GUI platform and can be applied in any other OO language like Java.

Before I go any further, I would like to express my gratitude to the STIX Fonts Project for creating such a great free font for mathematical & scientific typesetting.

Here is a screenshot of Math Editor:

Image 1


There are numerous resources available on the Internet and in the form of text books that teach the basic principles like objects, inheritance, polymorphism, encapsulation and so on. My focus will not be on the jargon as I assume that readers of this article are already familiar with the fundamentals of the subject and have some experience programming in an object oriented language like C# or Java.

In this article I primarily intend to address the following two categories of audience

  1. Programmers who know the OOP basics but are not able to apply the techniques in real world problems
  2. Experienced programmers who already know all the tricks but want to build an equation editor and want a starting point.

Analysis of Object Oriented Design Process

The best way to learn OOP is to apply the techniques in real programs. Only finishing a few examples given in the tutorials and books is not really sufficient as the examples presented there are usually very superficial. The biggest challenge in OO design and programming is identification of objects and inheritance and delegation of responsibilities to different classes. In a typical example found in text books the objects are almost clearly visible and there is hardly any work needed to define their roles and responsibilities. The following figure is a typical example of such a case:

Image 2

As we can see, the relationship chain given in the above figure is quite natural and there is hardly any effort required on our part to define it. However, we are not always so fortunate to have that kind of natural inheritance hierarchy in real world problems. For example think of GDI+ or WPF. Do the inheritance chains in those platforms really represent a natural underlying system? Did they just pop up from a real existing hierarchy or you think there was some effort needed to build them the way they are? I am sure the answer is obvious to you!

Sometimes the boundaries are so blurry that we have hard time figuring out how to devise a convenient OO model. Even the best of the best can find themselves quite perplexed at times (ask the MFC people!). The only good strategy in such cases is to give yourself a bit of time and keep looking for the invisible objects and the links between them. You may even need to experiment with a few different models before you decide which one to pick!

Designing the OO Model for the Equation Editor 

It is time to turn to the specific case we have decided to tackle and see if we can find our objects and their responsibilities and relationships. Let's first have a real deep look at a couple of equations typeset using Math Editor:

Image 3

Now try to answer the following questions:

  • Do you see any objects that we can use in our OO model for the equation editor?
  • Are the lines, letters and the brackets we see going to be our objects as they stand or will we need some other higher or lower level objects distinct from yet capable of representing these figures and letters?
  • Is there any specific relationship among these objects? If yes, is that relationship suitable to be represented in an OO Model?
  • Are there any common features that could be put in well defined related classes?
  • Will it be possible to create a common framework to handle input and output for different kinds of equations? Or will we need different strategy in different components?

Our goal is not the identification of objects, relationships and roles for only the currently visible equation entities. We want to be futuristic. We would like to create a robust model that is natural, flexible, extensible, maintainable and easy-to-understand and not too complex to implement.

Our task list consists of the following:

  • Identification of a set of classes that are capable of representing all the currently supported as well as yet-to-be-supported equations.
  • Building of a common model that allows us to typeset, serialize and represent equations in a uniform manner.
  • Creation of a unified framework for processing input using mouse and keyboard etc.

The task-list looks very short. However, if you give it some thought, you will see that it is very demanding as well as very inclusive. The first task wants us to not only cater for the few equations we can see in the given examples, but it also wants us to create basic support for more kinds of equations we are not yet considering. The second task demands to represent and save our model in a manner that it will be relatively easy for us to convert the equations to some other representation e.g. MathML or TeX when need be. The third task requires to create an input mechanism that suffices the basic needs of all kinds of equations we will ever support.

Now that we have identified some of the most important tasks and goals, let's once again have a look at the couple of equations we saw above and try to find the answers to the questions we asked. Do you see a pattern? Can you answer some or any of the questions? If yes, you have done a great job. But the chances are you will not discover much in just a few minutes!

Please remember that when the concept is more abstract than concrete, creating of a good OO model becomes relatively difficult and is almost always open to debate. There is never a mathematical proof that a particular OO model perfectly fits the given situation. It is more of an art than science. You may need to start over and over again and even sometimes just wait for the divine inspiration! My only advice is to keep looking for what fits best and make your decisions after as much thinking as needed.

I will now try to facilitate you finding the answers we are looking for. Here is a figure in which I represent a few equations as I see them at a lower level ready to be implemented in an OO manner:

Image 4

From the above figure, we can see a pattern emerging. The following are a few interesting facts we can notice:

  • Some of the entities are cyclic in nature i.e. they can host their own relatively smaller form as a container. For example, we can see a division appear inside another division and a bracket inside another bracket with no strict limit to the level of nesting.
  • There is an arranging of equations both vertically and horizontally in a repetitive manner.
  • The Unicode text either appears inside some other container or inside the top level container.

Understanding the Code

The entire core functionality resides inside just 6 classes. You only need to have a basic understanding of just these core 6 classes in order to able to understand the whole picture. After understanding these classes, you should be able to modify and extend the code according to your needs and wishes.  Before I give a brief description of the core classes, let's have a look at the main class hierarchy (the class names in italics represent abstract classes): 

Image 5

1. EquationBase

This is an abstract class. It contains the basic characteristics possessed by every equation we will ever typeset using our equation editor. This class is the ultimate base class of all equations we are going to create. Every other class in the equation model must inherit from this class or one of its descendents.

2. EquationContainer

This class is also abstract. As every equation must, it derives from EquationBase. This class is the base class of every equation that wants to host other equations inside it (hence the name!). This class is actually the backbone of equation nesting functionality we want implemented. 

3. TextEquation

This class allows us to add Unicode text to the document. It is the responsibility of this class to store and draw all textual content. This class is not a container itself (i.e. you cannot nest another EquationBase inside it), so it directly derives from EquationBase.

4. EquationRow

This class is the primary container of all the equations that need to be horizontally arranged. It supports both text equations as well as other container equations to nest inside it. All the container equations inside EquationRow must come between TextEquation instances. This way we are able to allow the user to enter text and more container equations anywhere they need.

5. RowContainer

Just like the lines of a paragraph or paragraphs of a section in a word processor, equations need to able to be typeset in horizontal lines. RowContainer is the class which supports that kind of functioanlity. The only equations it ever contains are instances of the class EquationRow. All other equations are then nested inside those EquationRow objects.

6. RootEquation

As the name suggests, this class is the first object to be created by the higher level GUI container. This class then creates a single RowContainer to get the ball rolling. Every subsequent equation is then created inside that single RowContainer object.

Some Points of Interest 

The 6 core classes discussed above construct the entire backbone of the model. From here we are ready to start implementing more useful equations like brackets, divisions, integrals and so on. However, building those kinds of equations from this point on is almost trivial. All the other container classes derive from EquationContainer and create one or more instances of RowContainer.

For ease of development, I have placed all the equation classes, including the core classes, inside a folder named equations in the project directory. Apart from the core classes, all the other equations reside in their respective sub-folders. 

Implementing Undo/Redo

This is, in fact, one of the most challenging tasks. We could make it very simple, though, by making use of the serialization routines used to save the files. But, that would be too inefficient. The most efficient way seems to let the major equation classes manage their own state and be able to revert back to a previous state when they are told to. Using this approach, however, the process becomes so invovled that we need to build a whole sub-system for this purpose.

The Undo/Redo sub-system is managed by the UndoManager class, which is responsible for keeping track of and dispatching undo/redo events. The core equations that will take part in the Undo/Redo stack must implement the following tiny interface:

public interface ISupportsUndo
    void ProcessUndo(EquationAction action);

EquationAction is the base class of all classes that will be passed around when the ProcessUndo(EquationAction action) method is called by the UndoManager class. Here's how EquationAction is defined:

public abstract class EquationAction
    public bool UndoFlag { get; set; }
    public ISupportsUndo Executor { get; set; }
    public int FurtherUndoCount { get; set; }

    public EquationAction(ISupportsUndo executor)
        Executor = executor;
        UndoFlag = true;

The UndoFlag of EquationAction allows the callee to figure out whether something should be added or removed from the equation tree (i.e. this tells the direction of the undo/redo process!

The good news is that, thanks to our robust OO model, despite all the complexity of the undo stack, only a handful of major equations should be able to manager all undo/redo actions on behalf of every euqation, present or future! In fact, only the following three major equation classes implement the ISupportsUndo interface:

  • EquationRow
  • RowContainer
  • TextEquation

Beware though, the actual implmentation of Undo/Redo stack inside these classes is no less than a nightmare, as they have to remember the whole equation tree, as it is built and modified, all the time!


Let's summarize what we tried to convey:

  • Object Oriented Design process is not a strictly defined set of principles or practices. It is more of an art than science.
  • The ability to find common characteristics and behavior in the entities forming a model is the key to good Object Oriented design. 
  • Abstraction is the cornerstone of flexible and extensible OO model.
  • Defining objects and relationships when the actual model to be represented is rather abstract is more challenging.

Please let me know if you find anything missing or unclear. You feedback will definitely help me make my work better and more useful. 


This article, along with any associated source code and files, is licensed under The MIT License

Written By
Technical Lead
I am a full-stack developer. My skills include JavaScript, C#/.Net, MS Azure cloud etc. I love to work on complex programming tasks requiring deep analysis, planning and use of efficient algorithms and data structures.

Comments and Discussions

GeneralRe: Full version source code Pin
Kashif_Imran20-Mar-13 4:55
Kashif_Imran20-Mar-13 4:55 
GeneralRe: Full version source code Pin
AlexeyRozhkov21-Mar-13 0:36
AlexeyRozhkov21-Mar-13 0:36 
GeneralRe: Full version source code Pin
Kashif_Imran21-Mar-13 1:09
Kashif_Imran21-Mar-13 1:09 
AnswerRe: Full version source code Pin
Kashif_Imran2-Oct-14 23:24
Kashif_Imran2-Oct-14 23:24 
QuestionGreat Article Pin
Andy_L_J19-Mar-13 20:22
Andy_L_J19-Mar-13 20:22 
AnswerRe: Great Article Pin
Kashif_Imran19-Mar-13 20:24
Kashif_Imran19-Mar-13 20:24 
Questionmy vote of 5 Pin
Shabana Parveen14-Jan-13 22:35
professionalShabana Parveen14-Jan-13 22:35 
AnswerRe: my vote of 5 Pin
Kashif_Imran15-Jan-13 2:08
Kashif_Imran15-Jan-13 2:08 
QuestionMy Vote of 5 Pin
Waqas Ahmad Abbasi8-Jan-13 19:07
Waqas Ahmad Abbasi8-Jan-13 19:07 
AnswerRe: My Vote of 5 Pin
Akinmade Bond21-Apr-13 3:48
professionalAkinmade Bond21-Apr-13 3:48 
GeneralRe: My Vote of 5 Pin
Kashif_Imran21-Apr-13 4:05
Kashif_Imran21-Apr-13 4:05 
QuestionGood job man Pin
Sacha Barber7-Jan-13 6:08
Sacha Barber7-Jan-13 6:08 
AnswerRe: Good job man Pin
Kashif_Imran7-Jan-13 9:14
Kashif_Imran7-Jan-13 9:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.