[dW] 다언어주의 Java -> Javascript :: 2008/04/29 00:02
== 원문 : http://innolab.tistory.com/1173513751
==
이번 문제는 단순합니다. 초보자 책에서도 흔히 볼 수 있는 도형 그리기 문제입니다. 하지만 익숙한 문제를 익숙하지 않은 언어, 익숙하지 않은 방법을 사용해 해결해 보면 어떨까요? 자바, C#, 루비, 파이썬, 자바스크립트, 그루비, Erlang, 스칼라, ML, 해스켈(Haskell), 리스프, Io, 펄 등 다양한 언어를 이용해 다양한 방법으로 프로그램을 작성할 수 있을 것입니다.
- 의도적으로 디자인 패턴을 마음껏 사용해 보는 것은 어떨까요?
- 루비 등의 오리 타입(Ducking Type)을 한번 써보세요. 타입으로서 인터페이스는 과연 어떤 의미를 지니고 있는 것일까요?
- 함수형 언어를 이용해 작성하면 프로그램이 어떤 모습이 될까요?
- 자바스크립트로는 어떻게 프로그램을 구조화할 수 있을까요, 클래스 상속을 사용할까요, 프로토타입 기반 상속을 사용해 볼까요?
제가 자바로 작성해 본 간단한 예제가 있습니다. 어떤 언어, 어떤 방법도 좋습니다. 다양한 언어의 상상력에 여러분의 상상력을 더해 주세요.
<html>
<head>
<body>
<script language="JavaScript">
function AbstractShape(x, y){
this.x = x;
this.y = y;
}
AbstractShape.prototype.moveTo = function(x, y)
{
this.x = x;
this.y = y;
}
AbstractShape.prototype.moveBy = function(dx, dy)
{
this.x += dx;
this.y += dy;
}
AbstractShape.prototype.toString = function(x, y)
{
return "[X : " + x + " Y : " + y + "]";
}
//Rectangle
function Rectangle(x, y, width, height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
Rectangle.prototype = new AbstractShape();
Rectangle.prototype.draw = function()
{
return "Drawing Rectangle - " + this.toString();
}
Rectangle.prototype.toString = function()
{
return AbstractShape.prototype.toString(this.x, this.y) + " [Width : " + this.width + ", Height : " + this.height + "]";
}
//Circle
function Circle(x, y, radius)
{
this.x = x;
this.y = y;
this.radius = radius;
}
Circle.prototype = new AbstractShape();
Circle.prototype.draw = function()
{
return "Drawing Circle - " + this.toString();
}
Circle.prototype.toString = function()
{
return AbstractShape.prototype.toString(this.x, this.y) + " [Radius - " + this.radius + "]";
}
function test(){
var re = new Rectangle(0,0,10,10);
var m = re.draw();
re.moveBy(10, 20);
var m1 = re.draw();
re.moveTo(100, 130);
var m2 = re.draw();
document.write(m + "<br>" + m1 + "<br>" + m2 + "<br>");
var cir = new Circle(10,10,10);
var n = cir.draw();
cir.moveBy(10, 20);
var n1 = cir.draw();
cir.moveTo(100, 130);
var n2 = cir.draw();
document.write(n + "<br>" + n1 + "<br>" + n2);
}
</script>
<button onClick="JavaScript:test()">클릭</button>
</body>
</head>
</html>
웹 플밍이 좋아서 JavaScript로 짜 보았습니다. 마지막 부분을 더욱 신경 써서 했어야 하는데..^^
부족한 부분은 더욱 보안 하겠습니다! ^^ 즐거운 코딩~ ㅋ




