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 6778511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:16:28+00:00 2026-05-26T16:16:28+00:00

I cannot decide between the following two patterns for eg. saving a dataObject (bean

  • 0

I cannot decide between the following two patterns for eg. saving a dataObject (bean in my case). The two options are:

1st

abstract class DataService {
    protected void save(Object data){
        //persist the data
    }
}
//the service for Project objects
class ProjectService extends DataService {
    public void saveProject(Project prj, Object... args /*other save options*/ ){
        // some preprocessing, checking, validation
        save(prj); //call the method in DataService
        // do postprocessing
    }
}

//calling save
projectService.saveProjec(project, /*some args*/);

2nd

abstract class DataService {
    public void save(Object data){
        if(beforeSave(data)){
            // persist the data
            afterSave(data);
        }
    }
    protected boolean beforeSave(){
        return true;
    }
    protected void afterSave(){
    }
}

//the service for Project objects
class ProjectService extends DataService {
    public initSave(Object... args /*other save options*/ ){
        // store these options in class properties
    }
    @Override
    protected bool beforeSave(Project objectAboutToBeSaved){
        // some preprocessing, checking, validation
        // use class properties set by initSave if needed
        return true;//if we want to continue with the saving procedure
    }

    @Override
    protected bool afterSave(Project savedObject){
        // do postprocessing
        // use class properties set by initSave if needed
    }
}

//calling save
projectService.initSave(/*some args*/);
projectService.save(project);

At the moment we are using the first pattern, but I began to think about moving to the second one, because:

(PROS)

  • better logical separation
  • unified method naming across multiple object types (would permit creation of generic unit tests: eg. initialize each object and its service and call save)

(CONS)

  • bit harder to set up (initSave) – might even have to include a teardown method

The idea came to me from the CakePHP MVC framework, where both the Model and Controller included such callback methods, using which I could really implement some clear business logic.

Right now I’m developing in Java – Spring + Spring Data Graph -(thus the javaish code), but this question can be a quite generic one in my opinion.

Note: the example was given for save, but the same would be for the deletion process too.

  • 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-05-26T16:16:29+00:00Added an answer on May 26, 2026 at 4:16 pm

    Another solution would be to use a strategy pattern and do something like the following.
    We’re using that approach to do pre-persist validation and sometime even calculate (based on other fields) and set some fields of the data object that is to be persisted (e.g. we have a “complete” flag that is updated based on other fields whenever the persist or update one of our entities).

    Your strategy:

    interface SaveStrategy<T> {
      boolean beforeSave(T data);
      void afterSave(T data);
    }
    
    class SomeFancyProjectSaveStrategy implements SaveStrategy<Project> {
    
      public SomeFancyProjectSaveStrategy( /*parameters*/) {
      } 
    
      public boolean beforeSave(Project data) {
         //whatever you like
      }
    
      public void afterSave(Project data) {
         //whatever you like
      }
    }
    

    Your data service:

    class DataService {
      public <T> void save(T data, SaveStrategy<? super T> strategy ){
        if(strategy.beforeSave(data)){
            // persist the data
            strategy.afterSave(data);
        }
      }
    }
    

    Then use them like this:

    SaveStrategy<Project> saveStrategy = new SomeFancyProjectSaveStrategy(someParameters); //could reuse that
    dataService.save( project, saveStrategy); //the service might even be shared for different data objects
    

    Pros:

    • Pre- and Post-Save actions are separated from persisting
    • You can resuse strategies as long as they contain reusable data (like validation rules but no state).
    • You can use a general DataService

    Cons

    • If you need special save logic you might have to maintain at least two classes: the strategy and the special data service
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decide between two ways of instantiating an object & handling any
I am crafting an application and cannot decide whether to use the terms Login/out
I cannot get a two-way bind in WPF to work. I have a string
I would like to invite your considered opinion to help me decide between the
Or decide between a parallel and a sequential operation in general. It is hard
I cannot find any answers on the following issue. On my website I would
I have to choose a platform for our product. I have to decide between
I am making a very simple game, but I cannot decide on how to
json_decode function is not part of PHP 5.1, so I cannot use it. Is
Cannot dump Stack Overflow XML file into SQL Server 2008. Could you please explain

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.