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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:49:59+00:00 2026-05-23T06:49:59+00:00

I am given this set of code and need to suggest ways to improve

  • 0

I am given this set of code and need to suggest ways to improve the code’s cohesion and coupling of the classes. But I thought these classes are quite well de-coupled since it looks like they are making use of events. And in terms of cohesion, all the init() calls are placed together, everything seems quite alright to me.

public class A
{
    private C t;
    private B g;
    public static void main(String args[]) {
        // Creates t and g.
        t = new C();
        t.init();
        g = new B();
        g.init();
        g.start(this);
    }
    pubic void update (Event e)
    {
        // Performs updating of t based on event
    }
}

public class C
{
    public C() { ... }
    public void init() { ... }
}

public class B
{
    public B() { ... }

    public void init() { ... }

    public void start(A s) {
        e = getNextEvent();
        while (e. type != Quit)
            if (e.type == updateB)
                update(e) ;
            else
                s.update(e) ;
        e = getNextEvent();
    }

    public void update(Event e) { ... }
    }
}

Are there still ways to improve the classes cohesion and coupling? It looks ok to me but I think I am missing something out.

Thanks for any suggestions on this.

  • 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-23T06:50:00+00:00Added an answer on May 23, 2026 at 6:50 am

    One suggestion would be to decouple event handling logic from the controller logic (class A).

    So you would have 4 types of classes:

    • the main class used for running the “server” (A)
    • the thread listening for events (B)
    • the model layer which will be updated (C)
    • an event handler class which will support some operation on an event (D)

    It could look something like this:

    public class Main {
      private Model m;
      private EventListener listener;
    
      ... main() {
        m = new Model();
        listener = new EventListener();
    
        EventHandler eventHandler = new MyEventHandler();
    
        // set the event handler
        listener.setEventHandler(eventHandler);
    
        listener.start(m);
    }
    
    public class Model {
      // nothing to see here
    }
    
    public class EventListener() {
    
      private EventHandler handler = new DefaultEventHandler();
    
      public void start(final Model m) {
        // startup
        while (event.type != Event.Quit) {
          // get event
          handler.handleEvent(event, m);
        }
        // shutdown
      }
    
      public void setEventHandler(EventHandler eh) { this.handler = eh }
    }
    
    public class MyEventHandler implements EventHandler {
    
      public void handleEvent(Event e, Model m) {
        // update the model
      }
    
    }
    

    Note that in this new design the business logic of updating the model (C in your example) has moved to an external class as opposed to the “Runner” class. this is a bit cleaner as the Main class does not need to have knowledge of what events are and how to handle them.

    Another advantage is that using this you can easily code complex event handling using chained event handlers or multiple serial event handlers. It is also quite simple to implement asynchronous event handling as B is only in charge of calling the handlers and doesn’t need to understand event types. This is sometimes referred to as Publish/Subscribe and keeps the listener (B) and the handler (your update(e) method) loosely coupled

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

Sidebar

Related Questions

I'm trying to set up a Backbone application. This code below gives to following
Given this example data set: ----------------------------- | item | date | val | -----------------------------
Given this variable in tcsh: set i = ~/foo/bar.c how can I get just
Given this : delimiter // create procedure setup() begin declare d datetime; set d
Given a data set like this; +-----+---------------------+--------+ | id | date | result |
This is one of the solution of getting true or false from given set
I have been struggling with this for a while now. given a set of
given this code namespace GridTests { public class Grid<T> { IEnumerable<T> DataSource { get;
Given that this field locale have been set for norwegian bokmål and norway: Locale
Given this code ( #stage is an anchor tag with a div element inside

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.