0

I need some advice about planning the application's architecture. I want to upgrade my console version of Chat Application to JavaFX. So I started planning. Now I have one main class to manage client and one to manage server. And the problem appears at first glance. I want to make login/register window where "I" will connect to the server and validate data from input fields such as login etc. And when everything is correct I want to pass my already created connection (socket, streams, etc.) to the main chat window. How do I do that in the CORRECT way? I mean how to manage one class Client through multiple windows? When/Where instantiate the Client object?

ofca1234
  • 53
  • 7
  • In fact ,it is hard to define an architecture for any project,but as fa as i know ,when your ServerSocket receive a new connection you will handle this socket from Client class controller which contains all methods of client socket.Don't forget to use Platfrom.runLater() to skip the freezing of your GUI. – Menai Ala Eddine - Aladdin Mar 25 '18 at 14:38
  • Use an MVC-type approach and put the shared data in a model. Then share a single instance of the model with all the controllers. See, perhaps https://stackoverflow.com/q/32342864/. You probably want a "service layer" managing the connection, etc; the model will have a dependency on the service. There's a [nice article by Adam Bien](http://www.oracle.com/technetwork/articles/java/javafxinteg-2062777.html) on approaches to that. – James_D Mar 25 '18 at 14:38

2 Answers2

0

I did it this way:

//loading main chat window fxml
FXMLLoader loader = new FXMLLoader(ClientMain.class.getResource("your_path.fxml"));
Parent mainSceneFXML = loader.load();
//getting controller object
MainController ctrl = (MainController)(loader.getController());

Now you can access needed attributes or methods from ctrl. Replace MainController with classname of your controller of main chat window.

Powercoder
  • 695
  • 1
  • 5
  • 25
0

Because you said :

I need some advice about planning the application's architecture

There are many patterns for JavaFx applications such as abstract MVC(Model-View-Controller),MVVM(Model-View-View-Model),MVC with Service layer,MVP(Model-View-Presenter) and the choice of pattern or the architecture depends on your project and what do you want achieve.

Based on @James_D comment ,it is better to use MVC model with Service layer (if you need Service Layer to manage your connection) .I made this schema for you to describe aproxiamtly the benefits of this pattern: Schema description

Now for client login you can validate your data in Login controller by checking client inputs and make your test (true or false) (it must having a stored data ) to manage many clients because if you use static test if(userNameField.getText()=="Ala Eddine") in this state you will handle many clients with same shared data after that you can show Dialog if iputs are false ..etc.

In your ServerController when you launch your server create a loop that recieve each time a new connection and in this state you have two choice :

  1. Handle client in ServerContoller by creating Inner classes

  2. Add ServiceImp Class to handle clients

For MVC model ,you should respect relationships OneToOne and OneToMany... etc between your models.Your model depends on real life,for example:

  1. Father has many children
  2. Child has one father

In your example

  1. Server has many Clients.
  2. Client has many Messages
  3. Message has content,attachments,date as properties..

Now you can see aproxiamtly the tree of packages :

enter image description here

In the end you can see the communication between controllers :

enter image description here

  • It's generally considered to be a bad idea to communicate directly between the different controllers, though, because it introduces dependencies between them. The controllers should communicate with a shared model, both updating the model and observing (and reacting to changes in) its properties. – James_D Mar 25 '18 at 16:37
  • So in this case we need to add a Singleton class to create only instance for the whole project. – Menai Ala Eddine - Aladdin Mar 25 '18 at 16:49
  • I don't recommend using a formal singleton (in the sense of the Gang of Four Singleton Pattern), but just create an instance of the model that is shared between all the controllers (DI frameworks become really useful for this). – James_D Mar 25 '18 at 17:06
  • Okey i will improve my answer according to your recomendation.Thank you @James_D. – Menai Ala Eddine - Aladdin Mar 25 '18 at 17:10
  • @James_D I tried to edit my answer to add DI example but i'm confused because i saw that DI are used most of time in MVP pattern ,please can you edit or post other answer for DI in MVC model.Thank you. – Menai Ala Eddine - Aladdin Mar 25 '18 at 18:34
  • Thank you so much guys. It gave me a fresh sight at my problem. Later I will post what I came up with, and I would ask you to take a look at my approach. – ofca1234 Mar 26 '18 at 12:46