This article explores the relationship between classes, inheritance and object design via the cypher patterns.
In essence all patterns allow the definition of an abstract class which follows three methods of defining an abstract class, it is an entity that can only be derived from another entity. The object is not derived or copied directly but rather is based on a common template shared by all derived instances created. This allows for the definition of more specific object models which are more suitable and functional for solving the problem without having to create duplicate objects. In the next part we discuss the various patterns and their applications throughout our code base and how creating these classes can drastically add functionality to our code base.
For these examples I will use the following example domain classes to show them in action. Let’s define a few different types/objects and see what our code may look like:
The Problem StatementOur application requires two tables to exist, one user and one comment. For our basic testing and validation purposes the tables have not been populated yet with data but they are both required and must have their columns defined:
cypher pattern
The Data StructureThis information was stored using Postgres database with 3 tables (user, id, comment). This could be written in a way similar to below. The difference is that each table maintains its own schema, meaning different columns will be defined based on the domain class definition but all users will share their first_name, last_name and a short description of how they fit into the company in which they work.
cypher pattern
As described previously the domain class for this scenario would be a “User”. It has its own set of database schema since each object requires some unique properties that is defined under its business logic. Next we need an interface for defining a base object (a business concept) that uses these defined properties. Users will most likely extend this generic interface by adding extra properties to support their specializations, as our codebase is evolving. We need to maintain and update these additional properties as we develop more specialized objects. The User object should take this information and combine it with a function that uses it to create a simple object in whatever model this new User subclass would follow, a Comment subclass extending “User”. This user comment object will be added as a newly created column (in the database) alongside the User ID and Comment id. It will not have its first row or all columns populated by default as we will not want that data displayed when its read.
If you were to see this object for the first time it might not seem as complex like the other “User” object