【问题标题】:How to port a Procedural project into an OO project如何将 Procedural 项目移植到 OO 项目中
【发布时间】:2012-01-12 02:58:27
【问题描述】:

我有一个小的 PHP 项目(大约 3000 行),我需要为它制作一个 BASIC UML 模型,从使用图、序列图和状态图的案例开始,可能是类图,并可能用协作图进行扩展.

我是 UML 建模的新手,这是我第一次从实现中制作模型,而不是相反(不是很有用,除非您从事逆向工程,但无论如何,这是一个任务)

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

我的项目也是在 Eclipse IDE 上编写的,有人知道它有什么插件可以帮助我完成这个 UML 建模任务吗?

【问题讨论】:

  • UML 有很多含义。所以你想创建一个类图并将实现变成一个面向对象的?另外:你唯一的任务是做 UML 建模还是实现?
  • 任务是拿一个我做的项目(用PHP编写)并制作一个基本的UML模型(包括使用案例、流程图、文档等等)。最简单的方法是将我的项目设计修改为面向对象,使用 UML 工具生成类图并从中开始工作,或者从模型开始一个新项目,然后执行。在任何情况下,我都需要带有流程图和使用案例的模型以及相应的实现。
  • 也许您可以编辑您的问题并准确说明您需要的内容(序列图、类图、用例图……)这是对 UML 图的各个方面的简洁介绍:@987654321 @
  • 你说得对,我应该更具体一些。不需要部署图,也不需要活动,状态机和协作图是可选的,不必非常详尽。我想我的模型以使用图和类图的情况为中心(如果我猜是 OO),序列图也应该简单整洁,我的问题是 OO 项目对我有多大好处,因为它反对程序 I '已经实施...

标签: oop modeling uml procedural-programming


【解决方案1】:

停止

您以前从事过面向对象编程吗?

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

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

不要跳到 U.M.L.,这么快。 U.M.L.是制作文档, 你脑子里想什么。

您必须首先考虑,您要对网站做什么, 之后,记录和模拟您的新网站将如何运作。

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

您有一个正常工作的网站,并且想要替换它。 主要有两种方法。

(1) 从零开始:

如果您有 O.O. 方面的经验,并且可以忘记旧网站,请继续使用它, 并使用 O.O. 或一些现有的框架从头开始创建一个新网站。

(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();

?>

(更多内容)。

【讨论】:

  • 好吧,现在中间步骤就是我所在的位置:我的文件中不包含函数或常量定义以外的任何内容,而是通过调用这些函数生成网页的文件(.phtml 文件主要是 html 内容,因为从结构上讲,网站不是那么动态)
  • 顺便说一句:非常好的答案,我几乎接受它,但它遗漏了一些东西:现在你知道我在哪里,我应该进行迁移吗? (此外,该网站甚至不在线,它只是我在大学的一个科目的一个项目)
  • @elcodedocle:您可能需要刷新页面。我为每个代码添加了一个类。它没有完成。您可能想继续,因为它可能对您未来的类似(工作)项目有所帮助......
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2011-02-27
    • 1970-01-01
    • 2014-07-31
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多