如何将Procedural项目移植到OO项目中

时间:2011-12-03 11:01:44

标签: oop modeling uml procedural-programming

我有一个小的PHP项目(约3000行),我需要制作它的BASIC UML模型,从使用图,序列图和状态图的情况开始,可能是类图,并可能通过协作图扩展它

我是UML建模的新手,这是我第一次将模型从实现中取出而不是相反(除非你进行逆向工程,否则不是很有用,但不管怎样,这是一个分配)

现在,我应该如何解决这个问题?如果我的项目有一个OO实现,一些UML工具可以让我的生活变得非常简单,但事实并非如此,所以我想知道我是否应该将我的项目重写为OO以及如何做(我的意思是,是否有一些标准的基础我应遵循的指南或程序?),或者我是否应该按原样制作项目模型(在这种情况下,哪种建模工具最好)。

此外我的项目是在Eclipse IDE上编写的,有人知道它的任何插件来帮助我完成这个UML建模任务吗?

2 个答案:

答案 0 :(得分:5)

<强> STOP

您之前是否使用过面向对象编程?

你使用过O.O.建模技术?

您是否在为PHP文件使用命名空间或函数前缀(“(”mylib.php“,”function mylib_dosomething(){...}“)”)?

不要快速跳到U.M.L. U.M.L.是制作文件, 什么是你脑子里的。

你必须先考虑一下,你打算怎么做网站, 然后,记录并模拟新网站的运作方式。

U.M.L。是一个很棒的工具,但是,如果你脑海中的设计很乱, 那么,你的文档就会搞得一团糟。

您有一个有效的网站,想要替换它。 主要有两种方法。

(1)从Scratch开始:

如果您有使用O.O.的经验,并且您可以忘记您的旧网站,请将其保留, 并使用O.O.或其中一些现有框架从“scratch”开始一个新的网站。

(2)重构

或者,您想要维护当前的网站,并迁移到O.O. 一步步。 ?它也被称为“重构”。

您可以从认为主程序或主要php文件是一个大对象(程序)文件开始,每个库文件也是对象。

示例:

让我们说你有几个php文件。某些文件是页面的主要文件。包含一些文件,甚至在页面文件中重复。其他的,只是具有功能的“库”文件。


<?php
// "indexfuncs1.php"

// this is an auxiliary file for "index.php",
// and has some free procedural code.

echo "indexfuncs1.php: dosomething()";

?>

<?php
// "funcslib.php"

// this is an library file,
// and has only functions and constants,

define ("ANYCONST", "Hello World");

function HelloWorld()
{
  echo ANYCONST;
}

?>

<?php
// "index.php"

// only declarations, doesn't do anything, by itself
//include("funcslib.php");
//require("funcslib.php");
//require_once("funcslib.php");
include_once("funcslib.php");

// this code is in other file, and its executed
include("indexfuncs1.php");

echo "index.php: Hello World";
HelloWorld();

// this code is in other file, and its executed
include("indexfuncs1.php");

?>

然后开始把它们变成这个:

<?php
// "indexfuncs1.php"

// this is an auxiliary file for "index.php",
// and has some free procedural code.

function indexfuncs1_dosomething()
{
  echo "indexfuncs1.php: dosomething()";
}

?>

<?php
// "funcslib.php"

// this is an library file,
// and has only functions and constants,

define ("funcslib_ANYCONST", "Hello World");

function funcslib_HelloWorld()
{
  echo funcslib_ANYCONST;
}

?>

<?php
// "index.php"

// only declarations, doesn't do anything, by itself
//include("funcslib.php");
//require("funcslib.php");
//require_once("funcslib.php");
include_once("funcslib.php");

function index_main()
{
  // this code is in other file, and its executed
  indexfuncs1_dosomething();

  echo "index.php: Hello World";

  funcslib_HelloWorld();

  // this code is in other file, and its executed
  indexfuncs1_dosomething();    
}

?>

还没有O.O.因为,这是一个中间步骤。

Lets start by transform each web page into a single class, without inheritance, without parent classes.

<?php
// "indexfuncs1.php"

// this is an auxiliary file for "index.php",
// and the free procedural code have become a class.

class indexfuncs1 {
    function dosomething()
    {
        echo "indexfuncs1.php: dosomething()";      
    } // function dosomething()
} // class IndexPage    

?>


<?php
// "index.php"

// only declarations, doesn't do anything, by itself
//include("funcslib.php");
//require("funcslib.php");
//require_once("funcslib.php");
include_once("funcslib.php");

class IndexPage {
    function main()
    {
      $myAux = new indexfuncs1();

      // this code is in other file, and its executed
      $myAux->dosomething();

      echo "index.php: Hello World";

      funcslib_HelloWorld();

      // this code is in other file, and its executed
      $myAux->dosomething();
    } // function main()
} // class IndexPage

function index_main()
{
    $myPage = new IndexPage();
    $myPage->main();
} // function index_main(...)

// --> the only allowed global procedural code:
index_main();

?>

(更多信息)。

答案 1 :(得分:2)

要在Eclipse中建模UML,您可能需要查看Eclipse Modelling Tools (MDT)