CSC/ECE 517 Fall 2007/wiki1 6 b2: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
== | == 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. | 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 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 == | == C++ code == | ||
<pre> | <pre> |
Revision as of 22:10, 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 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
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! >>
An example
In Java, we may define “say” method as interface in Staff and Student and then implement them in class WorkStudy.
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.