CSC/ECE 517 Fall 2007/wiki1 6 b2: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
=== C++ code ===
== Question ==
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.
== C++ code ==
<pre>
<pre>
class Staff{
class Staff{
Line 41: Line 43:
</pre>
</pre>


=== C++ Output ===
== C++ Output ==
<pre>
<pre>
>>Moussa is working here!
>>Moussa is working here!

Revision as of 22:08, 14 September 2007

Question

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.

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.