polymorphism

Developing to an interface with TDD

依然范特西╮ 提交于 2019-12-14 03:46:25
问题 I'm a big fan of TDD and use it for the vast majority of my development these days. One situation I run into somewhat frequently, though, and have never found what I thought was a "good" answer for, is something like the following (contrived) example. Suppose I have an interface, like this (writing in Java, but really, this applies to any OO language): public interface PathFinder { GraphNode[] getShortestPath(GraphNode start, GraphNode goal); int getShortestPathLength(GraphNode start,

Polymorphism in C

拟墨画扇 提交于 2019-12-14 03:42:35
问题 I'm designing a program in C that manipulates geometric figures and it would be very convenient if every type of figure could be manipulated by the same primitives. How can I do this in C? 回答1: You generally do it with function pointers. In other words, simple structures that hold both the data and pointers to functions which manipulate that data. We were doing that sort of stuff years before Bjarne S came onto the scene. So, for example, in a communications class, you would have an open,

Polymorphic many-to-many relation

折月煮酒 提交于 2019-12-14 02:58:22
问题 I couldn't get related models in many-to-many polymorphic relation saved into database. $photo = Photo::find(1); $photo->articles()->attach(2); something like this wouldn't work and gives error: Call to undefined method Illuminate\Database\Query\Builder::articles()' How to do it the right way? Models class Tag extends Eloquent { public function articles() { return $this->morphedByMany('Article', 'taggable'); } public function photos() { return $this->morphedByMany('Photo', 'taggable'); } }

Creating polymorphic lens

我的未来我决定 提交于 2019-12-14 02:09:21
问题 I am able to create a lens for the last field ( c ) in my data types by doing the follow: {-# LANGUAGE DuplicateRecordFields #-} data X1 a c = X1 { a' :: a, b' :: Int, c' :: c } data X2 a b c = X2 { a' :: a, b' :: b, c' :: c } class HavingFieldC x cs ct where c :: Functor f => (cs -> f ct) -> x cs -> f (x ct) instance HavingFieldC (X1 a) cs ct where c = lens (\X1 { c' } -> c') (\X1 {..} v -> X1 {c' = v, ..}) instance HavingFieldC (X2 a b) cs ct where c = lens (\X2 { c' } -> c') (\X2 {..} v ->

Polymorphism vs. division of responsibilities: how to avoid 'switching on type'

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-14 00:26:11
问题 In designing systems with hierarchical relationships, I often run into a problem that begs for polymorphic behavior, but there is more than one type of work that benefits from this polymorphic implementation. For instance, consider a compiler that uses an abstract syntax tree to organize the parsed source for compilation. It's convenient to organize the logic polymorphically, as you might have more than one type of ValueProvider, each of which is responsible for the emission of different code

Syntax for virtual functions

 ̄綄美尐妖づ 提交于 2019-12-13 23:06:00
问题 While making a function virtual in c++ where do I have to write "virtual" keyword? Does it make a difference if I write virtual int function_name instead of int virtual function_name like in java 回答1: While making a function virtual in c++ where do I have to write virtual keyword? In the function declaration, before the function name, and after any attribute specifiers, along with the other specifiers (including the type specifier for the function's return type). The general syntax for a

Call to undefined Builder::save() when saving a polymorphic relationship

China☆狼群 提交于 2019-12-13 21:00:10
问题 I'm trying to save a polymorphic relationship when registering a user, but it returns me: Call to undefined method Illuminate\Database\Query\Builder::save() I have 3 tables on my database: Schema::create('usuarios', function(Blueprint $table) { $table->increments('id'); $table->string('nombreUsuario', 20); $table->string('password', 60); $table->string('email', 30); $table->string('remember_token', 100)->nullable(); $table->integer('cuenta_id'); $table->string('cuenta_type'); $table-

Classes that defacto implement an interface but do not declare the fact

只谈情不闲聊 提交于 2019-12-13 20:55:15
问题 I will illustrate my question with the below posted code. It was created specifically for this purpose. The fact that it uses inner interface and inner classes is immaterial (it was just a convenience). The code compiles and runs as expected. In this code I use inheritance with the sole purpose to declare that a class implements an interface , so later on I can cast them up and use polymorphic behavior. Parent classes already have methods to comply with the interface, so no overriding is

C++ base class pointer calls child virtual function, why could base class pointer see child class member

末鹿安然 提交于 2019-12-13 20:25:22
问题 I think I might be confusing myself. I know class with virtual functions in C++ has a vtable (one vtable per class type), so the vtable of Base class will have one element &Base::print() , while the vtable of Child class will have one element &Child::print() . When I declare my two class objects, base and child , base 's vtable_ptr will pointer to Base class's vtable, while child 's vtable_ptr will point to Child class's vtable. After I assign the address of base and child to an array of Base

How to return Generic type from same method for parent and child classes

假装没事ソ 提交于 2019-12-13 18:28:16
问题 Here is my scenario I've 3 classes. class Animal { public getWeight(){ ..... } public getHeight(){ ..... } } class Dog extends Animal { public getDogType() { ...} } class Cat extends Animal { public getCatType(){....} } And there is a different method which returns an Animal type taking an Object as a parameter public Animal translate(CustomObject o){ ... so many calculations here } Here translate method returns Animal Object, but I need to use the same method to return Dog and Cat types too