Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8788429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:08:31+00:00 2026-06-13T22:08:31+00:00

Introduction I am trying to make a rather complex structure in Java with interfaces,

  • 0

Introduction

I am trying to make a rather complex structure in Java with interfaces, abstract classes and generics. Having no experience with generics and only average experience with creating good OOP designs, this is beginning to prove quite a challenge.

I have some feeling that what I’m trying to do cannot actually be done, but that I could come close enough to it. I’ll try to explain it as brief as I can. I’m just going to tell straight away that this structure will represent my DAO and service layers to access the database. Making this question more abstract would only make it more difficult.

My DAO layer is completely fine as it is. There is a generic DAO interface and for each entity, there is a DAO interface that extends the generic one and fills in the generic types. Then there’s an abstract class that is extended by each DAO implementation, which in turn implement the corresponding interface. Confusing read for most probably, so here’s the diagram showing the DAO for Products as an example:

Diagram showing the DAO implementation for Product entities

Now for the service classes, I had a similar construction in mind. Most of the methods in a service class map to the DAO methods anyway. If you replace every “DAO” in the diagram above with “Service”, you get the basis for my service layer. But there is one thing that I want to do, based on the following idea I have:

Every service class for an entity will at least access one DAO object, namely the DAO of the entity that it is designed for.

Which is…

The question/problem

If I could make a proper OO design to make each service class have one instance variable for the DAO object of their respective entity my service layer would be perfect, in my view. Advice on this is welcome, in case my design is not so good as it seemed.

I have implemented it like this:

Class AbstractService

public abstract class AbstractService<EntityDAO> {

    EntityDAO entityDAO;

    public AbstractService() {
        entityDAO = makeEntityDAO(); //compiler/IDE warning: overridable method call in constructor
    }

    abstract EntityDAO makeEntityDAO();
}

Class ProductServiceImpl

public class ProductServiceImpl extends AbstractService<ProductDAOImpl> {

    public ProductServiceImpl() {
        super();
    }

    @Override
    ProductDAOImpl makeEntityDAO() {
        return new ProductDAOImpl();
    }
}

The problem with this design is a compiler warning I don’t like: it has an overridable method call in the constructor (see the comment). Now it is designed to be overridable, in fact I enforce it to make sure that each service class has a reference to the corresponding DAO. Is this the best thing I can do?

I have done my absolute best to include everything you might need and only what you need for this question. All I have to say now is, comments are welcome and extensive answers even more, thanks for taking your time to read.

Additional resources on StackOverflow

Understanding Service and DAO layers

DAO and Service layers (JPA/Hibernate + Spring)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:08:33+00:00Added an answer on June 13, 2026 at 10:08 pm

    Just a little note first: usually in an application organized in layers like Presentation / Service / DAO for example, you have the following rules:

    • Each layer knows only the layer immediately below.
    • It knows it only by it’s interfaces, and not by it’s implementation class.

    This will provide easier testing, a better code encapsulation, and a sharper definition of the different layers (through interfaces that are easily identified as public API)

    That said, there is a very common way to handle that kind of situation in a way that allow the most flexibility: dependency injection. And Spring is the industry standard implementation of dependency injection (and of a lot of other things)

    The idea (in short) is that your service will know that it needs a IEntityDAO, and that someone will inject in it and implementation of the interface before actually using the service. That someone is called an IOC container (Inversion of Control container). It can be Spring, and what it does is usually described by an application configuration file and will be done at application startup.

    Important Note: The concept is brilliant and powerful but dead simple stupid. You can also use the Inversion of Control architectural pattern without a framework with a very simple implementation consisting in a large static method “assembling” your application parts. But in an industrial context it’s better to have a framework which will allow to inject other things like database connection, web service stub clients, JMS queues, etc…

    Benefits:

    • Your have an easy time mocking and testing, as the only thing a class depends on is interfaces
    • You have a single file of a small set of XML files that describe the whole structure of your application, which is really handy when your application grows.
    • It’s a very widely adopted standard and well – known by many java developers.

    Sample java code:

    public abstract class AbstractService<IEntityDAO> {
    
        private IEntityDAO entityDAO; // you don't know the concrete implementation, maybe it's a mock for testing purpose
    
        public AbstractService() {
        }
    
        protected EntityDAO getEntityDAO() { // only subclasses need this method
        }
    
        public void setEntityDAO(IEntityDAO dao) { // IOC container will call this method
            this.entityDAO = dao;
        }
    }
    

    And in spring configuration file, you will have something like that:

    <bean id="ProductDAO" class="com.company.dao.ProductDAO" />
    
    [...]
    
    <bean id="ProductService" class="com.company.service.ProductService">
        <property name="entityDAO" ref="ProductDAO"/>
    </bean>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Introduction : I have prior experience in programming (C, C++, Java), however, this is
I'm trying to learn redcode, because it looks fun to make a bot. Introduction
I'm trying to make this: <span class=introduction> <img alt=image src=/picture.jpg /> </span> transform into
I'm having an enforced introduction to idl trying to debug some old code. I
Introduction: I'm trying to get additional fields to log with log4j, and its working
I am trying to do an introduction page for a website. The trick is,
I'm trying to implement the example outlined here: http://www.codeproject.com/Articles/30994/Introduction-to-WPF-Templates The author states The ContentPresenter
Introduction: I want to loop through XML files with flexible categories structure. Problem: I
I'm trying to find a good, simple introduction to domain driven design, but that
Introduction I am trying to use P/Invoke to register a struct of callbacks with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.