{"id":2052,"date":"2026-01-12T09:21:24","date_gmt":"2026-01-11T20:21:24","guid":{"rendered":"https:\/\/www.ronella.xyz\/?p=2052"},"modified":"2026-01-12T09:21:24","modified_gmt":"2026-01-11T20:21:24","slug":"data-oriented-programming-in-modern-java","status":"publish","type":"post","link":"https:\/\/www.ronella.xyz\/?p=2052","title":{"rendered":"Data-Oriented Programming in Modern Java"},"content":{"rendered":"<p>Data-oriented programming (DOP) in Java emphasizes immutable data structures separated from business logic, leveraging modern features like records, sealed interfaces, and pattern matching for safer, more maintainable code.<\/p>\n<h2>Core Principles of DOP<\/h2>\n<p>DOP models data transparently using plain structures that fully represent domain concepts without hidden behavior or mutable state. Key rules include making data immutable, explicitly modeling all variants with sealed types, preventing illegal states at the type level, and handling validation at boundaries.<\/p>\n<p>This contrasts with traditional OOP by keeping data passive and logic in pure functions, improving testability and reducing coupling.<\/p>\n<h2>Java's Support for DOP<\/h2>\n<p>Records provide concise, immutable data carriers with built-in equality and toString. Sealed interfaces define closed hierarchies for exhaustive handling, while pattern matching in switch and instanceof enables declarative operations on variants.<\/p>\n<p>These features combine to enforce exhaustiveness at compile time, eliminating visitor patterns or runtime checks.<\/p>\n<h2>Practical Example: Geometric Shapes<\/h2>\n<p>Model 2D shapes to compute centers, showcasing DOP in action.<\/p>\n<pre><code class=\"language-java\">sealed interface Shape permits Circle, Rectangle, Triangle {}\n\nrecord Point(double x, double y) {}\n\nrecord Circle(Point center, double radius) implements Shape {}\n\nrecord Rectangle(Point topLeft, Point bottomRight) implements Shape {}\n\nrecord Triangle(Point p1, Point p2, Point p3) implements Shape {}<\/code><\/pre>\n<p>Operations remain separate and pure:<\/p>\n<pre><code class=\"language-java\">public static Point centerOf(Shape shape) {\n    return switch (shape) {\n        case Circle c -&gt; c.center();\n        case Rectangle r -&gt; new Point(\n            (r.topLeft().x() + r.bottomRight().x()) \/ 2.0,\n            (r.topLeft().y() + r.bottomRight().y()) \/ 2.0\n        );\n        case Triangle t -&gt; new Point(\n            (t.p1().x() + t.p2().x() + t.p3().x()) \/ 3.0,\n            (t.p1().y() + t.p2().y() + t.p3().y()) \/ 3.0\n        );\n    };\n}<\/code><\/pre>\n<p>The sealed interface ensures exhaustive coverage, records keep data transparent, and the function is stateless.<\/p>\n<h2>DOP vs. Traditional OOP<\/h2>\n<table>\n<thead>\n<tr>\n<th>Aspect<\/th>\n<th>DOP in Java<\/th>\n<th>Traditional OOP<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Data<\/td>\n<td>Immutable records, sealed variants<\/td>\n<td>Mutable objects with fields\/methods<\/td>\n<\/tr>\n<tr>\n<td>Behavior<\/td>\n<td>Separate pure functions<\/td>\n<td>Embedded in classes<\/td>\n<\/tr>\n<tr>\n<td>State Handling<\/td>\n<td>None; inputs \u2192 outputs<\/td>\n<td>Mutable instance state<\/td>\n<\/tr>\n<tr>\n<td>Safety<\/td>\n<td>Compile-time exhaustiveness<\/td>\n<td>Runtime polymorphism\/overrides<\/td>\n<\/tr>\n<tr>\n<td>Testing<\/td>\n<td>Easy unit tests on functions<\/td>\n<td>Mocking object interactions<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>DOP shines in APIs, events, and rules engines by prioritizing data flow over object lifecycles.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Data-oriented programming (DOP) in Java emphasizes immutable data structures separated from business logic, leveraging modern features like records, sealed interfaces, and pattern matching for safer, more maintainable code. Core Principles of DOP DOP models data transparently using plain structures that fully represent domain concepts without hidden behavior or mutable state. Key rules include making data [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/2052"}],"collection":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2052"}],"version-history":[{"count":1,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/2052\/revisions"}],"predecessor-version":[{"id":2053,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/2052\/revisions\/2053"}],"wp:attachment":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}