Observer Pattern (with Executor and JavaFX)

Поделиться
HTML-код
  • Опубликовано: 24 окт 2024

Комментарии • 29

  • @pramodahanya007
    @pramodahanya007 7 лет назад +2

    This is a neat explanation of using observer pattern. Thanks.

  • @josiahtobas9172
    @josiahtobas9172 5 лет назад +1

    Thanks man, I am just now trying to understand how to return data via the observer pattern.

    • @PaulGestwicki
      @PaulGestwicki  5 лет назад +1

      Great, I'm glad the video was helpful. Mastering the observer pattern is a great weekend project!

  • @sicariushahni5366
    @sicariushahni5366 5 лет назад +3

    Thanks, trying to understand how threads work and the observer pattern works >.

  • @maxh_music
    @maxh_music 8 лет назад +6

    "We have to get a little warmed up before we can get to that" - nice pun!

  • @wheelxofxfortune
    @wheelxofxfortune 4 года назад +1

    Fantastic tutorial, thanks!

  • @whoraclee
    @whoraclee 7 лет назад +1

    That was awesome and very helpful. Thank you so much!

  • @rubenb994
    @rubenb994 6 лет назад +1

    Thank you for this great lesson!

  • @splithor1zon
    @splithor1zon 5 лет назад +1

    Thank you mate, you saved my life! :D

  • @JeaneAdix
    @JeaneAdix 5 лет назад

    Thank you for the tutorial i found it very useful.

  • @keithscull8288
    @keithscull8288 6 лет назад +1

    Thank you.
    QUESTION: So for years in Swing GUI programming, it's been unacceptable for a model to import ANYTHING from the view, but in my experience with Swing, the listener interfaces were always in the view.
    So, would TemperatureSensorListener be in the model? And then the ThermometerApplication, (view), would import TemperatureSensorListener from the model? Or potentially retrieve it via a linkage from the controller, and the CONTROLLER would directly import it?
    Is it okay now with javafx for your view to be directly referencing the model like this, or would it be best to supply wrapper methods in a controller class?
    Lastly, even if we kept the TemperatureSensorListener interface in the model, I noticed that if we use a Java 8 Lamda expression to say, add a new TemperatureSensorListener, we don't actually have to import the model.TemperatureSensorListener, therefore this might be the preferred way, since it doesn't have to actually import from the model ? Sorry, it's all very confusing switching from Swing to Javafx for me.. they change a lot of concepts..

    • @PaulGestwicki
      @PaulGestwicki  6 лет назад +1

      The TemperatureSensorListener is in the model as the agent that allows other objects to hear about changes without the model's needing an explicit dependency upon them. Without it, the model would have to tell the view directly that the model has changed, but that's exactly the dependency that we're inverting with this design pattern. My example uses a separable model architecture, not MVC, so there is no controller: it gives a more pure example not to have to deal with a controller.

    • @PaulGestwicki
      @PaulGestwicki  6 лет назад +2

      Just saw the second part of your question. Lambdas don't fundamentally change this pattern; they just make it easier to type. You can still do observer or publish/subscribe with lambdas just with less plumbing.
      Using a separable model architecture, your view _has_ to depend on your model if it's going to act as a listener. You can do this with JavaFX too if you want, for example if you skip using FXML or SceneBuilder and simply build the GUI programmatically as you would in Swing.
      Switching to MVC or MVP architecture's doesn't really significantly change the design pattern. Observer still shows up everywhere, it's just a question of which agent watches which, inevitably requiring more plumbing the more indirection you add. That's why I like separable model for a lot of my work, for what it's worth: it's often the simplest solution that maintains the Single Responsibility Principle, although it comes with its own tradeoffs of course. Of course, do a search for a definition of "MVC" and you rarely get the same answer twice, which is why I like to avoid that acronym altogether :)

    • @keithscull8288
      @keithscull8288 6 лет назад

      Thanks so much for answering, and TWICE! Subscribed.

  • @daviddave776
    @daviddave776 8 лет назад

    okay one last question
    is this working ?
    private final List listeners = new ArrayList();
    you say ....Lists.new ArrayList ...but Liists is not working for me :-/ it doesnt exist and i cant import it :-/

  • @amrabdulaziz2253
    @amrabdulaziz2253 8 лет назад

    very thanks =D

  • @eugeniogonzato
    @eugeniogonzato 8 лет назад

    This is a very helpfool example. Thanks very much. Can you shere this file example ?

    • @PaulGestwicki
      @PaulGestwicki  8 лет назад

      +Eugenio Gonzato Thanks! I don't actually have these files any more, but it's all shown on the screen---nothing else to it.

  • @daviddave776
    @daviddave776 8 лет назад

    wow the Tutorial is awesome !!!! the best so far !!
    BUT it doesnt work for me =(
    I work with JavaFX and Scenebuilder so things are a bit different. I want to start everything with a click on a button. Which method should I call? :-/
    I dont just decrease a value. I have got a big method with a calculation and i need to display the result of it. Should i call this method in the constructor instead of your currentReading- - ;
    it would be awesome if you could help me !! *_*

    • @PaulGestwicki
      @PaulGestwicki  8 лет назад

      The fundamental approach is the same: push the work off of the event thread and onto a worker thread. Then, when the work is done, have that thread post notification back to the event thread, using that Platform.runLater method.

    • @daviddave776
      @daviddave776 8 лет назад

      i try but i dont get it :( normally i call my calculation method with my button click but i dont know what to do know. and i dont know if i have to call something in the start method. i think thats to difficult to me. but thank you !!

    • @daviddave776
      @daviddave776 8 лет назад

      it would be perfect if you could take a look stackoverflow.com/questions/37820792/problems-with-observer-and-threads?noredirect=1#comment63109291_37820792 *_*