I wish to create two separate LocationProvider instances, each using a different gps mode (assist, cellsite, etc.). The docs on LocationProvider.getInstance say that:
This method may, depending on the implementation, return the same
LocationProvider instance as has been returned previously from this
method to the calling application, if the same instance can be used to
fulfill both defined criteria
Which seems a little vague - if I set different modes in the different criteria can I be sure I will get two different LocationProviders? If not, will creating them on two different threads do the trick?
Related
I've got an app that has a Main Page, a Room page, and then a subroom of the room page.
To know what to show on the detail page, the detail page needs a string value from the Main page, and in the subroom it needs a Room class object from the room page.
I've been just setting these as static variables on my Room class, so when I'm needing the string or object I'd do
selectedRoomname = Room.selectedName
selectedInstance = Room.selectedInstance
It would be very possible to pass these variables around with segues or use delegegates, but is there any reason to NOT continue what I'm doing? Considering it's only two variables I'm doing this with I can't imagine there's a big impact on memory usage. Is there a limit to how far I could go with using static variables? If I'm needing to access a variable such as the user's username, profile image, etc on almost every one of my view controllers, is there any issue with making a static User class object?
tl;dr, how intense is it to use static variables and is there such a thing as abusing them?
The pragmatic reason would be that it limits you to one unique instance of a Room. If you ever want to show more than 1 room, you'll have to redesign it. That type of design is fragile and inflexible.
The more philosophical reason is that it breaks OOP. A class should not keep track of itself, that's it's parent's job (with a possible exception singletons but that's a whole other can-o-worms). It also makes subclassing tricky. Should a LaundryRoom class have it's own static variables? should the LaundryRoom class also use Room's static variable? That's inherently confusing.
The way I tend to solve these problems is think about it from an IRL perspective. Do all rooms have the same name? Can only 1 room ever be inhabited? If these attributes are individual to an instance and not the concept, it should be an instance variable.
I am trying to implement component for possibility to apply different skins to views and controllers at runtime without reinitialising these controls. I want to use such logic:
Declare protocol with methods for applying skins.
All necessary classes implements this protocol.
When user selects skin all instances of classes that conform to protocol receive message to apply skin.
So I know how to get all necessary classes that conform to my specific protocol by using objc_getClassList and class_conformsToProtocol functions.
But how to get all allocated instances of these classes for sending message to them?
I know that it could be implemented by internal logic of every class by storing all instances in static storage and returning array by class method. But it isn't elegant solution. I'm finding more universal solution where I can add new skinnable controls in easy way.
It sounds very much like you're reinventing <UIAppearance>. You should at least start there. It's what it's for. Also see Peter Steinberger's writeup for discussion of adding custom properties.
To your basic question, there is not a runtime call to enumerate all allocated objects of a class. It would add a lot of overhead to provide that (objects come and go all the time and very quickly). Even if you could do it, you probably shouldn't. But since you're talking about visible views, then you can always do this by enumerating the view hierarchy under NSWindow. Any views not currently in the view hierarchy should be expected to correctly redraw in an new style the next time they come on the screen.
But I'd start with <UIAppearance>.
i want to create a polymorphic Queue association. I have two different queues in mind, one is about creating buildings, one is about creating units.
Though the queue has many same properties, like adding or removing items, there are some differences as well.
For instance, a building has a level, while a unit does not. The QueuedItem polymorphic class normally has some methods, like the remove one. However, a remove method is different, depending on whether the item is a building or a unit and needs different handling.
Moreover, the level column is totally insignificant to an army queued item.
So, my question is, based on those needs, what is the best way to design that ? Should i just go ahead with 3 models like (a queued item always belongs to a city):
Building
Unit
QueuedItem -> queued_id, queued_type, city_id
Or i would need to add more intermediate models ? What do you think ?
From your description, it appears you actually need classes for the queues themselves, not just for the queued items. So queue-related methods and operations go in the queue class, and stuff that's related only to buildings or units goes into those classes.
A Queue class would only include methods for adding, removing, and maybe reordering its elements, all of which are unlikely to require different implementations for different types of object. It's better that these methods have no side effects other than adding or removing from the queue - if you need something else to happen after an object is removed, for example, this can be done by the caller of Queue#remove. If do you need the queues themselves to behave differently depending on whether they contain buildings or units, then you'd need a BuildingQueue and a UnitQueue, both of which could inherit their common methods from a basic Queue class, or an included Queue module.
Other stuff specific to buildings and units (such as whether they have a level, or what their specific stats are) would go inside the Building and Unit classes proper. They don't need to know anything about how a queue behaves.
You could create a module with the logic for adding/removing from the queue and include it in both the classes. If you plan to use it in a lot of classes, you could also move it to a plugin, and add the helpers to the models which are to be implemented as queues.
I'm currently working on a Rails project, and have found times where it's easiest to do
if object.class == Foo
...
else if object.class == Bar
...
else
...
I started doing this in views where I needed to display different objects in different ways, but have found myself using it in other places now, such as in functions that take objects as arguments. I'm not precisely sure why, but I feel like this is not good practice.
If it's not good practice, why so?
If it's totally fine, when are times that one might want to use this specifically?
Thanks!
Not sure why that works for you at all. When you need to test whether object is instance of class Foo you should use
object.is_a? Foo
But it's not a good practice in Ruby anyway. It'd much better to use polymorphism whenever it's possible. For example, if somewhere in the code you can have object of two different classes and you need to display them differently you can define display method in both classes. After that you can call object.display and object will be displayed using method defined in the corresponding class.
Advantage of that approach is that when you need to add support for the third class or a whole bunch of new classes all you'll need to do is define display method in every one of them. But nothing will change in places where you actually using this method.
It's better to express type specific behavior using subtyping.
Let the objects know how they are displays. Create a method Display() and pass all you need from outside as parameter. Let "Foo" know to display foo and "Bar" know how to display bar.
There are many articles on replacing conditionals with polymorphism.
It’s not a good idea for several reasons. One of them is duck typing – once you start explicitly checking for object class in the code, you can no longer simply pass an instance of a different class that conforms to a similar interface as the original object. This makes proxying, mocking and other common design tricks harder. (The point can be also generalized as breaking encapsulation. It can be argued that the object’s class is an implementation detail that you as a consumer should not be interested in. Broken encapsulation ≈ tight coupling ≈ pain.)
Another reason is extensibility. When you have a giant switch over the object type and want to add one more case, you have to alter the switch code. If this code is embedded in a library, for example, the library users can’t simply extend the library’s behaviour without altering the library code. Ideally all behaviour of an object should be a part of the object itself, so that you can add new behaviour just by adding more object types.
If you need to display different objects in a different way, can’t you simply make the drawing code a part of the object?
I have a guice based app that now needs multiple instances of a given type so I plan on using a named annotation to disambiguate the dependencies. However a dependency of this type also needs to vary based on which one I get.
To illustrate lets say I have
#Singleton
public class FooCache {
private final FooCacheListener listener;
#Inject
public FooCache(FooCacheListener listener) {
this.listener = listener;
}
// do stuff
}
and then lets say I have a need for 2 separate instances so I might have
#ThatOne FooCache
in one class and
#ThisOne FooCache
in another.
Now lets say I want a different listener in each case (maybe one writes something to a database and the other sends a notification over JMS or to some distributed cache). How would I do that? I can't see that I can stick a name on the FooCacheListener as I'd need a different name in one situation vs the other whereas I have just one place here. The only way I can think of doing this is by subclassing FooCache but that seems a really clumsy approach to me.
Cheers
Matt
You might be able to use PrivateModules. Go here and scroll down to How do I build two similar but slightly different trees of objects? It is a way to have two different instances of the same class,which sounds almost exactly what you are trying to do. You could pass in your cachelisteners instead of the "lefty" and "righty" passed in in the example.
There are more links with details from there if it looks like what you want.
Another option might be to inject a factory, which is also discussed in the link above, in the question How do I pass a parameter when creating an object via Guice?