CSC/ECE 517 Fall 2007/wiki1 6 b2: Difference between revisions
No edit summary |
No edit summary |
||
Line 102: | Line 102: | ||
>>So, Ying is a work-study student! | >>So, Ying is a work-study student! | ||
</pre> | </pre> | ||
== Comparision with C++ == | |||
* Inheritance, in general, is a rigid and hierarchical mechanism, and Ruby mixins is much more flexible than multiple inheritance in c++. | |||
* In Ruby, the same methods can be called to any number of classes regardless of where they are in the inheritance hierarchy | |||
* Modules in Ruby can group together methods and constants which are from different class and modules can also group classes. | |||
* There is a “dreaded diamond” problem in c++ when you try to use multiple inheritance. The link talked about the “dreaded diamond” is given below: | |||
http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.8 | |||
Revision as of 22:21, 14 September 2007
Assignment
Compare the use of Ruby mixins with how one would solve the same problem in Java or C++. In Java, you might use interfaces or the decorator pattern (p. 91 of Head-First Design Patterns). In C++, you would probably use multiple inheritance. Give code in all three languages and compare the solutions on the basis of verbosity and elegance.
Problem Description
For all the people at NC state, we classify them as three class faulty, class staff, and class student. In each of three classes, there is a say method to show their status (i.e., I am a student! in student). Also, for students who work part time on campus as staff, they are supposed to belong to both staff and student.
In c++, we use a subclass workstudy which inherits from both staff and student to represent such work-study students. The object of workstudy can call say methods of staff and student.
In Java, we may define “say” method as interface in Staff and Student and then implement them in class WorkStudy.
In Ruby, we use mixins by grouping two say methods saySta (from staff) and sayStu (from student) together in module Say. After include this module in workstudy class, we can then call saySta and sayStu from any objects of workstudy.
C++ code and output
C++ code
class Staff{ public: string getSta() {return Sta_name;} void setSta(string a) {Sta_name=a;} void saySta() {cout<<getSta()<<" is working here!"<<endl;} private: string Sta_name; }; class Student{ public: string getStu() {return Stu_name;} void setStu(string u) {Stu_name=u;} void sayStu() {cout<<getStu()<<" is studying here!"<<endl;} private: string Stu_name; }; class Workstudy: public Student, public Staff{ public: string get() {return name;} void set(string w) {name= w; setSta(name); setStu(name);} void say() {Staff.saySta(); Student.sayStu(); cout<<"So, "<<get()<<" is a work-study student!"<<endl;} private: string name; }; int main(){ Workstudy* a=new Workstudy(); a->set("Moussa"); a->say(); return 0; }
C++ Output
>>Moussa is working here! >>Moussa is studying here! >>So, Moussa is a work-study student! >>
Ruby code and output
Ruby code
module Say def saySta(name) @name=name puts “#{@name} is a working here!” end def sayStu(name) @name=name puts “#{@name} is studying here!” end end class Workstudy include Say def initialize(name) @name=name end def say saySta(@name) sayStu(@name) puts “So, #{@name} is a work-study student here!” end end c=Workstudy.new(“Ying”) c.say()
Ruby output
Output: >>Ying is working here! >>Ying is studying here! >>So, Ying is a work-study student!
Comparision with C++
- Inheritance, in general, is a rigid and hierarchical mechanism, and Ruby mixins is much more flexible than multiple inheritance in c++.
- In Ruby, the same methods can be called to any number of classes regardless of where they are in the inheritance hierarchy
- Modules in Ruby can group together methods and constants which are from different class and modules can also group classes.
- There is a “dreaded diamond” problem in c++ when you try to use multiple inheritance. The link talked about the “dreaded diamond” is given below:
http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.8
Java solution
Staff.java public interface staff { void saySta(); } Student.java public interface student { void sayStu(); } WorkStudy.java public class WorkStudy implements staff, student{ private String name; WorkStudy(String name){ this.name=name; } public void saySta(){ System.out.println(name+" is working here!"); } public void sayStu(){ System.out.println(name+" is studying here!"); } public void say(){ saySta(); sayStu(); System.out.println("So, "+name+" is work-study student here!"); } } mainclass.java public class mainclass { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub WorkStudy j; j=new WorkStudy("Ying"); j.say(); } }
Comparision with Java
Comparing with java, mixins has two advantages.
Firstly, mixins is like a “mix” of interface and abstract class. Interface is designed in Java to perform the act similar to “multiple inheritance” in C++. It is so purely abstract that either any implement of method or instant variable is not allowed. Meanwhile, abstract class may contain abstract method and non-abstract method but one class can only inherited from one super abstract class. Mixin here provides a more flexible way between them. We may implement methods in module or in class inherited from the module to reduce the code redundancy.
Secondly, the namespace provided by module helps dealing with the situation that a class inherits two methods from different modules with the same name. In Java, such act will throw out an error message.