Home  >  Article  >  Backend Development  >  Extend the class functionality without changing the class code?

Extend the class functionality without changing the class code?

WBOY
WBOYOriginal
2016-08-18 09:16:151138browse

First picture

Extend the class functionality without changing the class code?

The current application uses the yaf framework, and all controllers inherit from Base_controller, but due to the increasing number of later functions (authority management, product management, log management, etc.), Base_controller can no longer be used No more bloated,

Of course, you can create different class files according to different functions, and then initialize them in Base_controller to meet the needs. However, each function is strongly coupled with Base_controller, so I wonder if there is a better solution.

Currently, what I am thinking about is Decoration mode, (because I only know this at the moment, damn it),
Use specific decoration classes (permission management, log management) to decorate Base_controller, so that it has these functions, but due to decoration The pattern requires that the decorator (Base_controller) and the specific decorator both inherit from the same class. However, now Base_controller has inherited from other classes, so Base_controller cannot play the role of the decorator,

So who should I use to decorate the functional classes (authority management, product management, log management) that I have worked so hard to write?

So am I going in the wrong direction, the decoration mode is not suitable here at all, or do I need other designs?

Reply content:

First picture

Extend the class functionality without changing the class code?

The current application uses the yaf framework, and all controllers inherit from Base_controller, but due to the increasing number of later functions (authority management, product management, log management, etc.), Base_controller can no longer be used No more bloated,

Of course, you can create different class files according to different functions, and then initialize them in Base_controller to meet the needs. However, each function is strongly coupled with Base_controller, so I wonder if there is a better solution.

Currently, what I am thinking about is Decoration mode, (because I only know this at the moment, damn it),
Use specific decoration classes (permission management, log management) to decorate Base_controller, so that it has these functions, but due to decoration The pattern requires that the decorator (Base_controller) and the specific decorator both inherit from the same class. However, now Base_controller has inherited from other classes, so Base_controller cannot play the role of the decorator,

So who should I use to decorate the functional classes (authority management, product management, log management) that I have worked so hard to write?

So am I going in the wrong direction, the decoration mode is not suitable here at all, or do I need other designs?

Use php traits

Thanks for the invitation.

The trait mentioned above is indeed a solution, but the key to the problem may not be here.
Although your understanding of decoration mode is inaccurate, it is not a big problem and is not the key

I have never encountered the problem of BaseController being bloated in actual development. This is usually the result of the level (or realm) of the developer. Let alone not changing the class code, even if the class code is changed, this problem will not occur. It cannot be solved, but must be reconstructed.

Usually BaseController is bloated because many methods that should not be provided by Controller are declared. These methods should probably be declared in Model or belong to Helper. This is the key problem. Model is shared, so its methods can be used in any Controller. And if the method that should be defined by the Model is placed in the Controller, and the Controller is not public, the simplest solution at this time is to put it in the Base. The long-term accumulation is the result you see now.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn