At the moment I'm studying design patterns and I've come to a part where I'm confused whether the observer pattern makes use of the push mechanism or does it make use of the pull mechanism?
I've read different implementations of this and can't really establish which one is correct.
Also, I'd like to know three straight forward advantages of the push model towards the pull model.
I guess one of them is that the push model is less coupled then the pull model?
Observer Pattern in detail (with the focus on questions asked)
Definition : The Observer Pattern defines a one-to-many dependency between the objects so that when one object changes state, all of its dependents are notified and updated automatically
3 things to focus:
Observable object - The object being observed.
Observer objects - The objects that observe the observable object
Communication Mechanism - Pull or Push Mechanism
At the moment i'm studying design patterns and i've came to a part where i'm confused whether the observer pattern makes use of the push mechanism or does it makes use of the pull mechanism ?
Confusion might be because you are literary going by name - Pull or Push.
Please note that in both mechanisms, it is always the responsibility of Observable object to notify all the subscribed observers, but the difference lies whether [Push->]the observer get the exact data it wants or [Pull->] it get the data wrapped in some object (mostly Observable object) & it has to extract the required data from it.
Push -> Observer get the required data directly
Pull -> Observer get the data wrapped in an object and it needs to extract that.
I've read different implementations of this and can't really establish which one is correct.
It is not the question of correction here, actually both will work absolutely fine in any situation. It's just which is best suited to a particular scenario/situation which can be easily analysed if we see following details for both the mechanism.
Also i'd like to know three straight forward advantages of the push model towards the pull model. I guess one of them is that the push model is less coupled then the pull model ?
I may not be able to provide 3 advantages, but let me try if I can give you a clear picture of where to use what by using use-case examples:
Push Mechanism
This is purely the Observable's responsibility, Observer just need to make sure they have put required code in their update methods.
Advantages
The main advantage of the 'push' model is lower coupling between the observer and the subject.
Observable & Observer both are interfaces/abstract classes which is actually a design principle - Program to interface or supertypes
Disadvantage
Less flexibility : As Observable needs to send the required data to the Observer and it would become messy if we have say 1000 observers and most of them require different types of data.
Use-Case
It should be used when there are max 2-3 different types of Observers (different types means observer require different data) or all observers require same type of data.
Like token systems in a bank
In this all observers (different LEDs) just need one notification the list of updated waiting token numbers, so may better be implemented in this way as compared to Pull Mechanism.
Pull Mechanism
In pull mechanism as well, it is the Observable's responsibility to notify all observer that something has been changed, but this time Observable shares the whole object which has the changes, some observers might not require the complete object, so Observers just need extract the required details from that complete project in their update methods.
Advantages
The advantage of this is more flexibility.
Each observer can decide for itself what to query, without relying on the subject to send the correct (only required) information.
Disadvantage
The observers would have to know things about the subject in order to query the right information from the shared complete object.
Use-Case
It should be used when there are more than 2-3 different types of Observers (different types means observer require different data)
Like publishing of FX Rates by any foreign exchange rates provider for different investment banks
In this Some banks just deals with only INR, some others only GBP etc. so it should be implemented using Pull mechanism as compared to Push mechanism.
References
Head-First book for design Patterns
https://softwareengineering.stackexchange.com/questions/253398/the-observer-pattern-using-the-pulling-mechanism?newreg=999c28a6a1f6499783fbe56eb97fa8ec
https://dzone.com/articles/observer-pattern
This is an example code that uses the "PULL" mode as explained above Observers could get different types of data (not implemented in this case).
import java.io.*;
import java.util.*;
interface Observer{
public void update();
}
interface Observable {
public void notifyAll() throws Exception;
public void notify(Observer o) throws Exception;
}
class Suscriber implements Observer {
String id;
Subject subject;
boolean registered = false;
Double data = 0.0;
public Suscriber(String id,Subject sub){
this.id = id;
subject = sub;
subject.register(this);
registered = true;
}
public void update() {
if(registered){
data = subject.getData();
}
display();
}
void display(){
System.out.println("Suscriber:" + id + " updated");
System.out.println("Current DATA: " + data);
}
}
class Subject implements Observable{
private List<Observer> observers = new ArrayList<Observer>();
private Double data = 0.0;
public void register(Observer o){
observers.add(o);
}
public void unregister(Observer o){
int i = observers.indexOf(o);
observers.remove(i);
}
public void notify(Observer o) throws Exception{
o.update();
}
public void notifyAll() throws Exception {
for(Observer o:observers)
this.notify(o);
}
public void computeMetrics(){
try{
long bunch = System.currentTimeMillis()/2;
data = data + new Double(bunch);
this.notifyAll();
}catch(Exception ex){
ex.printStackTrace();
}
}
public Double getData() {
return this.data;
}
}
class ObserverTestDrive {
public static void main (String[] args) {
Subject subject = new Subject();
long transmission = 10000;
Suscriber norths = new Suscriber("NorthStation",subject);
Suscriber wests = new Suscriber("WestStation",subject);
Suscriber souths = new Suscriber("SouthStation",subject);
for(int i=0;i<transmission;i++)
subject.computeMetrics();
}
}
Here are two examples (one using push and other using pull) from Head First Design Patterns.
Push:
public interface Subject {
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers();
}
public interface Observer {
public void update(float temp, float humidity, float pressure);
}
import java.util.*;
public class WeatherData implements Subject {
private ArrayList<Observer> observers;
private float temperature;
private float humidity;
private float pressure;
public WeatherData() {
observers = new ArrayList<Observer>();
}
public void registerObserver(Observer o) {
observers.add(o);
}
public void removeObserver(Observer o) {
int i = observers.indexOf(o);
if (i >= 0) {
observers.remove(i);
}
}
public void notifyObservers() {
for (int i = 0; i < observers.size(); i++) {
Observer observer = (Observer)observers.get(i);
observer.update(temperature, humidity, pressure);
}
}
public void setMeasurements(float temperature, float humidity, float pressure) {
this.temperature = temperature;
this.humidity = humidity;
this.pressure = pressure;
notifyObservers();
}
public float getTemperature() {
return temperature;
}
public float getHumidity() {
return humidity;
}
public float getPressure() {
return pressure;
}
}
public class CurrentConditionsDisplay implements Observer {
private float temperature;
private float humidity;
private WeatherData weatherData;
public CurrentConditionsDisplay(WeatherData weatherData) {
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
public void update(float temperature, float humidity, float pressure) {
this.temperature = temperature;
this.humidity = humidity;
display();
}
public void display() {
System.out.println("Current conditions: " + temperature
+ "F degrees and " + humidity + "% humidity");
}
}
public class StatisticsDisplay implements Observer {
private float maxTemp = 0.0f;
private float minTemp = 200;
private float tempSum= 0.0f;
private int numReadings;
private WeatherData weatherData;
public StatisticsDisplay(WeatherData weatherData) {
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
public void update(float temp, float humidity, float pressure) {
tempSum += temp;
numReadings++;
if (temp > maxTemp) {
maxTemp = temp;
}
if (temp < minTemp) {
minTemp = temp;
}
display();
}
public void display() {
System.out.println("Avg/Max/Min temperature = " + (tempSum / numReadings)
+ "/" + maxTemp + "/" + minTemp);
}
}
public class ForecastDisplay implements Observer, DisplayElement {
private float currentPressure = 29.92f;
private float lastPressure;
private WeatherData weatherData;
public ForecastDisplay(WeatherData weatherData) {
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
public void update(float temp, float humidity, float pressure) {
lastPressure = currentPressure;
currentPressure = pressure;
display();
}
public void display() {
System.out.print("Forecast: ");
if (currentPressure > lastPressure) {
System.out.println("Improving weather on the way!");
} else if (currentPressure == lastPressure) {
System.out.println("More of the same");
} else if (currentPressure < lastPressure) {
System.out.println("Watch out for cooler, rainy weather");
}
}
}
public class WeatherStation {
public static void main(String[] args) {
WeatherData weatherData = new WeatherData();
CurrentConditionsDisplay currentDisplay =
new CurrentConditionsDisplay(weatherData);
StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData);
ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData);
weatherData.setMeasurements(80, 65, 30.4f);
weatherData.setMeasurements(82, 70, 29.2f);
weatherData.setMeasurements(78, 90, 29.2f);
}
}
Pull:
public interface Subject {
public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers();
}
public interface Observer {
public void update();
}
import java.util.*;
public class WeatherData implements Subject {
private ArrayList<Observer> observers;
private float temperature;
private float humidity;
private float pressure;
public WeatherData() {
observers = new ArrayList<Observer>();
}
public void registerObserver(Observer o) {
observers.add(o);
}
public void removeObserver(Observer o) {
int i = observers.indexOf(o);
if (i >= 0) {
observers.remove(i);
}
}
public void notifyObservers() {
for (Observer observer : observers) {
observer.update();
}
}
public void setMeasurements(float temperature, float humidity, float pressure) {
this.temperature = temperature;
this.humidity = humidity;
this.pressure = pressure;
notifyObservers();
}
public float getTemperature() {
return temperature;
}
public float getHumidity() {
return humidity;
}
public float getPressure() {
return pressure;
}
}
public class CurrentConditionsDisplay implements Observer {
private float temperature;
private float humidity;
private WeatherData weatherData;
public CurrentConditionsDisplay(WeatherData weatherData) {
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
public void update() {
this.temperature = weatherData.getTemperature();
this.humidity = weatherData.getHumidity();
System.out.println("Current conditions: " + temperature
+ "F degrees and " + humidity + "% humidity");
}
}
public class StatisticsDisplay implements Observer {
private float maxTemp = 0.0f;
private float minTemp = 200;
private float tempSum= 0.0f;
private int numReadings;
private WeatherData weatherData;
public StatisticsDisplay(WeatherData weatherData) {
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
public void update() {
float temp = weatherData.getTemperature();
tempSum += temp;
numReadings++;
if (temp > maxTemp) {
maxTemp = temp;
}
if (temp < minTemp) {
minTemp = temp;
}
System.out.println("Avg/Max/Min temperature = " + (tempSum / numReadings)
+ "/" + maxTemp + "/" + minTemp);
}
}
public class ForecastDisplay implements Observer {
private float currentPressure = 29.92f;
private float lastPressure;
private WeatherData weatherData;
public ForecastDisplay(WeatherData weatherData) {
this.weatherData = weatherData;
weatherData.registerObserver(this);
}
public void update() {
lastPressure = currentPressure;
currentPressure = weatherData.getPressure();
System.out.print("Forecast: ");
if (currentPressure > lastPressure) {
System.out.println("Improving weather on the way!");
} else if (currentPressure == lastPressure) {
System.out.println("More of the same");
} else if (currentPressure < lastPressure) {
System.out.println("Watch out for cooler, rainy weather");
}
}
}
public class WeatherStation {
public static void main(String[] args) {
WeatherData weatherData = new WeatherData();
CurrentConditionsDisplay currentDisplay =
new CurrentConditionsDisplay(weatherData);
StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData);
ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData);
weatherData.setMeasurements(80, 65, 30.4f);
weatherData.setMeasurements(82, 70, 29.2f);
weatherData.setMeasurements(78, 90, 29.2f);
}
}
The difference being, in case of Push, the Subject not only let the observer know that he has new data, he also send the data to Observer (regardless of whether the Observer asked for it or not).
For example, let's check the update method of StatisticsDisplay in push:
public void update(float temp, float humidity, float pressure) {
tempSum += temp;
numReadings++;
if (temp > maxTemp) {
maxTemp = temp;
}
if (temp < minTemp) {
minTemp = temp;
}
display();
}
In case of Pull, the Subject not only let the observer know that he has new data. The Observer use the getter methods on the Subject to pull the values it needs.
For example, let's check the update method of StatisticsDisplay in pull:
public void update() {
float temp = weatherData.getTemperature();
tempSum += temp;
numReadings++;
if (temp > maxTemp) {
maxTemp = temp;
}
if (temp < minTemp) {
minTemp = temp;
}
System.out.println("Avg/Max/Min temperature = " + (tempSum / numReadings)
+ "/" + maxTemp + "/" + minTemp);
}
You might have also noticed that in case of pull, update method does not have any arguments.
The observer pattern uses push since the observable object push notifications to its subscribers.
Push vs Pull (in web mostly):
Push - the server sends(push) notifications to clients, this means it needs keep tracking on their address (URI) or in the more general case their reference.
Pull - the client is responsible for requesting fresh data from the server.
The pattern is not only for Web and is used all over, for example in desktop applications.
Related
I'm learning observer design pattern from one of youtube videos and want to understand a bit more the behavior of interface arraylist, I think I quite understand how it works but when it comes the interface arraylist, its confusing to me how to access the value inside loop.
the subject and observer interfaces as follow;
public interface Subject {
public void register(Observer o);
public void unregister(Observer o);
public void notifyObserver();
}
public interface Observer {
public void updateViaObserver(double ibmPrice, double aaplPrice, double googPrice);
}
package observer_pattern;
import java.util.ArrayList;
import java.util.Arrays;
public class StockGrabber implements Subject{
private ArrayList<Observer> observers;
private double ibmPrice;
private double aaplPrice;
private double googPrice;
public StockGrabber(){
// Creates an ArrayList to hold all observers
observers = new ArrayList<Observer>();
}
public void register(Observer newObserver) {
observers.add(newObserver);
}
public void notifyObserver() {
for(Observer observer : observers){
observer.updateViaObserver(ibmPrice, aaplPrice, googPrice);
}
}
public void setPrice(double newIBMPrice, double newAAPLPrice, double newGOOGPrice){
this.ibmPrice = newIBMPrice;
this.aaplPrice = newAAPLPrice;
this.googPrice = newGOOGPrice;
notifyObserver();
}
}
package observer_pattern;
public class StockObserver implements Observer {
private double ibmPrice;
private double aaplPrice;
private double googPrice;
private Subject stockGrabber;
public StockObserver(Subject stockGrabber){
this.stockGrabber = stockGrabber;
this.observerID = ++observerIDTracker;
stockGrabber.register(this);
}
// Called to update all observers
public void updateViaObserver(double ibmPrice, double aaplPrice, double googPrice) {
this.ibmPrice = ibmPrice;
this.aaplPrice = aaplPrice;
this.googPrice = googPrice;
// this works
printThePrices();
// doesn't work
toString();
}
public void printThePrices(){
System.out.println(observerID + "\nIBM: " + ibmPrice + "\nAAPL: " +
aaplPrice + "\nGOOG: " + googPrice + "\n");
}
public String toString() {
return "StockObserver: ibmPrice=" + ibmPrice + " aaplPrice=" + aaplPrice;
}
}
MAIN
package observer_pattern;
public class GrabStocks{
public static void main(String[] args){
StockGrabber stockGrabber = new StockGrabber();
StockObserver observer1 = new StockObserver(stockGrabber);
stockGrabber.setIBMPrice(197.00);
stockGrabber.setAAPLPrice(677.60);
stockGrabber.setGOOGPrice(676.40);
observer1.toString();
}
}
How to access value for my ArrayList<Observer> observers inside "notifyObserver" method? if I do this System.out.println(observers.get(0)); I get observer_pattern.StockObserver#446cdf90
How to access value for my ArrayList observers inside "notifyObserver" method?
You're already doing it.
public void notifyObserver() {
for(Observer observer : observers){
observer.updateViaObserver(ibmPrice, aaplPrice, googPrice);
}
}
if I do this System.out.println(observers.get(0)); I get observer_pattern.StockObserver#446cdf90
That's because your StockObserver class does not have a toString method that returns a human readable representation of the object. Something like this
public String toString() {
return "StockObserver: ibmPrice=" + ibmPrice + " aaplPrice=" + aaplPrice;
}
I am trying to select a random method from the ones created inside the class. Is there a way to create an ArrayList and pass methods to it? I have attempted to do just that but I am getting an error when I try to add the method to the array.
public class Monkey{
private int energy;
String[] food = {"Meat", "Fish", "Bugs", "Grain"};
ArrayList<Activity> monkeyActivity = new ArrayList<>();
public Monkey(int energy) {
this.energy = energy;
}
public int getEnergy() {
System.out.println("Monkey energy level: " + energy);
return energy;
}
public void sound() {
System.out.println("Monkey: Oooo Oooo~!");
energy -= 3;
monkeyActivity.add(sound()); //I get an error message here when trying
//to add the method to the array
}
public void play(){
if (energy >= 8){
System.out.println("Monkey begins to play.");
energy -= 8;
}else {
System.out.println("Monkey does not have enough energy to play");
}
System.out.println("Energy remaining: " + energy);
}
public void eat(){
Random random = new Random();
int index = random.nextInt(food.length);
System.out.println("Monkey beings to eat " + food[index]);
energy += 5;
System.out.println("Energy remaining: " + energy);
}
public void sleep(){
System.out.println("Monkey is sleeping: Zzz...");
energy += 10;
System.out.println("Energy remaining: " + energy);
}
}
This is the separate class I have made for the generic Activity..
public class Activity {
private String sleep;
private String eat;
private String sound;
private String play;
public Activity(String sleep, String eat, String sound, String play) {
this.sleep = sleep;
this.eat = eat;
this.sound = sound;
this.play = play;
}
public String getSleep() {
return sleep;
}
public String getEat() {
return eat;
}
public String getSound() {
return sound;
}
public String getPlay() {
return play;
}
public void setSleep(String sleep) {
this.sleep = sleep;
}
public void setEat(String eat) {
this.eat = eat;
}
public void setSound(String sound) {
this.sound = sound;
}
public void setPlay(String play) {
this.play = play;
}
}
You are mixing up concepts.
technical issues:
return value clash
public void sound() {
// ...
monkeyActivity.add(sound());
The return value of your method sound() is void (which means no return value), but you try to add its (not existing) return value as element to the List. This is what your compiler complains about.
unintended recursion
public void sound() {
System.out.println("Monkey: Oooo Oooo~!");
energy -= 3;
monkeyActivity.add(sound());
In the last line you do a recursive call which means you call exactly the same method this code is in. If that happens unintended it almost ever results in a StackOverflowError.
writing classes without proper analysis
You have a class Activity.
But if you have a closer look this is not a single activity (as the classes name implies) but it is all possible activities.
As a result your collection monkeyActivity cannot hold single activities as elements.
Doing a wild guess I think what you wanted is more like this:
interface Activity{
void do();
}
public class Monkey{
private int energy;
String[] food = {"Meat", "Fish", "Bugs", "Grain"};
List<Activity> monkeyActivity = new ArrayList<>();
// ...
public void sound() {
monkeyActivity.add(new Activity(){
public void do(){
System.out.println("Monkey: Oooo Oooo~!");
energy -= 3;
}
});
}
You may store each method as Runnable, as any "action" is no-arg void method satisfying Runnable functional interface:
List<Runnable> actions = Arrays.asList(this::sound, this::play, this::eat, this::sleep);
to execute random method, just:
Random rnd = new Random();
actions.get(rnd.nextInt(actions.size())).run();
I am trying to create a method for " winning percentage " in a player class. I know I need to incorporate total wins divided by total games played, but the code is meant to be simple so I cannot use complex code. (beginner project in computer science) Any useful feedback would be great as I have spent multiple days attempting this and getting no where. By the way, ties count as half a win.
Update: Implemented the getters into the getWinningPercentage method. Also calculated everything inside the getWinningPercentage and removed the setWinningPercentage considering it was useless code. Results were as follows:
Bob
5 wins, 1 losses, 2 ties
Winning percentage = 0.75
public class Player
{
private int numWins = 0;
private int numTies = 0;
private int numLosses = 0;
private String name;
public void setWins(int w)
{
numWins = w;
}
public int getWins()
{
return numWins;
}
public void setTies(int t)
{
numTies = t;
}
public int getTies()
{
return numTies;
}
public void setLosses(int L)
{
numLosses = L;
}
public int getLosses()
{
return numLosses;
}
public void setName(String n)
{
name = n;
}
public String getName()
{
return name;
}
public void incrementWins()
}
numWins++;
}
public void incrementTies()
{
numTies++;
}
public void incrementLosses()
{
numLosses++;
}
public double getWinningPercentage()
{
double totalGames = getWins() + getTies() + getLosses();
double totalWins = getWins() + (getTies() / 2.0);
double winningPercentage = (totalWins / totalGames);
return winningPercentage;
}
}
The winning percentage should be a calculated property, not a field, and not have a setter method. Instead there should only be a "getter" (public double getWinningPercentage()) method and you should calculate and return this value from within the method itself from the other fields that your class already has.
We should leave it up to you to create this method and formula yourself.
I was looking for some good patterns to have possibility to express distance in different units. I found Martin Fowler article about quantities and I programmed something like:
Here is Distance class ( I think it is not necessery to make it abstract ):
public class Distance {
double mValue;
DistanceUnit mUnit;
public Distance(double value, DistanceUnit unit){
this.mValue = value;
this.mUnit = unit;
}
public Distance toUnit(DistanceUnit unit){
double factor = this.mUnit.getMetresFactor()/unit.getMetresFactor();
double newValue = this.mValue * factor;
Distance distance = new Distance(newValue, unit);
return distance;
}
#Override
public String toString(){
return String.valueOf(mValue);
}
}
It looks very simple. Conversion toUnit is based on DistanceUnit method getMetresFactor. Each Unit class implements DistanceUnit interface and has method getMetresFactor() like:
public interface DistanceUnit {
double getMetresFactor();
}
public class Inch implements DistanceUnit {
#Override
public double getMetresFactor() {
return 0.0254;
}
}
public class Kilometer implements DistanceUnit {
#Override
public double getMetresFactor() {
return 1000.0;
}
}
And the usage is for example:
Distance inches = new Distance(300.0, new Inch());
Distance kilometres = inches.toUnit(new Kilometres());
So it returns the correct value.
Is it good way to store distance in this way? Maybe you know some weaknesses of this approach. Maybe is a good idea to use here a FactoryMethod pattern to construct distance based on unit shortcut like "m" for meter. I think about the amount of classes if I would have a lot of units... Is it good idea to have factory which return factor of meters based on unit name? There will be no classes for units then?
Hm, i would use enum instead of DistanceUnit classes, because there is no different instances of them.
You can set a value to enum like here
and then call enum.getValue() instead of unit.getMetresFactor().
Also it is a little bit confusing, is the mValue value in meters or in DistanceUnit's, if in meters, you must have
double factor = unit.getMetresFactor();
there
Ok and now with any convertion function support:
import java.util.HashMap;
import java.util.Map;
public abstract class MeasureConverter {
public abstract double valueToBasic(double value);
public abstract double basictoValue(double basic);
/**
*
*/
public static Map<String, MeasureConverter> converters;
public static Map<String, MeasureConverter> getConverters() {
if (converters == null) {
converters = new HashMap<String, MeasureConverter>();
converters.put("kilo", new MeasureConverter() {
#Override
public double valueToBasic(double value) {
return value * 1000;
}
#Override
public double basictoValue(double basic) {
return basic / 0.001;
}
});
// taking the basic temperature value in kelvines
converters.put("kelvine", new MeasureConverter() {
#Override
public double valueToBasic(double value) {
return value;
}
#Override
public double basictoValue(double basic) {
return basic;
}
});
converters.put("celsius", new MeasureConverter() {
#Override
public double valueToBasic(double value) {
return value + 273.15;
}
#Override
public double basictoValue(double basic) {
return basic - 273.15;
}
});
converters.put("faren", new MeasureConverter() {
#Override
public double valueToBasic(double value) {
return value * 1.8 - 459.67 ; // or whatever is there?
}
#Override
public double basictoValue(double basic) {
return (basic + 459.67 ) / 1.8;// or whatever is there?
}
});
}
return converters;
}
}
And then :
import java.util.Objects;
public class MeasurePattern {
double value;
String name;
public MeasurePattern(double value, String name) {
this.value = value;
this.name = name;
}
#Override
public String toString() {
return "MeasurePattern{" + "value=" + value + ", name=" + name + '}';
}
#Override
public int hashCode() {
int hash = 7;
hash = 29 * hash + (int) (Double.doubleToLongBits(this.value) ^ (Double.doubleToLongBits(this.value) >>> 32));
hash = 29 * hash + Objects.hashCode(this.name);
return hash;
}
#Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final MeasurePattern other = (MeasurePattern) obj;
if (Double.doubleToLongBits(this.value) != Double.doubleToLongBits(other.value)) {
return false;
}
if (!Objects.equals(this.name, other.name)) {
return false;
}
return true;
}
public MeasurePattern convertTo(String converter) {
MeasureConverter mycon = MeasureConverter.getConverters().get(name);
MeasureConverter hiscon = MeasureConverter.getConverters().get(converter);
double basic = mycon.valueToBasic(value);
double hisValue = hiscon.basictoValue(basic);
return new MeasurePattern(hisValue, converter);
}
public static void main(String[] args) {
//trying temperatures;
MeasurePattern temp = new MeasurePattern(10, "celsius");
MeasurePattern kelvine = temp.convertTo("kelvine");
MeasurePattern faren = kelvine.convertTo("faren");
MeasurePattern cels = faren.convertTo("celsius");
System.out.println("kelvine = " + kelvine);
System.out.println("faren = " + faren);
System.out.println("cels = " + cels);
}
}
Output:
kelvine = MeasurePattern{value=283.15, name=kelvine}
faren = MeasurePattern{value=412.67777777777775, name=faren}
cels = MeasurePattern{value=9.999999999999943, name=celsius}
You can implement it analog to java.util.concurrent.TimeUnit as an enum. E.g.
public enum DistanceUnit {
KILOMETER {
#Override
protected double conversionFactor(DistanceUnit toDistanceUnit) {
switch (toDistanceUnit) {
case KILOMETER:
return 1;
case MILE:
return 0.621371;
default:
throw new UnsupportedOperationException(toDistanceUnit + " is not supported");
}
}
},
MILE {
#Override
protected double conversionFactor(DistanceUnit toDistanceUnit) {
switch (toDistanceUnit) {
case KILOMETER:
return 1.60934;
case MILE:
return 1;
default:
throw new UnsupportedOperationException(toDistanceUnit + " is not supported");
}
}
};
public double toDistance(double value, DistanceUnit targetDistance) {
return value * conversionFactor(targetDistance);
}
protected abstract double conversionFactor(DistanceUnit toDistanceUnit);
}
change your Distance class to
public class Distance {
double mValue;
DistanceUnit mUnit;
public Distance(double value, DistanceUnit unit){
this.mValue = value;
this.mUnit = unit;
}
public Distance toUnit(DistanceUnit unit){
double newValue = mUnit.toDistance(mValue, unit);
Distance distance = new Distance(newValue, unit);
return distance;
}
#Override
public String toString(){
return String.valueOf(mValue);
}
}
and the client code will look very clear
public class Main {
public static void main(String[] args) {
Distance kilometers = new Distance(265.35, DistanceUnit.KILOMETER);
Distance miles = kilometers.toUnit(DistanceUnit.MILE);
System.out.println(miles);
}
}
will output
164.88079485000003
Java convention does not use a m(ember) prefix (but say a this. qualification), and convention is taken quite seriously in java (as opposed to C++ for instance).
toString misses the unit.
JScience offers more, the capability to calculate in different units, m/s², and so on. Your class is a nice abstraction. But in a wider context, you probably will want to have math operations, powers of units (-2 for s above).
Take a look at your own usage ideas first:
(Just garbage:)
U speedUnit = U.of(Distance::km, Time::h.up(-1));
double timeInS = U.mile(40).div(speedunit(30)).in(U.m);
I think you should use the "Strategy" pattern.
An interface:
public interface DistanceUnit {
double getDistance(int metres);
}
The Inch class:
public class Inch implements DistanceUnit {
#Override
public double getDistance(int metres) {
return meters*39; //do conversion here
}
}
The Kilometers class:
public class Kilometres implements DistanceUnit {
#Override
public double getDistance(int metres) {
return meters/1000; //do conversion here
}
}
Then:
List<DistanceUnit> distanceList = new ArrayList<>();
distanceList.add(new Inch());
distanceList.add(new Kilometres());
for (DistanceUnit item : distanceList) {
System.out.println(item.getDistance(1000));
}
If I understand you, I think it is a simple and clean solution.
You can follow this model for conversion between others units.
I have a variable money of type double. I want this variable to have 3 states like this:
double money = something;
public int getMoneyState(){
if (money > 0){
return 1;
} else if(money == 0){
return 0;
} else{
return -1;
}
}
Problem is: I only know how to formulate this problem in the most conventional way, that is without using any javafx libraries / functions.
Eventually, I want to have a tableView where one of the columns will display the money variable, and its font color will change depending on the state of this variable, i.e. if after editing the cell, money = 100, the state will be 1 and font color is yellow. If after editing the cell, money = 0, the state will be 0 and font color is grey.And if after editing the cell, money = -555, the state will be -1 and font color is Green.
What I am looking for: I want to be able to track the money variable as well as its state and any changes in state. By that, I mean a change in the money variable will lead to a change in the state by using a method similar to getMoneyState() above. And depending on the state of the variable, the cell's font color will change.
I need help re-writing getMoneyState() method such that the state will automatically be updated after the user edits the money cell.
Hope this makes more sense.
Assuming you have money represented as a DoubleProperty:
DoubleProperty money = new SimpleDoubleProperty();
for example, you can do
IntegerBinding moneyState = Bindings.createIntegerBinding(() -> {
if (money.get() > 0) {
return 1 ;
} else if (money.get() == 0) {
return 0 ;
} else {
return -1 ;
}
}, money);
The two arguments to createIntegerBinding are a function returning an Integer, and a list of other observables on which the binding depends (here there is only one, money).
Now you can add listeners to moneyState or bind to it in the usual way.
If money is a property in some bean, then you can expose moneyState as a ReadOnlyIntegerProperty in a similar way:
public class MyEntity {
private final DoubleProperty money = new SimpleDoubleProperty();
public DoubleProperty moneyProperty() {
return money ;
}
public final double getMoney() {
return moneyProperty().get();
}
public final void setMoney(double money) {
moneyProperty().set(money);
}
private final ReadOnlyIntegerWrapper moneyState = new ReadOnlyIntegerWrapper();
public ReadOnlyIntegerProperty moneyStateProperty() {
return moneyState.getReadOnlyProperty();
}
public int getMoneyState() {
return moneyStateProperty().get();
}
private IntegerBinding moneyStateBinding ;
public MyEntity(double money) {
setMoney(money) ;
moneyStateBinding = Bindings.createIntegerBinding(() -> {
if (getMoney() > 0) {
return 1 ;
} else if (getMoney() == 0) {
return 0 ;
} else {
return -1 ;
}
}, moneyProperty());
moneyState.bind(moneyStateBinding);
}
}
A couple of other options. First note that your logic is already implemented by Math.signum(), so you can do:
IntegerBinding moneyState = Bindings.createIntegerBinding(() ->
(int) Math.signum(money.get()), money);
You can also implement it with the fluent Bindings API:
IntegerBinding moneyState = Bindings.when(money.greaterThan(0)).then(1)
.otherwise(Bindings.when(money.isEqualTo(0)).then(0).otherwise(-1));
You could create and Observer and make a MoneyClass, which inherits the Observable Class for example. You could then track any changes to the Money and it´s state made
The result could be looking like this
// Money class
import java.util.Observable;
public class MoneyClass extends Observable{
private double money = 0;
private int state = 0;
public static final int POSITIV = 1;
public static final int ZERO = 0;
public static final int NEGATIV = -1;
public int getMoneyState(){
if (money > 0){
return MoneyClass.POSITIV;
} else if(money == 0){
return MoneyClass.ZERO;
} else{
return MoneyClass.NEGATIV;
}
}
public void setMoney(int money) {
this.money = money;
setChanged();
notifyObservers("Money");
setMoneyState();
}
public double getMoney() {
return money;
}
public int getState() {
return state;
}
private void setMoneyState() {
if (state != getMoneyState()) {
state = getMoneyState();
setChanged();
notifyObservers("State");
}
}
public static void main(String[] args) {
}
}
//Observer
import java.util.Observable;
import java.util.Observer;
public class MoneyObserver implements Observer{
public void addObserving(MoneyClass money) {
money.addObserver(this);
}
#Override
public void update(Observable o, Object arg) {
if(arg instanceof String) {
String type = (String) arg;
if(type.equals("Money")) {
System.out.println("Money got changed to " + ((MoneyClass)o).getMoney());
} else if(type.equals("State")) {
System.out.println("State got changed to " + ((MoneyClass)o).getState());
}
}
}
public static void main(String[] args) {
MoneyObserver o = new MoneyObserver();
MoneyClass c = new MoneyClass();
o.addObserving(c);
c.setMoney(20);
c.setMoney(50);
c.setMoney(-30);
}
}