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

  • SEARCH
  • Home
  • 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 9220005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:14:51+00:00 2026-06-18T03:14:51+00:00

I am a novice programmer learning how to design API for my module. I

  • 0

I am a novice programmer learning how to design API for my module. I thought of 2 types of service class to provide a API for other classes to use.

First example handles the logic by parameters, the second example handles by object way.

Which is a better approach / design for me to provide business methods for other classes to use?

As a general rule of thumb which should I use?

Example 1 – Service A

public class ServiceA {

    private SampleDAO dao =  new SampleDAO();
    private static final String DRAFT_STATUS = "DRAFT";
    private static final String APPROVED_STATUS = "APPROVED";
    private static final String SUBMITTED_STATUS = "SUBMITTED";


    public boolean isDocumentApprove(String documentId) {
        Document doc = getDocument(documentId);

        return (APPROVED_STATUS.equals(doc.getStatus()));
    }

    public boolean isDocumentDraft(String documentId) {
        Document doc = getDocument(documentId);

        return (DRAFT_STATUS.equals(doc.getStatus()));
    }

    public boolean isDocumentSubmited(String documentId) {
        Document doc = getDocument(documentId);

        return (SUBMITTED_STATUS.equals(doc.getStatus()));
    }


    private Document getDocument(String documentId) {
        return (dao.getByDocumentId(documentId));
    }
}

Example 2 – Service B

public class ServiceB {

    private SampleDAO dao =  new SampleDAO();
    private static final String DRAFT_STATUS = "DRAFT";
    private static final String APPROVED_STATUS = "APPROVED";
    private static final String SUBMITTED_STATUS = "SUBMITTED";


    public Document getDocument(String documentId) {
        return (dao.getByDocumentId(documentId));
    }

    public boolean isDocumentApprove(Document doc) {    
        return (APPROVED_STATUS.equals(doc.getStatus()));
    }

    public boolean isDocumentDraft(Document doc) {
        return (DRAFT_STATUS.equals(doc.getStatus()));
    }

    public boolean isDocumentSubmited(Document doc) {   
        return (SUBMITTED_STATUS.equals(doc.getStatus()));
    }

}
  • 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-18T03:14:52+00:00Added an answer on June 18, 2026 at 3:14 am

    If I had to choose, i would pick the the second example, because it contains less code duplication. In the first one, there is the same Document doc = getDocument(documentId); statement in every method. So, here the general principle is “don’t repeat yourself”.

    Furthermore, in the first version you can pass any garbage as a string to the methods. Although you can pass null in the second version too, but it is easier to check for null than for invalid ids.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

first post here. I'm a novice programmer as you'll see, and I'm learning to
Novice programmer asking first question on stack-overflow. I am writing an app for mac
I am a novice programmer, learning C++. I am doing a computational biology project
I'm an extremely novice programmer who finds himself tasked with learning how to program
As a novice C++ programmer I have always put my classes interface in .h
I am a novice iPhone programmer I want to use some of the functions
I'm a novice programmer with basic Java experience, and currently learning Python. I've stumbled
I am a novice programmer and I recently started learning Algorithm and DS. I
I am a novice programmer in python. I'm currently constructing a class that parses
I'm a very novice programmer. I'm trying to use the combinations tool in the

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.