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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:23:15+00:00 2026-05-20T18:23:15+00:00

I have a: class ServiceObject { ServiceClass svcClass; void execute(String operation, Map arguments, …)

  • 0

I have a:

class ServiceObject {

    ServiceClass svcClass;

    void execute(String operation, Map arguments, ...) {
        svcClass.execute(this, operation, arguments, ...);
    }

}

Then I have a subclass of that to add in synchronization:

class SynchronizedServiceObject extends ServiceObject {

    void execute(String operation, Map arguments, ...) {
        synchronized(lock) {
            super.execute(operation, arguments, ...);
        }
    }

}

I also have a subclass to add a logging context

class LogContextServiceObject extends ServiceObject {

    void execute(String operation, Map arguments, ...) {
        MDC.set("context", myCtx);
        super.execute(operation, arguments, ...);
        MDC.remove("context");
    }

}

However, if I want to use both features, I would need to write another subclass which has them both. You can imagine that with more added features (logging, request rewriting, etc.) I would have to write a lot of classes for every combination I need.

Instead, I’d like to ditch the subclasses and pass some sort of filter or component objects at the creation of ServiceObject. What’s a recommended pattern for this (In Java, or in general).

  • 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-20T18:23:16+00:00Added an answer on May 20, 2026 at 6:23 pm

    In terms of famous GoF book: Decorator.

    public class LoggingDecorator implements ServiceObject {
    
        private final ServiceObject decoratee;
    
        public LoggingDecorator(ServiceObject decoratee) {
            this.decoratee = decoratee;
        }
    
    
        @Override
        public void execute(String operation, Map<?, ?> arguments) {
            MDC.set("context", myCtx);
            try {
                decoratee.execute(operation, arguments);
            } finally {
                MDC.remove("context");
            }
        }
    }
    
    public class SynchronizedDecorator implements ServiceObject {
    
        private final ServiceObject decoratee;
    
        private final Object lock = new Object();
    
        public SynchronizedDecorator(ServiceObject decoratee) {
            this.decoratee = decoratee;
        }
    
        @Override
        public void execute(String operation, Map<?, ?> arguments) {
            synchronized (lock) {
                decoratee.execute(operation, arguments);
            }
        }
    }
    

    After all you can combine them as you like:

    ServiceObject serviceObject =
                        new LoggingDecorator(
                                new SynchronizedDecorator(
                                        new ServiceObjectImpl()
                                )
                        );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class like this: public class object { [Key] int number; String mystring;
I have class Fred { public: void inspect() const {}; void modify(){}; }; int
i have class like this DrillDownAppAppDelegate.h PictureCell.h RootViewController.h SlideShowViewController.h DrillDownAppAppDelegate.m PictureCell.m RootViewController.m SlideShowViewController.m i
I have class Map(db.Model): urlHash= db.ByteStringProperty() hasher = hashlib.sha256() hasher.update(staticMapUrl) urlHash = hasher.digest() query
I have class(Customer) which holds more than 200 string variables as property. I'm using
I have class that has a property that is of type string[]. I need
I have class which handles packages: typedef void (*FCPackageHandlerFunction)(FCPackage*); class FCPackageHandlers{ ... void registerHandler(FCPackage::Type
I have class Myadapter extends BaseAdapter.in Myadapter i changed the MyListView data then call
I have class (MyCustomWebView) that extend webview can i do something like this ?
I have class: static class GetRole { public static string OfUser(string username) { string

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.