#Struggling with object oriented design

29 messages · Page 1 of 1 (latest)

tardy helm
#

I want to design a class "Signal", which holds the attributes "TimeDomain and FreqDomain" (2 other objects)

  • A Signal should be instantiated using either the TimeDomain, or the FreqDomain. But NOT BOTH, because one depends on the other (e.g. FreqDomain is calculated from TimeDomain using the FFT algorithm)
  • Additionally, the TimeDomain or FreqDomain should not be able to be created without belonging to a Signal.
  • Whenever the TimeDomain is changed, the FreqDomain should be changed accordingly (e.g. given a TimeDomain, I need to access its outer object (Signal) then access FreqDomain from that)

Attachments 1-2 show my attempt to implement this.
However, in the multiply method (line33): "Signal.this cannot be used from a static context" and I'm not sure how to access Signal from the TimeDomain passed in as a parameter
What should I do?

toxic gobletBOT
#

This post has been reserved for your question.

Hey @tardy helm! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

tawny nest
#
  • Additionally, the TimeDomain or FreqDomain should not be able to be created without belonging to a Signal.
    probably pass this when constructing those and put that as an instance field
tardy helm
#

like this?

tawny nest
#

yeah

#

actually though, do you need Signal at all

#

could you just have methods on TimeDomain and FreqDomain that covert to each other?

tardy helm
#

thing is when I change TimeDomain, it affects its corresponding FreqDomain too.

I'm basically making a JavaFX application which shows both (on LineCharts), and if you chose to multiply the TimeDomain with another TimeDomain - it causes a "convolution" (a type of math operation) of both FreqDomains

So Signal acts as the platform of changes which affect both? not sure if that makes sense

tawny nest
#

you could make them immutable

#

then you could just recompute the FreqDomain explicitly

tardy helm
#

Actually yeah recomputing the FreqDomain is more efficient than doing convolution, but there are also some other special cases of when it's far more efficient to avoid recomputation (shifting TimeDomain etc.)

#

or when I scale the TimeDomain (multiply by constant), it does the same to the FreqDomain

tardy helm
tardy helm
tawny nest
#

ah, right, yeah. you could maybe set it afterwards but having that exposed setter doesn't seem all that good

#

i was thinking about this, mb

Constructor() {
  field = new Inner(this);
}
```which i don't think really works with your pattern
tardy helm
#

Hmm maybe I can just pass in a ''Signal'' to each method instead of the inner objects?

tawny nest
#

that seems like it'd be passing around partial objects, that doesn't seem like a good idea 🤔

tardy helm
#

alright what about this?

tawny nest
#

oh, i didn't notice that FreqDomain took a different parameter for its constructor. that looks like it could work.

tardy helm
#

Hm the issue is FreqDomain can also be just real numbers (double[])
the static builder methods fromTimeDomain and fromFreqDomain used before clearly stated which you were using to create the Signal

In this case it doesn't really show that anywhere

#

But if there's no better solution then this is fine ig

tardy helm
#

actually can I just do this?

tawny nest
#

sure

tardy helm
#

then replace the Signal. with these instead

#

should be good now ty for help

tawny nest
#

i think signal is a better name than belongsTo there

tardy helm
#

fair enough yeah