Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
// AvgTemp.java
public abstract class AvgTemp {
// This function receives nottification from other Temperature Sensors
public AvgTemp() {
}
public void notifyReceived(String eventName, Object arg) {
if (eventName.equals("temperatureMeasurement"))
{
onNewtemperatureMeasurement((TempStruct) arg);
}
}
public abstract void onNewtemperatureMeasurement(TempStruct tempStruct);
}
For receiving notifications, AvgTemp.java file has to subscribe to a temperature sensor. It means I have to call subscribetemperatureMeasurement().
Now, my question is "Where should I call subscribetemperatureMeasurement() in AvgTemp.java file, so I can get notification from Sensor?"
Should I call subscribetemperatureMeasurement() function in the constructor of the AvgTemp class or in somewhere else?
Looks like your question is missing Sensor skeleton, I guess it looks like this:
public class Sensor {
public void subscribeTemperatureMeasurement(AvgTemp avgTemp) {
//keep avgTemp reference for later use
}
}
and you have a choice between:
public AvgTemp(Sensor sensor) {
sensor.subscribeTemperatureMeasurement(this);
}
or (somewhere outside):
AvtGemp avgTemp = SomeAvgTemp();
sensor.subscribeTemperatureMeasurement(avgTemp);
The former approach has several drawbacks:
introduces unnecessary coupling from AvgTemp to Sensor
what if you want to subscribe to several sensors, you provide first one as a constructor argument and the remaining using the latter approach?
this reference escapes from the constructor, very bad, your notifyReceived might get called before the object is fully initialized (especially because this is an abstract class)
the AvgTemp cannot live without a Sensor which seems to strict and makes testing harder (mocking/stubbing required)
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I made this, basically the user can toggle options to be able to see them or not in the application, i wanted to know how i can improve this class. there are parts like SEARCH & MAP that only change the value in the map and nothing else, would be better to create an abstract class and extend for each type? TypeWithoutToggle (this will only change the value in the map for the type and implement empty #toggle)) TypeWithToggle.. then extend these depending.
public enum ToggleType {
NAME {
#Override
public void toggle(VideoPlayer videoPlayer) {
videoPlayer.doToggleName();
}
},
EDITOR {
#Override
public void toggle(VideoPlayer videoPlayer) {
if (videoPlayer.isTrue("EDITOR"))
videoPlayer.createEditors();
else
videoPlayer.deleteEditors();
}
},
SEARCH {
#Override
public void toggle(VideoPlayer videoPlayer) {
}
},
MAP {
#Override
public void toggle(VideoPlayer videoPlayer) {
// handle on {#link VideoPlayer#create()}
}
},
protected abstract void toggle(VideoPlayer videoPlayer);
public void run(VideoPlayer videoPlayer) {
videoPlayer.toggleMap.put(name(), !videoPlayer.isTrue(name()));
toggle(videoPlayer);
}
You should not expose the toggleMap attribute on VideoPlayer class. Instead the should be encapsulated as a method in VideoPlayer class.
It's difficult to give a more detailed comment only by looking at these few lines.
This question is mostly opinion based, but there are at least two things that I could mention:
First is you are coupling VideoPlayer with ToggleType. There should be a separate method, probably in VideoPlayer (or a VideoPlayerToggler), that is responsible for the toggle that accepts ToggleType.
Example:
switch(toggleType):
case NAME:
doToggleName()
break;
case EDITOR:
if (videoPlayer.isTrue("EDITOR"))
videoPlayer.createEditors();
else
videoPlayer.deleteEditors();
break;
...
Let's assume that you will add a new component that can be toggled, like a MenuComponent. So it might scale better, and decouples your code. With your approach, you would have to add another abstract method, that will handle your type. In short- ToggleType doesn't have to know what or how to toggle. There might be many toggle handlers depending on your environment, configuration or other factors.
Secondly if you put this method inside VideoPlayer, then you don't have to expose any methods, keep everything well encapsulated and private, which is a good idea in general.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
My teacher is convinced that it is convenient to use streams when looping over a list because operations can be parallelized.
I understand that this is true in some ways, but I think that we could always implement a faster code writing it by ourselves.
We are talking here of use cases where we want to optimize as much as we can.
Suppose that we start with the following code:
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
public class HelloWorld{
public static void main (String args[]) {
List<Something> ls = new ArrayList<Something>();
ls.add(new Something(true));
ls.add(new Something(false));
System.out.println("Active things: " + getActive(ls).size());
}
public static List<Something> getActive(List<Something> listOfThings){
return listOfThings != null? listOfThings.stream().filter(t -> t.isActive()).collect(Collectors.toList()): null;
}
public static class Something {
public Something(boolean active) {
this.active = active;
}
private boolean active;
public boolean isActive() {
return active;
}
public void justDoIt() {
System.out.println("done");
}
}
}
Isn't true that, the method getActive() can be optimized avoiding the use of streams ?
I understand that it is easier to use streams, but because they have to be general purpose, they will never be faster than well written and optimized code.
For example, if the list is very big and we know that it would be convenient to parallelize the loop on three cores, couldn't we just execute in three different threads the loop with a standard iterator?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm new to Java and development and don't know how to do most of the stuff. So I wanter to ask you guys how to check if an enum value changed. So I have this:
public enum GameState {
WAITING,
INTRO,
INTRO_WAIT,
INTRO_1,
INTRO_1_WAIT,
LOBBY_INTRO,
LOBBY,
INTRO_GAME1,
GAME1,
INTRO_GAME2,
GAME2;
}
So I want to know how to detect if an enum value changed from any of those to any of those. Hope you know what I'm try to say.
Thanks :)
I assume you mean that some other class has a field GameState state, and you want to know when it changes from one value to another.
There's not an "automatic" way to do that. Have that other class make that field be private (which is a good idea anyway), and any time it changes it (for instance, via a setState(GameState) method, it can perform whatever action you want — such as calling any GameStateListener that's been registered with that class, or whatever checking mechanism you want.
It might look something like this:
public interface GameStateListener {
void onChange(GameState changingFrom, GameState changingTo);
}
public class Game {
private GameState state = WAITING; // or whatever initial value
private final Collection<GameStateListener> listeners = new ArrayList<>();
public void registerListener(GameStateListener listener) {
listeners.add(listener);
}
public void changeState(GameState newState) {
for (GameStateListener listener : listeners) {
listener.onChange(state, newState);
}
this.state = newState;
}
...
}
Note that that code is not thread-safe, and making it be thread-safe would add a good deal of complexity. There are other ways to do it, too, but the above is a pretty standard pattern.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How can i change the virtual void Execute (vtkObject *caller, unsigned long eid, void *callData) function of the vtkCallbackCommand class (vtk) to java, thanks a lot, AMAL
Adding an callback method on a specific event is different from C++. As you can see in some vtk Java Exemple you don't have to create a class which extends from vtkCallbackCommand to rewrite the Execute Method.
To add specific behavior you have to use the Java AddObserver() method, It should be something like :
public class kbHandler
{
private vtkRenderWindowInteractor iren;
public static void main(String[] args) {
kbHandler kbh = new kbHandler();
kbh.doit();
}
void callbackHandler ()
{
// if i'm here, a key is pressed !!
// you can get back information from iren (which key : iren.GetKeyCode())
}
public void doit ()
{
// Do lot of things
iren = new vtkRenderWindowInteractor();
iren.SetRenderWindow(renWin);
// add observer for the handler arg1 = event to observe, arg2 object handler of the event, arg3: method to call
iren.AddObserver("CharEvent", this, "callbackHandler");
iren.Initialize();
iren.Start();
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am currently creating a events recorder GUI program. Yet I am encountering a very difficult problem.
How should I store objects within an object within an object within an object?
For example,
I have an event.
This event has 4 category.
In the first category (Category A), there are 30 exhibition shows.
Within each show, there are 20 - 30 representatives. (Let's say 30 reps for the first show).
...
How can I store all these information in an arraylist? OR is there any other better idea?
Should I also apply Polymorphism to this one too?
Event --> Category A (first one out of the four) --> First Show out of 30 --> 1 rep out of 30 reps --> ... etc.
Thanks.
My confusion is that I would like to treat every single of these as an object. For example, category is an object. The show is an object. The reps is an object. My question is how can I store an object within an object within an object and so on? Thanks.
try this
Test.java
import java.util.List;
public class Test {
public List<Category> category;
}
import java.util.List;
Category.java
public class Category {
public List<Exhibition> exhibitionShow;
public void setExhibitionShow(List<Exhibition> exhibitionShow) {
this.exhibitionShow = exhibitionShow;
}
public List<Exhibition> getExhibitionShow() {
return exhibitionShow;
}
}
Exhibition.java
import java.util.List;
public class Exhibition {
public List<Representative> representative;
public void setRepresentative(List<Representative> representative) {
this.representative = representative;
}
public List<Representative> getRepresentative() {
return representative;
}
}
Representative .java
public class Representative {
//add method
}
I am not sure what is your confusion, you need to do something like this
class Event{
Category[] categories;
}
class Category{
ArrayList<Show> shows;
}
class Show{
ArrayList<Representative> reps;
}
//.. and so on.
I think you have the idea. My idea is create one public method.