Does the class function be extended without changing the class code?

Source: Internet
Author: User
Currently, the application uses the yaf framework. all controllers are inherited from the Base_controller. However, due to more and more features (such as permission management, product management, and log management ), as a result, Base_controller can no longer be bloated. of course, you can create different functions...

First

Currently, applications useyafFramework, all controllers areInheritanceFromBase_controllerBut due to the increasing number of features (permission management, product management, log management, etc.) in the later stageBase_controllerYou can no longer be bloated,

Of course, you can create different class files based on different functions, and thenBase_controllerBut such functions andBase_controllerStrong coupling, so I want to see if there is a better solution.

What I want now isDecoration mode, (Because this is the only option currently, mom and dad ),
Use specific decoration classes (permission management and log management) for decorationBase_controllerSo that it has these functions, but because the decoration mode requires that the decorator (Base_controller), And the specific modifier inherits from the same class, but nowBase_controllerIt has inherited from other classes, soBase_controllerCannot act as the decorator,

Who can I describe with the features (permission management, product management, and log management) that I have worked hard to write,

So I am wrong. the decoration mode is not suitable here, or do I need other designs?

Reply content:

First

Currently, applications useyafFramework, all controllers areInheritanceFromBase_controllerBut due to the increasing number of features (permission management, product management, log management, etc.) in the later stageBase_controllerYou can no longer be bloated,

Of course, you can create different class files based on different functions, and thenBase_controllerBut such functions andBase_controllerStrong coupling, so I want to see if there is a better solution.

What I want now isDecoration mode, (Because this is the only option currently, mom and dad ),
Use specific decoration classes (permission management and log management) for decorationBase_controllerSo that it has these functions, but because the decoration mode requires that the decorator (Base_controller), And the specific modifier inherits from the same class, but nowBase_controllerIt has inherited from other classes, soBase_controllerCannot act as the decorator,

Who can I describe with the features (permission management, product management, and log management) that I have worked hard to write,

So I am wrong. the decoration mode is not suitable here, or do I need other designs?

Use php trait

Thank you.

The Trait mentioned above is indeed a solution, but the key to the problem may not be here.
Your understanding of the decorative model is not accurate, but the problem is not big, and it is not the key.

In actual development, I have never encountered a very bloated problem with BaseController. this is usually the result of a developer's level (or realm). do not change the class code, this problem cannot be solved even if the class code is changed, but must be reconstructed.

Generally, BaseController is bloated because many methods should not be declared by the Controller. these methods may be declared in the Model or belong to Helper. this is the key issue. The Model is shared, so the method can be used in any Controller. If the method defined by the Model is put into the Controller, and the Controller is not public, the simplest solution is to put it in the Base, the long-term accumulation is what you see now.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.