tags:

views:

114

answers:

2

Hi all,

I have a (design of) an application to basically work like this:

class Main, class BusinessLogic, class UserInterface

BusinessLogic and UserInterface are designed to be library-like - not on-goingly developed but used by a developer in say, class Main.

The constructor of BusinessLogic also instantiates UserInterface, which will display a text-based menu and take terminal input. Based upon that input, it will execute the relevant functionality by invoking the relevant method of its respective BusinessLogic object.

However, this is a circular reference, and there's no apparent way for UserInterface to call its respective BusinessLogic object (without the BusinessLogic object identifier hardcoded, but that's even worse practice!).

This does seem a logical design to me, but hard to implement in programming terms.

One alternative I considered is simply having UI return the input data to BusinessLogic's constructor, but I don't want BusinessLogic to handle input analysis, UI seems the sensible place for it.

Any ideas?

+2  A: 

You should try the Model-View-Controller pattern. In your case the model and controller may end up the same. Isolating your classes from each other via interfaces and then creating those outside (from Main in your example) will simplify everyone's life.

Nick Veys
Oh wow, should've thought of that, I know of it :) I suppose the way to best implement that is have: class Main, class BusinessLogic, class UI (view), class Controller (controller) (and database being model), effectively separating data, UI components, input analysis and business logic, right? :)
wiseguy
Well, the database is kind of a model, but there should be a representation of that data as a real model, usable in the application. Objects that represent the problem you are solving. This may be the "BusinessLogic" portion you speak of.
Nick Veys
A: 

This sounds like you may want to use an Model-View-Controller pattern.

In addition, if you want to reduce the instantiation dependencies between the parts, you may want to consider using dependency-injection, as with Spring.

Uri