polymorphism

django-polymorphic Filter by child type

旧街凉风 提交于 2019-12-23 05:30:07
问题 I have models structure like below: class MyObject(PolymorphicModel): group = models.ForeignKey(Group) class Group(PolymorphicModel): pass class SpecialGroup(Group): pass Now, I would like to select all MyObjects, which group is of type SpecialGroup. Is it possible to achieve it with QuerySet API, without running raw SQL? The only working solution I came up with was by running additional 'select' SQL query using .extra(). Thanks in advance, Cheers! 回答1: Internally, django_polymorphic uses

Observer pattern with type information (C++)

社会主义新天地 提交于 2019-12-23 04:34:38
问题 i'm trying to implement a custom version of an Observer pattern in c++. These are my classes (just interface): class Observer{ public: void observe(std::string behaviour, Observable * observable); void stopObserving(std::string behaviour, Observable * observable); virtual void notified(std::string behaviour, Observable * observed); // ... }; class Observable{ public: void notifyBehaviour(std::string behaviour); // ... }; And can be used like this: class A : public Observer{ public: void

Entity Framework Polymorphism

蓝咒 提交于 2019-12-23 03:19:11
问题 I wanted some help with trying to implement a simple polymorphic relationship using Entity Framework. An example of a relationship I would like to implement: Comment table ItemType ('Video', 'User') ItemID Body Video has many Comments User has many Comments No idea the best way to do this, I come from a Ruby on Rails way of thinking. 回答1: This does not call for polymorphism (inheritance). You state correctly: Video has Comments. Inheritance would require: Video is a Comment. 回答2: Your basic

Java polymorphism - how to call same method in different classes

折月煮酒 提交于 2019-12-23 02:55:27
问题 So I'm making a game in java, and I have objects all with the 2 methods; update() and render() . In my main class, where the game loop resides, I have to call both the update() and render() methods for every object that is updatable and renderable. Is there any way to set up some form of interface where I can call the methods once and it will call it in all implemented objects? 回答1: The other answers are correct, however it would be much better to use the composite pattern: public interface

Sending in an object of type Object instead of String - Polymorphism

孤街浪徒 提交于 2019-12-23 02:47:26
问题 I've an existing method which looks like this: public void parseMessage(String message){ ... ... ... } and the method is called by calling it as shown below String message; parseMessage(message); I need to modify it for it to process a new type of message. The parser for the new type of message which is called from the parseMessage method expects some properties first before it can parse the message. What i am thinking of doing is passing the message as an object that looks like this public

refactoring and removing case statements when circling over an enum structure

放肆的年华 提交于 2019-12-23 02:42:16
问题 An enum structure declared in its own class is a member variable to the business logic class. That enum basically represents the state of that other class. Although I have revisited the issue several times, replacing, or getting rid of those case statements proves quite frustrating to me. Several business logic methods simple iterate over the enum and change the state of that class by assigning another value of the same enum, and other properties. public enum MyEnum{ A,B,C,D } The business

REST API and polymorphism

半世苍凉 提交于 2019-12-23 02:42:10
问题 I have classes hierarchy in JAVA that should be available through a rest API for an angularJS SPA application (the data persisted in PostgreSQL DB) the classes hierarchy looks as follows: PERSON BASE ABSTRACT CLASS contains name and and age CHILD EXTENDS PERSON contains favorite TV show PARENT EXTENDS PERSON contains a list of children GRANDPARENT EXTENDS PERSON contains a list of parents The UI looks like an hierarchy tree, where you can view all the data and edit it. I could thought of

Service - client interface, architecture advice

拜拜、爱过 提交于 2019-12-23 01:28:10
问题 I have a Windows WCF serivce and Web client. My service has one method [OperationContract] SubmitOrder(OrderInfo info).... // class used to pass all relevant data [DataContract] class OrderInfo { [DataMember] OrderType Type; // general order data } It was great until I have introduced new order types (controlled by OrderInfo.Type property). You can think of new order type as derived from general order (in terms of behaviour). Each new order has some additional properties. What is the best

Service - client interface, architecture advice

若如初见. 提交于 2019-12-23 01:28:05
问题 I have a Windows WCF serivce and Web client. My service has one method [OperationContract] SubmitOrder(OrderInfo info).... // class used to pass all relevant data [DataContract] class OrderInfo { [DataMember] OrderType Type; // general order data } It was great until I have introduced new order types (controlled by OrderInfo.Type property). You can think of new order type as derived from general order (in terms of behaviour). Each new order has some additional properties. What is the best

What should be done with inherited factory methods?

末鹿安然 提交于 2019-12-22 20:04:20
问题 Suppose I have a class BasicDate , and a subclass of BasicDate called EuroDate . The difference between the classes is month-day-year versus day-month-year. I know it'd probably be better to just have methods on the same class to output them differently... but that's not the point of this question. BasicDate has the following init method : -(id)initWithMonth:(int)m andDay:(int)d andYear:(int)y { if(self = [super init]) { /*initialize*/ } return self; } And the matching factory method then