polymorphism

LinQ ORM Data Objects and Inheritance.

I'm thinking about how to do this, but I have several different shapes of Data in my Database, Articles, NewsItems, etc. They All have something in common, they all have IDs (in the DB they're named ArticleID, NewsID etc. ) They all have a Title They all have BodyText. They all have a Status They all have a DateAdded What I'd lik...

Rails Associations, habtm? Polymorphic? Both?

In my Rails app I have three models, Projects, BlogPosts and Images. Projects and BlogPosts can have many linked images and an image can be linked to a Project, a BlogPost or both. What is the best way of setting up the associations to make this work in Rails? ...

Are non-pure virtual functions with parameters bad practice?

I have a base class with an optional virtual function class Base { virtual void OnlyImplementThisSometimes(int x) {} }; When I compile this I get a warning about the unused param x. Is there some other way I should have implemented the virtual function? I have re-written it like this: class Base { virtual void OnlyImplement...

What is the best way to include PHP libraries when using static factory pattern?

I have several static factory patterns in my PHP library. However, memory footprint is getting out of hand and we want to reduce the number of files required during execution time. Here is an example of where we are today: require_once('Car.php'); require_once('Truck.php'); abstract class Auto { // ... some stuff ... public st...

Change return signature via inheritance – Polymorphism

Hi guys Just wondering if there is any way to do the following: public Interface IDataField { object GetValue(); } public Interface IComplexDataField : IDataField { object GetDefaultValue(); } public class MyBase { private IDataField _DataField; public MyBase() { this._DataField = this.CreateDataField();...

In Java, how to I call a base class's method from the overriding method in a derived class?

Hello everybody, I have two Java classes : B, which extends another class A, as follows : class A { public void myMethod() { /* ... */ } } class B extends A { public void myMethod() { /* Another code */ } } I would like to call the A.myMethod() from the B.myMethod(). I am coming from the C++ world, and I don't know ho...

Does OCaml have general map()/reduce() functions?

In Python map() works on any data that follows the sequence protocol. It does The Right Thing^TM whether I feed it a string or a list or even a tuple. Can't I have my cake in OCaml too? Do I really have no other choice but to look at the collection type I'm using and find a corresponding List.map or an Array.map or a Buffer.map or a S...

Does delete work with pointers to base class?

Do you have to pass delete the same pointer that was returned by new, or can you pass it a pointer to one of the classes base types? For example: class Base { public: virtual ~Base(); ... }; class IFoo { public: virtual ~IFoo() {} virtual void DoSomething() = 0; }; class Bar : public Base, public IFoo { public: vi...

Polymorphic factory / getInstance() in Java

I'm aiming to create a set of objects, each of which has a unique identifier. If an object already exists with that identifier, I want to use the existing object. Otherwise I want to create a new one. I'm trying not to use the word Singleton, because I know it's a dirty word here... I can use a factory method: // A map of existing ...

How should I cast for Java generic with multiple bounds?

Is it possible to cast an object in Java to a combined generic type? I have a method like: public static <T extends Foo & Bar> void doSomething(T object) { //do stuff } Calling this method is no problem if I have a class that implements both interfaces (Foo & Bar). The problem is when I need to call this method the object I need...

Unresolved Token in Managed C++

I have a mystery on my hands. I am trying to learn managed C++ coming from a C# background and have run into a snag. If I have a project which includes two classes, a base class Soup and a derived class TomatoSoup which I compile as a static library (.lib), I get unresolved tokens on the virtual methods in Soup. Here is the code: Abst...

How would you refactor this conditional to use polymorphism?

I just finished watching the Google clean code video on YouTube (see link, first article) about removing if statements from your code and using polymorphism instead. After watching the video I had a look at some code that I was writing before watching the video and noticed some places where I could use this method, mainly places where...

Would this be a good case for polymorphism.

Sorry if this is a really basic question but I struggling with how I should attack this. I am trying to wrap up some commands for a OLE object, the basic spec looks like this: Set Window window_id //Units is part of the position setter. [ Position ( x, y ) [ Units paper_units ] ] [ Width win_width [ Units paper_units ] ] ...

What is the difference between Abstraction and Polymorphism

I seem to not understand two OOP concepts very well. Could you explain what abstraction and polymorphism are, preferably with real examples and code? Thank you. ...

C# Interface (Call Method in Different Project/Assembly)

The code below pretty much sums up what I want to achieve. We have a solution which comprises many different projects however we have a need to be able to call methods in projects from projects which are not referenced (would cause circular reference). I have posted previous questions and the code below is pretty much what I have come...

How would you attack this polymorphism string building problem.

Sorry for not so discrptive title, if someone wants to change it go ahead. Also note I have already asked a question about this kind of thing but this one is more about how you would attack it because my attack just doesn't seem to feel right. I am having a problem with building a string up to send to an OLE object in the correct order...

RoR: has_one "or the other"? (Or, polymorphism without the inheritance.)

Hey all, I have something of an interesting requirement for my project. I need a has_one relationship where it is either one class or the other, but without inheritance. I could get away with inheritance if it is the only way, but the two associate records have completely different data and aren't related at all. What I need to figure...

Should we always favor polymorphism over enums?

After watching: The Clean Code Talks -- Inheritance, Polymorphism, & Testing I checked my code and noticed a few switch statements can be refactored into polymorphism, but I also noticed I only used switch statements with enums. Does this mean enums are "evil" in OO-design and should be eliminated with polymorphism? ...

Passing superclass as parameter to method expecting sub class

Hello, I have an object tree that looks something like Ball / \ LegalBall IllegalBall And I have 2 methods: class o { AddBall(LegalBall l) AddBall(IllegalBall i) } in another class I'd like to do the following: o.AddBall(myBall); where myBall is of type Ball. And get it to call the correct method de...

Data schema and query for polymorphic tree in SQL Server

I have run into this problem a couple of times in my career, and have never been very happy with the solution, and am presented with it again on a project I am doing in ASP.Net MVC, C#, SQL Server 2008: Imagine I have a Person type (class). I further have types Mother and Father that extend Person. Father and Mother are very similar: ...