Call unique methods without instanceof

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 2271 Views - Last Post: 28 November 2012 - 08:05 PM Rate Topic: -----

#16 Virkin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-June 12

Re: Call unique methods without instanceof

Posted 27 November 2012 - 04:23 PM

Thanks a lot for all your help but I getting little confused about all the things you say and if they are good or bad etc.

So perhaps it would be better if I told you a bit more what I am doing.

Basically my superclass is called UnitGroup and has subclasses like Length, Area etc etc and are able to convert from one unit to another in each. e.g Length can convert miles to kilometers. Common methods and fields are placed in the superclass UnitGroup.

Then I have Converter class which holds a List of UnitGroup objects. Filled with objects of Length, Area so on. When I convert I loop through each UnitGroup, see if the conversion can be done, if can do it and return it from Converter method.

This works very well for all units I have so far.

The problem is I have added UnitGroup for Currency. It works fine for base values but I put method into Currency UnitGroup called updateCurrencies() which downloads latest currency conversions and updates the current working set.

This is all well and good and I have implemented it all. The problem however is that I need method in Converter class called updateCurrencies() as well which will call the update method on the Currency object.

This is problem I am having, how to call the unique method in Converter on Currency objects from List of UnitGroup objects.

Hope that clarify more.
Was This Post Helpful? 0
  • +
  • -

#17 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2021
  • View blog
  • Posts: 4,194
  • Joined: 11-December 07

Re: Call unique methods without instanceof

Posted 27 November 2012 - 04:38 PM

You don't need to update the currencies every time a conversion is done. Correct me if I am wrong but I think they are updated daily. I would maintain a separate list of currencies which can be updated every day or, more realistically, every time you launch your application.
Was This Post Helpful? 0
  • +
  • -

#18 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2735
  • View blog
  • Posts: 11,525
  • Joined: 20-September 08

Re: Call unique methods without instanceof

Posted 27 November 2012 - 04:43 PM

Quote

Basically my superclass is called UnitGroup and has subclasses like Length, Area
That sounds strange. The 'subclasses' sound more like attributes of entities than entities. And why has the superclass got 'group' in its name?
Was This Post Helpful? 0
  • +
  • -

#19 Virkin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-June 12

Re: Call unique methods without instanceof

Posted 27 November 2012 - 04:53 PM

@cfoley - No the currencies aren't updated each time a conversion is done, its in a completely separate method. Essentially the Converter class has an updateCurrencies() method as well which can be called at any time when the user clicks on a button in the GUI I made for it. It's all to split the logic from the Gui layer.

@g00se - I am not sure what you mean? It has Group in the name because it is a group. A group of units. For example the Length subclass is a UnitGroup as it holds a group of all the length units e.g miles, meters etc.
Was This Post Helpful? 0
  • +
  • -

#20 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2735
  • View blog
  • Posts: 11,525
  • Joined: 20-September 08

Re: Call unique methods without instanceof

Posted 27 November 2012 - 05:00 PM

Hmm. Maybe you can fill in some more details
Was This Post Helpful? 0
  • +
  • -

#21 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2021
  • View blog
  • Posts: 4,194
  • Joined: 11-December 07

Re: Call unique methods without instanceof

Posted 27 November 2012 - 05:02 PM

OK, so my point is that at the moment updating the currency rates in confused with some of your other logic. Get it out of there and put it in its own part of the program. That might mean having a separate list containing only the objects with that method. It might be making a new method somewhere else to do the updating.

It certainly shouldn't be in a part of the program that loops over ALL your objects, including ones that don't have the method you want to call.
Was This Post Helpful? 0
  • +
  • -

#22 Virkin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-June 12

Re: Call unique methods without instanceof

Posted 27 November 2012 - 05:18 PM

So perhaps just store a reference to that object as a field or something and then have the same reference in the List? That way I suppose there is no need to loop at all in the updateCurrencies() method of the Converter class. Is this what kind of thing you mean?
Was This Post Helpful? 0
  • +
  • -

#23 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7808
  • View blog
  • Posts: 13,204
  • Joined: 19-March 11

Re: Call unique methods without instanceof

Posted 27 November 2012 - 05:21 PM

@cfoley, re the visitor pattern: so, "doUniqueMethod()", as I suggested earlier?

This post has been edited by jon.kiparsky: 27 November 2012 - 05:22 PM

Was This Post Helpful? 1
  • +
  • -

#24 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2021
  • View blog
  • Posts: 4,194
  • Joined: 11-December 07

Re: Call unique methods without instanceof

Posted 27 November 2012 - 05:23 PM

Possibly. I'm not sure what you mean so I don't know if what you mean is what I mean, if you get what I mean.

Why don't you post your code? Then I might be able to show you what I'm talking about.
Was This Post Helpful? 0
  • +
  • -

#25 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2021
  • View blog
  • Posts: 4,194
  • Joined: 11-December 07

Re: Call unique methods without instanceof

Posted 27 November 2012 - 05:29 PM

@Jon, yup, missed that. :) A key point of the accept() method is that it "accepts" a visitor as an argument. You could have another visitor for feeding the cat hierarchy, for example, without having to change the Cat class or its subclasses.
Was This Post Helpful? 0
  • +
  • -

#26 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7808
  • View blog
  • Posts: 13,204
  • Joined: 19-March 11

Re: Call unique methods without instanceof

Posted 27 November 2012 - 05:36 PM

View Postcfoley, on 27 November 2012 - 07:29 PM, said:

@Jon, yup, missed that. :)/> A key point of the accept() method is that it "accepts" a visitor as an argument. You could have another visitor for feeding the cat hierarchy, for example, without having to change the Cat class or its subclasses.


Awesome. I invented another design pattern! Woot! :)
Was This Post Helpful? 0
  • +
  • -

#27 Virkin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-June 12

Re: Call unique methods without instanceof

Posted 27 November 2012 - 05:37 PM

I don't want to post the whole thing as well once it on the internet its on the internet. But I give my best try to give you the idea - \

public class UnitGroup {
    protected List<Unit> units;

    // some other fields here

   // method to convert one unit to another using the list and return double not relevant though
}

public class Length extends UnitGroup {
   // handles population of the units list with length Unit objects
}

public class Currency extends UnitGroup {
      // handles population of the units list with currency Unit objects

      public void update() {
           // unique method to get latest rates online and change list correspondingly
      }
}


public class Converter {
   private List<UnitGroup> groups;

   // groups filled with UnitGroups such as Currency or Length etc

  // here is a convert method also that loops through the list and converts the units if valid


  public void updateCurrencies() {
      // but I also need a way of updating the currencies in a method in this class

     // here lies the problem, how do I call the update method on the Currency object in the list
  }
}


Thats pretty much it, not sure what else I can say.
Was This Post Helpful? 0
  • +
  • -

#28 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2021
  • View blog
  • Posts: 4,194
  • Joined: 11-December 07

Re: Call unique methods without instanceof

Posted 27 November 2012 - 05:55 PM

A quick fix is to create a field private Currency currencyGroup; in your Converter class. This means it will not have to loop through all the groups to update the currencies. A better solution might be to move that method and field to another part of your program.
Was This Post Helpful? 0
  • +
  • -

#29 Virkin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-June 12

Re: Call unique methods without instanceof

Posted 27 November 2012 - 06:01 PM

Yeah that what I was thinking I might have to do. The thing is I can't really see way of moving the method to another part of program as there is nowhere suitable to put it to be honest. I think I might have to follow through with the quick fix like you said.
Was This Post Helpful? 0
  • +
  • -

#30 Virkin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-June 12

Re: Call unique methods without instanceof

Posted 28 November 2012 - 12:18 PM

Hi, again. Sorry I wasn't around my actual code yesterday and forgot about a big factor that means the solution that I was going to implement now wouldn't work.

Essentially before I said the Converter class was responsible for filling the List of Units with UnitGroup objects, as it turns out actually added another class to provide the a list of units as I thought the Converter class shouldn't be responsible for this.

Its actually more like

class Converter {
    public Converter() {
         UnitGroupProvider provider = new UnitGroupProvider();
         this.groups = provider.provideGroups();
    }

    // then convert method as I said before
}


Which essentially means now I don't have access to any Currency objects as they are being provided already in a List of UnitGroup objects from the UnitGroupProvider object.

I know theres probably no solution now, but any ideas. I get the feeling that this design is poor?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3