Extend this class of functionality without changing the class code?

Source: Internet
Author: User

First

At present, the application is the yaf framework, all the controllers are 继承 self Base_controller , but due to more and more late functions (rights management, product management, log management, etc.), resulting in Base_controller no longer bloated,

Of course, it is possible to create different classes of files according to different functions, and then initialize them to Base_controller meet the requirements, but such functions and Base_controller strong coupling, so I think there is no better solution.

At the moment I'm thinking 装饰模式 , (because this is the only one, mom's egg),
Decorate with specific decorative classes (Rights Management, log management) to Base_controller make them have these functions, but because the decorative mode requires the decorator ( Base_controller ), and the specific decorators are inherited from the same class, but now Base_controller has been inherited from other classes, so Base_controller can not act as the role of the decorator,

So I worked hard to write the functional class (Rights management, product management, log management) to decorate who,

So is not my direction is wrong, decorative mode is not suitable here, or the need for other design?

Reply content:

First

At present, the application is the yaf framework, all the controllers are 继承 self Base_controller , but due to more and more late functions (rights management, product management, log management, etc.), resulting in Base_controller no longer bloated,

Of course, it is possible to create different classes of files according to different functions, and then initialize them to Base_controller meet the requirements, but such functions and Base_controller strong coupling, so I think there is no better solution.

At the moment I'm thinking 装饰模式 , (because this is the only one, mom's egg),
Decorate with specific decorative classes (Rights Management, log management) to Base_controller make them have these functions, but because the decorative mode requires the decorator ( Base_controller ), and the specific decorators are inherited from the same class, but now Base_controller has been inherited from other classes, so Base_controller can not act as the role of the decorator,

So I worked hard to write the functional class (Rights management, product management, log management) to decorate who,

So is not my direction is wrong, decorative mode is not suitable here, or the need for other design?

Using PHP's Trait

Thank you for your invitation.

Upstairs, Trait is really a solution, but the key to the problem may not be here.
Your understanding of decorative patterns is inaccurate, but not very important.

I have never encountered the problem of Basecontroller being bloated in actual development, usually this is the result of the level (or realm) of the developer, not to mention not changing the class code, even if the problem of changing the class code is not solved, but to refactor.

Often the basecontroller is bloated because many of the methods that should not be provided by the Controller are declared, and these methods may be declared in the model or referred to as helper, which is the key issue. Model is common, so its methods can be used in any controller. And if the method defined by the model is placed in the controller, and the controller is not public, the simplest solution is to put it in base, and the long-term accumulation is the result 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.