Why a Good Code Structure Matters and How to Design It: Models, Utils, Services, DAO, and Controllers
The article explains why a solid code structure is essential, outlines the principles of single responsibility, reusability and clear definitions, and details the roles of Model, Util, Service, DAO and Controller, while discussing their relevance to Web, Android and iOS development.
Why a good code structure is needed: a clear architecture not only makes the system easier to understand, it acts like a way to decompose and re‑assemble a system.
A good structure reduces friction during code hand‑over, eases multi‑person collaboration, and prevents the chaos that feels like dealing with a pile of waste.
Metaphors such as a lump of excrement or a tangled ball of yarn illustrate how bad code feels; the goal is to turn that mess into a clean, Swiss‑army‑knife‑like design.
What makes a good structure?
Maintain single responsibility.
Be generic and reusable.
Have clear definitions.
These principles are the value that scaffolding frameworks provide; Java, Android, iOS all have well‑defined frameworks, while JavaScript historically lacked one until modern libraries emerged.
1. Model
A Model represents pure data, often mirroring persistent storage such as MySQL or MongoDB. It abstracts a data definition; instances are collections of data that flow through the system. Examples include an Article model (title, summary, author, content) and a Comment model (content, userID).
2. Util
Util classes contain generic, reusable utilities that are unrelated to business logic. They should replace private helper methods that clutter code. A well‑named Util (e.g., ArticleUtil ) has a clear input and output, making it easy to unit‑test.
3. Service
A Service provides a higher‑level operation that may orchestrate multiple Utils or other Services. It represents a callable service: "what do you need, come to me." If a Service starts to contain business logic inside the Model, it becomes a “fat” model, which is discouraged.
4. Dao
Dao (Data Access Object) is responsible solely for communicating with the underlying database—CRUD operations. Best practice: one Service corresponds to one Dao, keeping data access concerns separate from business logic.
5. Controller
The Controller is the entry point that dispatches requests to the appropriate Service, determines which data is needed, and orchestrates the flow. It is a common source of “dirty” code if business logic leaks into it.
Applicability to Web, Android, and iOS: Java back‑ends already have clear layered structures; Android follows MVP/MVC patterns; iOS lacks an official framework but often suffers from ad‑hoc dictionary‑based data handling. JavaScript historically mixed HTML, CSS, and logic, but modern frameworks (Angular, React, Vue) now provide clearer architectures.
Further learning: beyond the basics, one should study system architecture, service decoupling, message queues, and databases like MongoDB. The content is most relevant for engineers with up to two years of experience.
If you enjoyed this article, please continue reading and like it.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.