简体   繁体   English

理解“行为”和方法的放置

[英]Understanding 'behaviors' and placement of methods

I would like to better understand how to structure methods in a simple inventory manager program that has inheritance. 我想更好地了解如何在具有继承的简单库存管理器程序中构造方法。

Context: products in a retail environment are assigned a location when not on the sales floor. 上下文:零售环境中的产品在不在销售场所时被分配一个位置。 Think of the location as a divided bookshelf with rows and columns, like this: 将该位置视为具有行和列的分隔书架,如下所示:

在此输入图像描述

An action the program must execute is adding a product to a location. 程序必须执行的操作是将产品添加到某个位置。 Such as assigning the red star to location 10/2/3 in the graphic above. 例如将红色星分配到上图中的位置10/2/3。 My question is, is this a 'behavior/method' of the worker, the bin, or the product? 我的问题是,这是工人,垃圾箱还是产品的“行为/方法”? When you think in terms of execution, how do you decide which class? 当你从执行的角度思考时,你如何决定哪个班级? The worker can put the product...the product can be placed ...and the bin can receive . 工人可以放置产品......可以放置产品......并且垃圾箱可以接收 It seems that it could be implemented in all 3. 它似乎可以在所有3中实施。

My mind wants to tell me that I should implement this at the bin level as the bin should ultimately be in-charge of what gets placed in it. 我的想法是告诉我,我应该在bin级别实现这个,因为bin应该最终负责放入它的内容。 For example, when it receives a request to store an item it makes sure it isn't storing an improper product, or a non-product. 例如,当它收到存储项目的请求时,它确保它不存储不正确的产品或非产品。 Get what I mean? 得到我的意思?

What about the argument that the product could be responsible to check itself to make sure it's proper and even the worker ensuring it has a proper product?? 那个产品可以负责检查自己以确保它是正确的甚至工人确保它有一个合适的产品的论点怎么样? Something tells me that perhaps there should be a super form of validation and then each one implements its own version of it? 有些东西告诉我,也许应该有一种超级形式的验证,然后每个都实现它自己的版本?

Maybe I'm not thinking about this correctly. 也许我没有正确地思考这个问题。 How do you think about it? 你觉得怎么样? Am I over simplifying it? 我是在简化吗? Under simplifying? 简化下? Are there rules for this sort of thing or a best practice? 这类事情或最佳做法是否有规则?

The real answer is "it depends". 真正的答案是“它取决于”。 I think many developers struggle with this early on. 我想很多开发人员都很早就开始挣扎。

It also depends on the ever changing requirements. 它还取决于不断变化的要求。 Your first solution may make lots of sense initially but not make sense 2 years later as you implement new features. 您的第一个解决方案最初可能会有很多意义,但在您实施新功能后2年后才有意义。

My best recommendation is to purchase (or borrow) a copy of Martin Fowler's book on Refactoring and read his Movie Rental example in the (I believe) first chapter. 我最好的建议是购买(或借用)Martin Fowler关于重构的书的副本,并在(我相信)第一章中阅读他的电影租赁示例。 He shows a simple example and how he refactors it over time due to changing requirements. 他展示了一个简单的例子,以及由于需求的变化,他如何随着时间的推移重构它。 (His trivial example exhibits some of the same concerns you express. Should the rental class, movie class, or customer class handle which requirements.) Not only should you read his example, you should follow along in your own IDE. (他琐碎的例子展示了你表达的一些相同的担忧。租赁班,电影班或客户班级应该处理哪些要求。)你不仅应该阅读他的例子,你应该在你自己的IDE中进行。 And you should attempt to work it on your own to see what you come up with given the same changing requirements. 而且你应该尝试自己动手,看看你提出的相同变化要求。

This might start to give you a sense of what things to worry about initially and how to adapt your code over time. 这可能会让您了解最初需要担心的事项以及如何随着时间的推移调整代码。 You'll make mistakes and learn to recognize "bad smells" in your code. 你会犯错误并学会识别代码中的“难闻气味”。 This will inform your design decisions in the future. 这将在未来为您的设计决策提供信息。 The book also will help you address those bad smells that you already have. 这本书还将帮助你解决你已经拥有的那些难闻的气味。

Here's a link: http://refactoring.com 这是一个链接: http//refactoring.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM