Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

Extending an interface
 Post subject: Extending an interface Post rating: 0   New post Posted: Fri 12 Nov, 2010, 16:30 
User avatar

User rating: 0
Joined: Tue 24 May, 2011, 00:41
Posts: 38
Location: United KingdomUnited Kingdom
I apologize for the newbie question, but I am not a java programmer.

I am writing my strategy and I would like to extend the methods implemented by a certain interface.

E.g.: suppose I want to declare a method 'getBarColor()' which refers to the IBar interface. Is it possible to achieve it?

At the moment I am using static methods: so I am writing something like

public static ColorType getBarColor(IBar myBar)
{ ... } (note: ColorType is an enum I defined}

and when I call it, I have to call it in a procedural way.

Would it be possible to define a public ColorType getBarColor() method which refers to IBar in order to call it, e.g., like:

lastBar.getBarColor(). It would be more natural for me.

Thank in advance for your precious help and for this great forum.

Have a nice weekend.


 
 Post subject: Re: Extending an interface Post rating: 0   New post Posted: Sat 13 Nov, 2010, 13:01 
User avatar

User rating: 0
Joined: Tue 24 May, 2011, 00:41
Posts: 38
Location: United KingdomUnited Kingdom
I partially solved the issue, defining an inner class.

Is it an efficient solution? Are there any other methods to achieve this?

Thanks again.


 
 Post subject: Re: Extending an interface Post rating: 0   New post Posted: Mon 15 Nov, 2010, 17:00 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Hi,
It's really not a good idea to complicate your strategy with inner class that implements a single method, why not keep simple things simple:

private static ColorType getColor(IBar bar){
  return bar.getClose() > bar.getOpen() ? ColorType.GREEN : ColorType.RED;
}

is so much more readable than the inner class.


 
 Post subject: Re: Extending an interface Post rating: 0   New post Posted: Tue 16 Nov, 2010, 15:26 
User avatar

User rating: 0
Joined: Tue 24 May, 2011, 00:41
Posts: 38
Location: United KingdomUnited Kingdom
Dear Support,

Thanks for your kind response.

In fact I was also considering the proposed solution; I wondered if there were any other methods to achieve this, without using static methods. I infer that it is not.

I agree that in fact defining inner classes may be confusing (and lengthy). I have already coded that part, but I will revert to 'old' static methods in the future, then.

Thanks again.


 

Jump to:  

  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com