这个OOP代码基本上是否正确?

Luk*_*een 0 php oop object-oriented-analysis data-structures

我目前正在尝试将我们的页面模板转换为OOP,我感觉我为导航类提出的内容并不是完全正确的.

  • 这些方法中的一些真的属于扩展类drawNav吗?
  • getMenuBar- > generateMenuBar- > generateMenuItems结构太崩溃?它应该只是getMenuBar,并将所有内容放入generateMenuBar()generateMenuItems()放入getMenuBar()

我调用类和方法的方式:

$drawNav = new drawNav();

$breadcrumbTrail = $drawNav->getBreadcrumbTrail();
$menuBar = $drawNav->getMenuBar();
Run Code Online (Sandbox Code Playgroud)

代码:

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
            }
}
Run Code Online (Sandbox Code Playgroud)

mea*_*gar 6

getMenuBar- > generateMenuBar- > generateMenuItems结构太崩溃?

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

而不是这种古怪:

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

你应该这样做:

        public function getSocialMediaButtons()
        {
            //return the HTML code with the social media buttons 
        }
Run Code Online (Sandbox Code Playgroud)

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

否则,您的代码很好,但有一点需要注意:我希望您"使用社交媒体按钮返回HTML代码"呈现一些外部HTML模板文件,并且您不是在内部编写HTML内联.后端/前端逻辑的良好分离比代码部分的结构更重要; 我宁愿看到程序代码干净地分离业务/视图逻辑,而不是精心设计的面向对象的代码,它们将它们混合在一起.