duankange2433 2010-12-21 17:51
浏览 27

如何处理数据对象的多个视图? 哪种设计模式可以接受?

I have a person object.

class Person {
    private $name;

    ...
}

I need to be able to change how they are displayed on the front-end ( visitors have control ). They can choose list view, grid view, photo view for example.

class PersonDisplay {
    public function displayList() {
        // Query database
        // Output html to display in list mode
    }

    public function displayPhoto() {
        // Query database
        // Output html to display in photo mode
    }
}

Is this an acceptable way to handle the presentation of the items on the front-end or is there a specific design pattern I should be researching to help me with this task? Does anyone have any suggestions or ideas where this could go wrong or if this could potentially make maintenance a nightmare?

The Person object was just an example very similiar to what I am using.

  • 写回答

2条回答 默认 最新

  • dongzexi5125 2010-12-21 18:59
    关注

    I feel like a lot of folks get swept up in the "best practice wave" where it's always best to go by what the elite programmers say. Here's my opinion:

    If it works and it's pretty elegant, go with it.

    As long as your solution isn't "hackish" and will scale as the volume of usage your app sees increases, whatever you come up with is fine.

    In terms of which will be most maintainable, you've got two fundamentally different views: grid and list. If you assume that you're going to need two separate chunks of HTML for each, as long as you're not duplicating code up and down, then it's fine. If you do see that you're duplicating a lot of code, make a private function in PersonDisplay and centralize the logic there.

    Frankly, it seems like this bit of code isn't going to grow much farther past, say, a thousand lines of code (max). Use some common sense and take a reality check every now and then. If you see that you're creating too many functions, merge some functions. If you're writing duplicate code, move that code out into a separate function. You really can't screw this one up too much.

    As the saying goes, "don't sweat the small stuff." You can afford to have marginally imperfect code every now and then.

    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数