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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:55:46+00:00 2026-05-24T16:55:46+00:00

I have 3 classes: class First { public void SetA(){ … } public void

  • 0

I have 3 classes:

class First {
    public void SetA(){ ... }
    public void SetB(){ ... }
    public void SetC(){ ... }
    public void SetD(){ ... }
    public void SetZ(){ ... }
}

class Second {
    public void SetC(){ ... }
    public void SetD(){ ... }
    public void SetE(){ ... }
    public void SetF(){ ... }
    public void SetX(){ ... }
}

class Third{
    public void SetA(){ ... }
    public void SetB(){ ... }
    public void SetE(){ ... }
    public void SetF(){ ... }
    public void SetY(){ ... }
}

As you can see, I duplicate code in the same methods.
Yesterday I realized that sometimes I would like to add another commend inside each method.
So I am looking for a way to solve both those problems. One solution I thought about is:

  1. Create Interface:

    interface IAllMethods {
            void SetA();
            void SetB();
            void SetC();
            void SetD();
            void SetE();
            void SetF();
            void SetX();
            void SetY();
            void SetZ();
    }
    
  2. Create default implementation:

    class DefaultAllMethods {
            public void SetA(){ ... }
            public void SetB(){ ... }
            public void SetC(){ ... }
            public void SetD(){ ... }
            public void SetE(){ ... }
            public void SetF(){ ... }
            public void SetX(){ ... }
            public void SetY(){ ... }
            public void SetZ(){ ... }
    }
    
  3. Create another implementation using decorator pattern in order to add the extra command:

    class ExtraAllMethods {
            private IAllMethods _allMethods;
            public ExtraAllMethods (IAllMethods allMethods) {
                _allMethods=allMethods;
            }
            public void SetA(){ 
                _allMethods.SetA();
                extraMethod();
            }
            public void SetB(){ 
                _allMethods.SetB();
                extraMethod();
            }
            public void SetC(){ 
                _allMethods.SetC();
                extraMethod();
            }
            ..
            ..
            ..
    }
    
  4. Use the desire implementation inside classes First, Second and Third. For example:

    class Third{
        private IAllMethods  _allMethods;
        public Third(IAllMethods allMethods) {
            _allMethods=allMethods;
        }
        public void SetA(){ _allMethods.SetA(); }
        public void SetB(){ _allMethods.SetB(); }
        ..
        ..
        ..
    }
    

What do you think about this solution? Is there any better design to this need?

UPDATE
People ask for the real business, so here is it:
I have 3 types of transmission: TransmissionA, TransmissionB, TransmissionC
Each transmissions has many parameters (members or properties). For example, TransmissionA has WorkerId, CustomerId, MessageName and so on. TransmissionB has WorkerId and MessageName but no CustomerId. TransmissionC has WorkerId, CustomerId but no MessageName. These are example only – in my situation I have many more properties for each transmission. Each property has Set method.
Now there is a new need. Somewhere on the system there is an option called “Update Task”. If the option is ON then I need to update relevant task in each Set method. This is why I thought on decorator pattern.

  • 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-24T16:55:47+00:00Added an answer on May 24, 2026 at 4:55 pm

    this will work but instead of introducing all-knowing interface and base implementation classes containing all methods (and thus having knowledge of the entirety of your collection of methods) you could also go for a more granular approach and just make a single interface and base implementation for each method. That would be more extensible and you could attach those just as easy in a plugin-like fashion if you set it up properly.

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

Sidebar

Related Questions

I have a .py file with lots of classes: class First(Second): #code class Third(Fourth):
Assume we have legacy classes, that can't be modified: class Foo { public void
I have two classes class A { public string something { get; set; }
Say I have three classes: class X{}; class Y{}; class Both : public X,
Let's say i have 2 classes: class Class1 { public: std::vector<CustomClass3*> mVec; public: Class1();
Which static class initializes first if we have one more static classes in our
I have a base class: class CBase { public: virtual void SomeChecks() {} CBase()
I have a project in NetBeans where two different classes have public static void
I have some basic classes with cloning methods: public class SimpleClass { public int
I got 2 classes in my project. The first class(main) have a listview and

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.