I’m a little confused about something in Ember with the object model, specifically relating to controllers. I have a base class with a property that is an array and I’m extending two controllers from the base class. The array on the base class seems to persist across both instances of the base class; although, I thought Ember would make them two separate instances of the class. Crude example: http://emberjs.jsbin.com/firovahoxera/1/
Shouldn’t the base class instantiate as two different instances or am I thinking about this wrong? Thanks in advance.
Array's are objects in the sense that adding it to the controller adds a reference to that array to all instances of the controller.
If you want an instance on all of your controllers you can create the array on init.
App.BaseClassController = Ember.Controller.extend({
setupPersists: function(){
this.set('persists', []);
}.on('init')
});
http://jsbin.com/firovahoxera/2/edit
Related
If I have a class that inherits from a base class, can I use that base class as a variable type in c++?
class Component {
// Code here
};
class TransformComponent : public Component {
// Code here
};
class Entity {
// Code here
Component *getComponent(Component *searchComponent) {
// Code Here
}
};
as you can see here, I am using the base class "Component" as a return type and a variable type. The problem is that the user may input a "TransformComponent". The only reason I am asking this is because the "TransformComponent" class inherits from the "Component" class and there might be a way to do this?
<Entity>.getComponent(Component &TransformComponent());
The answer I'm looking for is one that works both for the return type, and the variable type.
Absolutely! It's one of the beauties of OOP. Your instanced class of type TransformComponent is both an instance of Component as well as TransformComponent.
If you had some function that returned a type of Component, this could return any class derived from Component as a Component! If you later wanted to refer to it as its sub-class, you might have to check its type and then cast to it, but what you want is absolutely possible, and you're going the right way about it.
In fact, in the example you describe, were you are using Component and the user might pass a TransformComponent, all of the base methods and properties that the Component possesses will be possessed by TransformComponent too. It will look and feel as if it was a Component, with all the benefits of being one.
The only time a problem will arise is if you specifically want to access the features of a TransformComponent, and the user passed a Component. The parent class doesn't know about the sub-class stuff, because it isn't an instance of one, it will throw up errors for you. Sub-classes build upon the base class, so they have all the base-class stuff, plus more. Basically its only an issue when your example is reversed.
Your Entity.getComponent() method suggests that it only cares that the provided argument is a Component ... not any specialization, such as TransformComponent, of that original class.
So, if you find yourself writing logic that actually cares that "this Component might actually be a TransformComponent," then "warning bells should be going off." Create method definitions within the class that are as specific as possible.
I am facing this problem:
An upstream application defines a class (e.g. box), and a member (say property) with a base class type. I would make a derived class for that member, add new members and methods without updating their application.
Essentially I do box->property = make_shared<myProperty>(). Is there a way to keep the interface of calling the members and methods the same? That is, to access a property using box->property->length or box->property->GetWeight(), rather than dynamic_pointer_cast<myProperty>(box->property)->GetWeight(). The challenge here is they won't update the base property class, and I am not supposed to change box. But we wish to keep the interface the same so our customers won't complain.
Is it possible? If not, how could we do to best keep the main app and my plugin relatively independent while minimize the changes on the customer side? Any suggestions are welcome.
Looks to me like the derived class for that member property violates Liskov's substitution principle.
You mentioned not being able to modify the Box class.
But are you allowed to modify the property base class? I suggest you add your "additional" methods of your derived class to the property base class.
The intent here being that the interface between the base and derived class should be one and the same. So do this only if it makes sense design wise.
Root Problem
I am trying to build a render engine with Vulkan in C++. I am trying to remove the asset meshes, textures, and similar from within the engine structure where I had it for testing purposes to its own separate class. I need to access some of the objects stored in the render engine class because each asset need to be able to add themselves to the vector of vertices, and have access to the same VkDevice for example.
First attempt
The first method I tried was making the asset's constructor take the render engine as one of its arguments, then taking pointers to all the objects that I would need from the render engine for the asset. I still think that this method might work, but I could not declare the class for the render engine before the class for the asset because the render engine needs a vector of all the assets that it was managing in this scenario. Neither could I declare the class for the asset before the class for the render engine because then I could not take the render engine as an argument for the asset's constructor. This feels like it should have a trivial solution, but I could not find one, so I went off to try my second method.
Second attempt
The second method I tried was inheritance. However, I realized as I was experimenting with it that I did not know of a way to get one instance of the render engine class to be the parent of many instances of the asset class. Each asset wanted its own render engine, but I needed for each asset to inherit from the same render engine so that the render engine's objects would be the same for all assets and to avoid having many instances of the render engine to manage.
Final thoughts
I think that there is probably a simple way to do the first option, but I think that in the long run the second option will be more rewarding as it will be easier to manage, cleaner, lead to better organized code, and even teach me more about this great language that I am beginning to explore. I would prefer a solution that involves the second method, but I also don't really know if this is possible because it seems like inheritance does not work on a per instance basis. Finally, I am very much open to new ideas that do not involve either of these options.
Final question
Is there a method of going about inheritance that allows for many instances of one object to inherit from one instance of another object in C++?
Is there a method of going about inheritance that allows for many instances of one object to inherit from one instance of another object in C++?
No, you are mixing apples and oranges here.
Inheritance is a way to build one class off of one or more base classes. It has nothing to do with instances of those classes, and in C++ classes are not objects. So by deriving a class D from another class B you define D but you do not create any instances of either B or D.
When you create objects (instances) of D, these objects will all have the same binary layout that will include data members from B and D. That is, every object of class D will contain a base subobject of class B. You cannot create an object of class D without its base subobject of class B or with a different binary layout, e.g. by referencing B at a different address.
You could share data between different objects by using references or (smart) pointers, like std::shared_ptr. But this has nothing to do with class inheritance. You will have to use references and (smart) pointers as data members of your classes.
Can one instance of a class (not the class itself) be the parent of many instances of another class?
Technically yes... if those many instances are all sub objects within one complete object. Example (which is probably something that no one would ever write):
struct Base {};
struct Second : virtual Base {};
struct Third1 : Second {};
struct Third2 : Second {};
struct Derived : Third1, Third2 {};
Here, there is one instance of Base within Derived, and that Base instance is the (virtual) base of two separate instances of Second, both of which are indirect bases of Derived.
Even though the answer was "yes", I don't know if this would be of use in any use case.
I have several objects which are behaving very similarly. I would like to provide a base implementation for the controllers related to those objects, and then extend those controllers to slightly customize the behaviour. I am doing something like this:
App.BaseIndexController = Ember.ArrayController.extend({
...
});
And now I would use the base controller to define my real object controller:
App.AgentsIndexController = App.BaseIndexController.extend({
....
});
I have two questions:
Are there any general comments discouraging this kind of reuse?
Is the extend mechanism the right way to reuse a generic controller implementation, ensuring that no state data will "leak" between controllers extending the same base controller?
You can do it as you've suggested, but note that any properties defined in App.BaseIndexController will leak over if they've been initiated (check this out for a common mistake)
Mixins can also work well, since they're more reusable. Ember uses Mixins a lot internally so you can't go wrong with that approach.
I have a couple of base/interface classes each of which has several derived classes. I have a need to store metadata on each derived class that has a lot of overlap, but different values.
I also have a Factory class for creating instances of the derived classes that's implemented as a singleton and has a few macros. For example, you'd:
REGISTER_COMPONENT("MyComponent", MyComponentClass);
// create an instance of each component
BaseComponent *base;
foreach(QString component, ComponentRegister::componentList())
{
base = ComponentRegister::create(component);
// do stuff with base
}
The question is: how and where to store the metadata from a solid design viewpoint.
I could store the data in the ComponentRegister as a QMap structure. When someone registers a component, they could also register its metadata with something like
REGISTER_COMPONENT_METADATA("MyComponent", MyMap);
If the QVariant::isValid() for a particular key, you know the metadata is set and available.
Another way would be static class variables or maybe a static class QMap.
I see advantages and draw backs to both. Most of the metadata are things like "path to QResources for this class" which is not tightly coupled to the business logic of the class itself.
Another issue with the static class variable method comes into play with inheritance. You can't enforce overriding of static class variables like you can with pure virtual functions. So if someone forgets...it could be unclear where in the inheritance tree the values are coming from. If you require access to the metadata through a series of pure virtual "getters" then setting of the MetaData is spread across all implementations of the Base class.
With data held, set, and looked up in the Register if you needed to make changes (like changing the root path for resources), you could do so at a single point...in the class registration calls, probably a header or wrapped in a application Utility function. With static data, you'd have to edit each class declaration.
Open to suggestions and thanks!
If data related to an object isn't specific to a single instance, as the path in your example, my designs usually include a class which manages my collection of objects. That's where I put the meta data.
example:
class zoo { std::vector<animals> zoo_animals; size_t count; }
count is metadata about the animals.