What is Java domain model?

房东的猫 提交于 2019-11-27 12:39:31

A domain model (the term is not at all Java specific) is a class that models something in the problem domain, as opposed to a class that exists for technical implementation reasons.

Domain model instances often need to be persisted in a database, and in Java, they typically conform to the Java Beans specification, i.e. they have get and set methods to represent individual properties and a parameterless constructor. Spring and other frameworks allow you to access these properties directly in your JSPs.

For example, in a shop application, some of your domain model classes would be Product, Order, ShoppingCart and Customer.

A Domain model is a conceptual model of the problem domain. By "java domain model" they just mean the java classes representing that model. There's nothing specific to java in the concept.

See also Domain Driven Design for an approach to focusing your development on the business domain needs.

okaygo

Michael Borgwardt's answer "A domain model (the term is not at all Java specific) is a class" is wrong. I am very surprised so many agree with that answer.

A domain model is all the classes that model the behavior of the solution. It is the minimum necessary to accomplish the required behavior. The domain model is free of UI and persistence functionality (unless the problem revolves around UI or persistence).

I have seen domain model implemented in one class but that is not the design of an object-oriented solution. In an object-oriented domain model, each concept has its own class that implements the behavior required of that concept and contains the necessary fields to maintain the state of the class.

Let’s start with an example. You are creating an application with will be used by some people in your locality. When design the system you call these people users of your system. You also have to manage a list of roles for these people in the system and authentication information. So, you decide to create a conceptual entity in the system. This conceptual entity is further mapped to a User object in you software solution (your application). Now when you represent your application, you describe that User object as a Domain Model. The basic idea behind this term is that only. You can further read about it in the following Wikipedia link.

I know it has been a long time since the last post here. But it is important that information around this concept is clear. A domain model is often a set of classes which represent a particular problem domain. The concept is not tied to any one type of technology implementation. I think it is a little misleading to say :

"Domain model instances often need to be persisted in a database, and in Java, they typically conform to the Java Beans specification, i.e. they have get and set methods to represent individual properties and a parameter-less constructor. Spring and other frameworks allow you to access these properties directly in your JSPs"

Domain models are often the result of domain driven design. Domain driven design is teh key to a good and robust domain model. I suggest having a read through Eric Evans' book Domain Driven Design, to give you a better understanding.

Domain model classes do have information associated to them but behavior in my opinion, is more important than data in this context. A big mistake around domain driven design is to create data classes that represent the data of a domain entity ,such as customer and provide only public getters and setters for the customer attributes. These object tend just to mimic your database structure and as a result the actual business logic is more likely to reside in domain services , resulting in a anemic domain model. This model is closer to a Transaction Script than a domain model.

In layman's terms, your domain package is the object representation of elememts you would primarily but not necessarily need to render UI fields like username, client_info, solution specific pojos etc. The classes contained in the domain package will be used in the DAO(DATA ACCESS OBECT) package, where the dao will query the database and map these fields in the classes in domain and return them. Or you could use a framework like hibernate and return this object, where querying the DB will be taken care by a framework like hibernate.

This answer is more MVC and java specific but details one of the possible implementations.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!