Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,143,324 members, 7,780,806 topics. Date: Thursday, 28 March 2024 at 10:43 PM

A Simple Signals And Slots Events Mechanism In Java - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / A Simple Signals And Slots Events Mechanism In Java (2335 Views)

How To Sort Array In Java With User Defined Method ? / Question On Regular Expressions In Java / Network Programming In Java (UDP And TCP) (2) (3) (4)

(1) (Reply) (Go Down)

A Simple Signals And Slots Events Mechanism In Java by Olyboy16(m): 4:35pm On Aug 11, 2017
Okay so I was working on one monstrous java project like that where I had to design interactive software for Truss modeling and analysis (Mech. and Civil Engr. Stuff); and I had designed the canvas API very separately from the main logic classes.

Like a dam tearing up, i started facing issues receiving feedback from the canvas API as the App logic has been separated. Even the UI is independent of the core analysis and calculations. So to tie everything together became an issue and I had to start creating silly convenience variables and methods here and there…it’s kinda difficult to explain.

So, It just came into my mind that I might be better off finding a lasting solution; and hence the reason for this thread. So the news is I designed a mini Signals and Slot mechanism in java. if you don't know what Signals and Slots means, please just google this "qt signals and slot tutorial".

I posted it here cos I thought it’s gonna interest some java geeks working on large projects, Especially UI projects. And possibly also trigger some discussions and criticisms which is why I love NL!
And yea, i know java has Signal class but have you tried using it? duhh...

Please check the codes here https://github.com/Olagsfark/JSigSlot

And you may give feedbacks on this thread.

Here's a pretty basic usage sample


import core.SignalSlot;

/**
* See that the two classes didn't know each other, yet they could easily communicate?
* @author ADEDAMOLA
*/
public class ConsoleTest {
public static void main(String[] args){
new ConsoleTest();
FooMan.foo();
}

public ConsoleTest(){
SignalSlot.addSlot("do", slot -> doSomething((String)slot));
}
private void doSomething(String foo){
foo = (foo == null)? "Duhh": foo.concat(foo == "java"? " Rocks!!!" : " Sucks!!"wink;
System.out.println(foo);
}
}

class FooMan{
public static void foo(){
String doThis = javax.swing.JOptionPane.showInputDialog("Hello, what would you like to do?"wink;
SignalSlot.emitSignal("do", doThis);
}
}



Now imagine a App logic of a much larger scale, say a UI based app with several multiple widgets.

What do you think?
Re: A Simple Signals And Slots Events Mechanism In Java by Nobody: 4:53pm On Aug 11, 2017
I Wanna Learn From You Guys. I'm A Beginner In Java Though, Still Stuck At Applet
Re: A Simple Signals And Slots Events Mechanism In Java by seunny4lif(m): 5:08pm On Aug 11, 2017
dayo2me:
I Wanna Learn From You Guys. I'm A Beginner In Java Though, Still Stuck At Applet
Applet in 2017?

1 Like

Re: A Simple Signals And Slots Events Mechanism In Java by Nobody: 5:11pm On Aug 11, 2017
seunny4lif:

Applet in 2017?
u mean i should've gone beyond that or what's your stance?
Re: A Simple Signals And Slots Events Mechanism In Java by seunny4lif(m): 5:24pm On Aug 11, 2017
dayo2me:
u mean i should've gone beyond that or what's your stance?

I'm new to Java but I know Applet is old fashion
Re: A Simple Signals And Slots Events Mechanism In Java by Olyboy16(m): 5:33pm On Aug 11, 2017
seunny4lif:


I'm new to Java but I know Applet is old fashion

you can't blame the guy, he's probably following every single step in the E-book he's using.
@dayo2me, to be a programmer..a successful one, you have to be able so see A and try to turn it to B without being told.
You also have to ask a lot of questions, from google, SO, quora...etc.

Also, try not to just read programming books and memorize codes, start a real project and work on it, even when you know you don't know much to finish the project, you will be surprised at how fast you will learn while debugging, google issues and trying to make improvements on the project.

So instead of reading code, write codes, even if half of it will come from googling; its not a bad thing to check google for programming problems, its actually a sign that you're getting better.
Re: A Simple Signals And Slots Events Mechanism In Java by seunny4lif(m): 6:37pm On Aug 11, 2017
Olyboy16:


you can't blame the guy, he's probably following every single step in the E-book he's using.
@dayo2me, to be a programmer..a successful one, you have to be able so see A and try to turn it to B without being told.
You also have to ask a lot of questions, from google, SO, quora...etc.

Also, try not to just read programming books and memorize codes, start a real project and work on it, even when you know you don't know much to finish the project, you will be surprised at how fast you will learn while debugging, google issues and trying to make improvements on the project.

So instead of reading code, write codes, even if half of it will come from googling; its not a bad thing to check google for programming problems, its actually a sign that you're getting better.
Thats why i prefer Youtube to Ebook
E-books for testing my ability but You-tube to learn
I'm learning Android development with JAVA for now later maybe to Kotlin
cos Kotlin is the new kid in Town
Re: A Simple Signals And Slots Events Mechanism In Java by Olyboy16(m): 8:25pm On Aug 11, 2017
seunny4lif:

Thats why i prefer Youtube to Ebook
E-books for testing my ability but You-tube to learn
I'm learning Android development with JAVA for now later maybe to Kotlin
cos Kotlin is the new kid in Town
cool bro. i've also coded kotlin, its really great and all but way too slow for me.
until speed improves, java still rocks...i'm anxiously waiting for JDK 1.9
Re: A Simple Signals And Slots Events Mechanism In Java by seunny4lif(m): 8:39pm On Aug 11, 2017
Olyboy16:

cool bro. i've also coded kotlin, its really great and all but way too slow for me.
until speed improves, java still rocks...i'm anxiously waiting for JDK 1.9
Many people are complaining about Kotlin too slow
Koltin just make code more short but i prefer Java
Java makes code more easy to read than Kotlin
Re: A Simple Signals And Slots Events Mechanism In Java by Nobody: 12:10am On Aug 12, 2017
Olyboy16:


you can't blame the guy, he's probably following every single step in the E-book he's using.
@dayo2me, to be a programmer..a successful one, you have to be able so see A and try to turn it to B without being told.
You also have to ask a lot of questions, from google, SO, quora...etc.

Also, try not to just read programming books and memorize codes, start a real project and work on it, even when you know you don't know much to finish the project, you will be surprised at how fast you will learn while debugging, google issues and trying to make improvements on the project.

So instead of reading code, write codes, even if half of it will come from googling; its not a bad thing to check google for programming problems, its actually a sign that you're getting better.
wow! thanks for this eye-opening information. i will start as you have said. i have enough programming background already with bucky (thenewboston) videos and deitels ebooks, i think with what you said i will try to put what i have known in to use.
Re: A Simple Signals And Slots Events Mechanism In Java by Nobody: 12:13am On Aug 12, 2017
seunny4lif:


I'm new to Java but I know Applet is old fashion
tell me the new version that can be used with xml if not java applet.
Re: A Simple Signals And Slots Events Mechanism In Java by seunny4lif(m): 12:20am On Aug 12, 2017
dayo2me:
tell me the new version that can be used with xml if not java applet.
You are going to Android Develop?
bucky video about Java are around 2009 which are long ago
https://www.youtube.com/playlist?list=PLS1QulWo1RIbfTjQvTdj8Y6yyq4R7g-Al
Re: A Simple Signals And Slots Events Mechanism In Java by Nobody: 12:39am On Aug 12, 2017
seunny4lif:

You are going to Android Develop?
bucky video about Java are around 2009 which are long ago
https://www.youtube.com/playlist?list=PLS1QulWo1RIbfTjQvTdj8Y6yyq4R7g-Al
yes very soon, probably i will start next month. his java tutorials are 2009 but his android tutorials are 2014.
Re: A Simple Signals And Slots Events Mechanism In Java by seunny4lif(m): 12:56am On Aug 12, 2017
dayo2me:
yes very soon, probably i will start next month. his java tutorials are 2009 but his android tutorials are 2014.
Hmmmm
Me too still dey learn
Re: A Simple Signals And Slots Events Mechanism In Java by Nobody: 1:19am On Aug 12, 2017
seunny4lif:

Hmmmm
Me too still dey learn
you must have been well grounded in the GUI, right?
Re: A Simple Signals And Slots Events Mechanism In Java by Desyner: 2:18am On Aug 12, 2017
Which one be mini signal n slot again OP? What's the difference between Signal n slot and observer pattern. As for me observer pattern still gets things done.
Re: A Simple Signals And Slots Events Mechanism In Java by Olyboy16(m): 8:20am On Aug 12, 2017
Desyner:
Which one be mini signal n slot again OP? What's the difference between Signal n slot and observer pattern. As for me observer pattern still gets things done.
if you write a lot of java code and you come from a Qt/c++ background, you'll appreciate the power of a signals slot mechanism.

observer pattern is no different from object binding method java uses for its event handling. it sucks in a project thats composed of many seperate parts with multiple developers. the signal slots allows parts of an application to work and communicate without any property binding or adding listeners to any observable.
this means on a project with teams, team A doesnt even have to see team B's codes to trigger events in their part of the app. using observables would require access to both parts by both teams, and trust me, the result usually gets dirty.
Re: A Simple Signals And Slots Events Mechanism In Java by Olyboy16(m): 8:26am On Aug 12, 2017
Desyner:
Which one be mini signal n slot again OP? What's the difference between Signal n slot and observer pattern. As for me observer pattern still gets things done.
if you try using your imagination and play around with the idea, you'll get how important signals and slots is
Re: A Simple Signals And Slots Events Mechanism In Java by Olyboy16(m): 8:27am On Aug 12, 2017
.cleared
Re: A Simple Signals And Slots Events Mechanism In Java by Nobody: 7:00pm On Aug 12, 2017
Olyboy16:

if you try using your imagination and play around with the idea, you'll get how important signals and slots is

Actually Signal and Slots seems almost the same as Publish and Observer, I know the Qt Platform for desktop uses such, I had a similar usage with this in Processing, just that then I had no idea it was called Signal and Slot, you that the Canvas is an Immediate Mode which means items are drawn and drawn over repeatedly with nothing in memory unlike the retained mode, so the Signal/Slot method is very cool, another benefit is decoupling, one simply just emits the event and the listener picks up and respond, however I would have love to see how you handle the issue of multiple listeners and who takes the priority first.

Good work it's a threads like this that make me happy, finding a usage for a pattern in a real life scenario
Re: A Simple Signals And Slots Events Mechanism In Java by Olyboy16(m): 12:34am On Aug 13, 2017
pcguru1:


Actually Signal and Slots seems almost the same as Publish and Observer, I know the Qt Platform for desktop uses such, I had a similar usage with this in Processing, just that then I had no idea it was called Signal and Slot, you that the Canvas is an Immediate Mode which means items are drawn and drawn over repeatedly with nothing in memory unlike the retained mode, so the Signal/Slot method is very cool, another benefit is decoupling, one simply just emits the event and the listener picks up and respond, however I would have love to see how you handle the issue of multiple listeners and who takes the priority first.

Good work it's a threads like this that make me happy, finding a usage for a pattern in a real life scenario

its good to know we still have programmers on NL.
Multiple slots can be registered on the same signal ID, much like multiple slots can be connected to a signal in Qt.

For now, each slot simply gets polled(works for my current need); may include ability to stop signal propagation and set slot priorities later.
The cool part is when signals get emitted, all slots polled under the signal ID are executed in parallel; now this may affect single threaded UI libraries like javafx, but a tiny little tweak in the code will fix this issue, but remove the parallelism feature.

Also in cases where you may have like, 100s of slots being added by multiple instances of the same class with the same signal ID - and you want the signal to execute only one slot at a time, ie. the target slot, you could simply add a predicate as a matching criteria.
Although the predicate thing is mainly useful for my program.

I find the library to be very useful already; making UI feedback has been made very easy with those few pieces of code. and every single piece of the app can be torn apart and rebuilt like a lego toy...

(1) (Reply)

With AI Like GPT4, Is Been A Tech Developer Still Worth. / Seun, Come And See This. / After Learning Programming What Next??

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 46
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.