Mind Forest: Beneath imaginations: 매트랩 태그 글 목록https://forest.nubimaru.com/시간의 토양에 기억의 나무를 심으며2024-03-29T06:28:01+09:00Textcube 1.10.10 : Tempo primoMATLAB 객체 프로그래밍의 특이점inureyeshttps://forest.nubimaru.com/entry/MATLAB-object-orient-programming-and-its-strangeness2009-03-23T20:34:31+09:002009-03-23T20:33:41+09:00<p>MATLAB이 2008a 이후부터 클래스 지원을 필두로 객체를 지원한다. 주 언어가 C++이라 예전 전산물리학 조교할 때와 비선형 계획 수업을 제외하면 MATLAB을 필드에서 제대로 써 보지 않아서, 테스트겸 어제 오늘 이틀동안 여러가지 코딩을 해 보았다.</p>
<p>테스트 결과 MATLAB에서도 객체 프로그래밍이 가능하긴 하지만 원래 객체 기반의 언어가 아니라 몇 년에 걸쳐 구조체부터 하나씩 도입을 해 온 산물이기 때문에 C++이나 자바와는 굉장히 특성이 다르다. 간단하게 정리해 본다. 아래의 지원 목록은 help 를 참조하며 하나씩 만들어가면서 본 것이라 확실하지는 않다. (지원하더라도 괴상하게 지원하는 경우가 많다...)</p>
<h3>객체 프로그래밍을 위해 지원하는 것</h3>
<ul>
<li>Member variable / function</li>
<li>Scope and Methods
<ul>
<li>Public / Private / Protected</li>
<li>Static / Accessor</li>
</ul></li>
<li>Inheritance</li>
<li>Polymorphism</li>
<li>Abstract
<ul>
<li>Subclasses / Superclasses</li>
</ul></li>
<li>Interface</li>
</ul>
<h3>꼭 필요한 것은 아니지만 지원하는 것들</h3>
<ul>
<li>Event / handler</li>
<li>Dynamic methods</li>
<li>Recursion</li>
<li>Properties and attributes</li>
</ul>
<h3>MATLAB에서 기형적으로 지원하는 것들</h3>
<ul>
<li>Properties</li>
</ul>
<p>적어놓고 보면 다 지원하는 것 같은데, 총평을 먼저 하자면 구현이 괴악하다. 다른 언어에서 아주 기본적으로 지원하는 몇몇 부분이 지원되지 않거나 굉장히 특이하게 지원된다. 여러가지 중에서 properties의 경우, MATLAB가 주언어가 아니라서 이해가 참 힘들었다.</p>
<p>Properties 는 객체에서 저장 공간이나 속성을 담당한다. 다른 언어에서 변수를 member variable의 형태로 객체에 포함시켜서 사용하는 것과는 차이가 있는 부분인데, 변수들을 properties라는 일종의 구조체로 묶어서 가지고 있다. 예를 들면</p>
<p><pre class="prettyprint"><code>classdef Trader
properties (SetAccess = protected, GetAccess = protected)
name = 'Trader'
id = 1
end
</code></pre></p>
<p>이런 식으로 기술한다.</p>
<p>그런데 MATLAB에서는 같은 객체 안의 method에서 자신의 property를 참조할 수 있는 직접적인 방법을 제공하지 않는다. (this나 self등이 없다!) 그래서 편법을 써야 하는데, method의 첫 인자로 자신을 넘기는 방법이다.</p>
<p><pre class="prettyprint"><code>function obj=initialize(obj,name)
test = obj.id
end
</code></pre></p>
<p>이렇게 method를 만들면 MATLAB은 첫 인자로 자신을 넘겨주게 되고, 그 객체의 변수값을 직접 억세스하는 방식으로 현재의 값을 읽어오게 된다.</p>
<p>이러한 이상한 특징은 MATLAB의 기본적인 특징 하나와 맞물려 굉장히 재미있는 동작을 만든다. MATLAB은 call-by-reference를 지원하지 않는다. 그래서 위의 예에서 인자로 넘어간 객체는 실제 객체가 아니라 객체의 복사본이다. 따라서 값을 읽을 수는 있는데, <strong>그 값을 수정할 수는 없다.</strong> 객체 안의 method에서 자신의 값을 수정할 수가 없게 된다. 그래서 properties의 값은 constructor에 의해서만 지정이 가능하다. 메모리를 가질 수 없는 객체라니, 객체 프로그래밍의 관점에서 보면 진정 엽기적인 객체 지원이다.</p>
<p>MATLAB에서 제안하는 방법은 객체의 리턴값으로 객체를 재지정하는 것이다. 아래와 같이, 리턴되는 값이 property 값이 변한 객체이므로 그것으로 자신을 재지정하는 식이다.
<pre class="prettyprint"><code>A = Trader;
A = A.initialize();
</code></pre></p>
<p>주윗 사람들의 태반은 이 방법에 동의할 수 없을 것이다. 방법이 없는 것은 아니다. MATLAB에서도 call-by-reference를 사용하는 부분이 있는데, handle 지원이다. MATLAB에서 그래프를 그리거나 UI를 만들 때, 모든 UI object들을 제어하기 위해서 handle이라는 unique number가 존재한다. 그 handle 값을 이용하여 오브젝트들의 핸들을 잡고 내부의 속성을 조정할 수 있다.</p>
<p>따라서 MATLAB 클래스 지원의 상속 기능을 사용하여 객체를 handle을 상속해서 생성하는 방법이 가능하다. 그러면 위의 예에서 obj는 복사된 객체가 아니라 원래 객체의 handle이 된다. 따라서 직접적인 수정이 가능해진다. 이 방법에도 문제점이 있다. 객체를 상속받은 경우 abstract나 interface를 사용할 수가 없다. 또한 여러개의 superclass들을 상속받아서 클래스를 정의하려는 경우, 모든 클래스는 handle을 루트로 가지고 있어야 한다.
<pre class="prettyprint"><code>classdef Trader < handle
properties
name = 'Trader'
id = 1
end
methods (Access = public)
function obj=initialize(obj,name)
obj.name = 'TEST';
end
end
end
</code></pre></p>
<p>대충 이런 식으로 '상식적인' 객체 프로그래밍을 할 준비가 마무리 되었다. 사실 처음 시작이 복잡해서 그렇지 MATLAB으로 객체 프로그래밍을 하는 것은 <span style="color: #E4E4E4">(코딩과 거리가 먼 사람들을 위한)</span> 다른 언어에서 유래를 찾아볼 수 없는 어마어마한 내부 함수들 때문에 꽤 재미있다. 첫인상 정리는 여기까지.</p>
<fieldset style="margin:20px 0px 20px 0px;padding:5px;"><legend><span><strong>크리에이티브 커먼즈 라이센스</strong></span></legend><!--Creative Commons License--><div style="float: left; width: 88px; margin-top: 3px;"><a rel="license" href="http://creativecommons.org/licenses/by-nc-nd/2.0/kr/" target=_blank><img alt="Creative Commons License" style="border-width: 0" src="http://i.creativecommons.org/l/by-nc-nd/2.0/kr/88x31.png"/></a></div><div style="margin-left: 92px; margin-top: 3px; text-align: justify;">이 저작물은 <a rel="license" href="http://creativecommons.org/licenses/by-nc-nd/2.0/kr/" target=_blank>크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스</a>에 따라 이용하실 수 있습니다.
<!-- Creative Commons License-->
<!-- <rdf:RDF xmlns="http://web.resource.org/cc/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<Work rdf:about="">
<license rdf:resource="http://creativecommons.org/licenses/by-nc-nd/2.0/kr/" />
</Work>
<License rdf:about="http://creativecommons.org/licenses/by-nc-nd/">
<permits rdf:resource="http://web.resource.org/cc/Reproduction"/>
<permits rdf:resource="http://web.resource.org/cc/Distribution"/>
<requires rdf:resource="http://web.resource.org/cc/Notice"/>
<requires rdf:resource="http://web.resource.org/cc/Attribution"/><prohibits rdf:resource="http://web.resource.org/cc/CommercialUse"/></License></rdf:RDF> --></div></fieldset><div class="fb-like" data-href="https://forest.nubimaru.com/entry/" data-layout="standard" data-action="like" data-show-faces="true" data-share="true"></div>
<div class="fb-comments" data-href="https://forest.nubimaru.com/entry/" data-width="630" data-numposts="2" data-colorscheme="light"></div><p><strong><a href="https://forest.nubimaru.com/entry/MATLAB-object-orient-programming-and-its-strangeness?commentInput=true#entry2041245WriteComment">댓글 쓰기</a></strong></p>