【问题标题】:Is this OOP code fundamentally correct? [closed]这个 OOP 代码基本上正确吗? [关闭]
【发布时间】:2012-08-29 18:14:43
【问题描述】:

我目前正在尝试将我们的页面模板转换为 OOP,我觉得我为导航类提出的内容从根本上来说并不完全正确。

  • 其中一些方法真的属于drawNav 的扩展类吗?
  • getMenuBar -> generateMenuBar -> generateMenuItems 结构是否太多故障?是不是应该只是getMenuBar,把generateMenuBar()generateMenuItems()的所有内容都放到getMenuBar()里面?

我调用类和方法的方式:

$drawNav = new drawNav();

$breadcrumbTrail = $drawNav->getBreadcrumbTrail();
$menuBar = $drawNav->getMenuBar();

代码:

class drawNav { 

            public function __construct() {
                //I’ve not nothing to put here…                                
            }

            public function getMenuBar()
            {
               return $this->generateMenuBar();                          
            }

            public function getBreadcrumbTrail()
            {
                return $this->generateBreadcrumbTrail();                           
            }

            public function getSocialMediaButtons()
            {
                return $this->generateSocialMediaButtons();    
            }

            private function generateSocialMediaButtons()
            {
               //return the HTML code with the social media buttons
            }

            private function generateMenuBar()
            {
               //Generate the HTML containing the menu and social media buttons
               $this->generateMenuItems();
               $this->getSocialMediaButtons();
               //Generate the HTML closing tags for the container for the menu and social media buttons
            }

            private function generateMenuItems()
            {
                //Call to the database and generate each individual menu item and its dropdown
            }

            private function generateBreadcrumbTrail()
            {
                //Generate the HTML containing the breadcrumb trail
                $this->generateBreadcrumbs();
                //Generate the HTML closing tags for the container for the breadcrumbtrail
               }

            private function generateBreadcrumbs()
            {
                //Call to the database and generate the pieces of the breadcrumb trail
            }
}

【问题讨论】:

  • 似乎这个问题在codereview.stackexchange.com上会更好
  • @Travesty3 一开始我也是这么想的,但是好像SO上有很多类似的问题
  • 如果您的方法都不依赖于类的实例,为什么不将它们设为静态。
  • @AlexLunix:在哪里提到没有任何方法依赖于类的实例?
  • @Travesty3 看看它们以及它们的作用,似乎类更像是一种将它们组合在一起的方式,而不是需要实例的类。据我们所知,这里也没有属性,构造函数中也没有。

标签: php oop data-structures object-oriented-analysis


【解决方案1】:

getMenuBar -> generateMenuBar -> generateMenuItems 结构是否过于崩溃?

是的。绝对没有理由对包含私有方法的单行公共方法进行一对一映射。这不在任何人的最佳 OOP 实践列表中。

而不是这种怪异:

        public function getSocialMediaButtons()
        {
            return $this->generateSocialMediaButtons();    
        }
        // ...
        private function generateSocialMediaButtons()
        {
           //return the HTML code with the social media buttons
        }

你应该这样做:

        public function getSocialMediaButtons()
        {
            //return the HTML code with the social media buttons 
        }

如果您担心能够在公共接口中混合和匹配私有方法,这在以后很容易重构。但是编写单行公共方法,其唯一目的是调用具有几乎完全相同名称的私有方法,这是一种巨大的代码气味。

否则,您的代码很好,但有一个警告:我希望您的“返回带有社交媒体按钮的 HTML 代码”正在呈现一些外部 HTML 模板文件,并且您没有在类中内联编写 HTML .后端/前端逻辑的良好分离比代码部分的结构更重要;我宁愿看到将业务/视图逻辑清晰分离的过程代码,也不愿看到精心设计的将它们混合在一起的面向对象代码。

【讨论】:

  • +1 表示代码逻辑和模板的分离。
【解决方案2】:

我真的不认为你如何分离你的方法有什么大问题。我个人宁愿让类方法处理一些特定的操作,然后使用其他方法将“构建块”方法组合成更复杂的操作。例如,如果您需要更改面包屑数据库逻辑,则只需在一个方法中进行更改,并且该方法从其他方法中抽象出来,足以使其不需要更改。

【讨论】:

    【解决方案3】:

    你所拥有的似乎很好。

    我会质疑你是否有一个 Navigation 类,因为这都可以添加到其中,或者作为 Navigation 类的扩展,以保持清洁。

    【讨论】:

    • 我没有导航类,就是这样。您是否建议将其称为 Navigation 而不是 drawNav?
    • 不,你可以按你喜欢的方式命名它,但命名表明你也有一个导航类。
    猜你喜欢
    • 2011-06-26
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 2020-10-22
    相关资源
    最近更新 更多