- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
关闭。这个问题需要更多 focused .它目前不接受答案。
想改进这个问题?更新问题,使其仅关注一个问题 editing this post .
4年前关闭。
Improve this question
我是 SOLID 原则的新手,但我理解它。我的主要问题是很难设计我的类(class)来遵循 SOLID,特别是依赖倒置。有时将整个逻辑写入程序模式而不是使用 SOLID 很容易。
例如:
假设我们正在创建一个考勤监控系统,我们有逻辑(或程序)来扫描员工指纹,获取它的 ID,确定它是否有效,确定他在什么时间,将登录信息写入数据库,并显示它是否成功。
用一堆“if else”、循环和切换以程序方式编写它很容易。但在 future ,我将遭受“代码债务”。
如果我们在这里应用 SOLID 原则。我知道我们需要某种对象,例如“AttendanceServiceClass”,该对象具有“scanEmployeeID()”、“processthislogin()”或“isItsucessful()”等方法。而且我知道这个类依赖于存储库、用户信息和其他对象。
基本上我的问题是分析类的设计及其依赖关系
分析您的类(class)设计的逐步方法是什么?
对不起我的英语不好。
最佳答案
不是专门关于 SOLID,但值得一提的是一个非常有趣的 OOP-培训 Jeff Bay 的方法:Object Oriented Calisthenics .这个想法是,您可以尝试在非现实生活中的小型项目上遵循一套非常严格的规则。
The Rules
1. One level of indentation per method
2. Don’t use the ELSE keyword
3. Wrap all primitives and Strings
4. First class collections
5. One dot per line
6. Don’t abbreviate
7. Keep all entities small
8. No classes with more than two instance variables
9. No getters/setters/properties
By suspending disbelief, and rigidly applying these rules on a small,1000 line project, you’ll start to see a significantly differentapproach to designing software. Once you’ve written 1000 lines ofcode, the exercise is done, and you can relax and go back to usingthese 9 rules as guidelines.
This is a hard exercise, especiallybecause many of these rules are not universally applicable. The factis, sometimes classes are a little more than 50 lines. But there’sgreat value in thinking about what would have to happen to move thoseresponsibilities into real, first-class-objects of their own. It’sdeveloping this type of thinking that’s the real value of theexercise. So stretch the limits of what you imagine is possible, andsee whether you start thinking about your code in a new way.
关于design-patterns - 如何实践OOP设计的SOLID原则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16638873/
GitOps描述了一种使用植根于 Git 版本控制系统的方法来操作和管理软件的方法。使用基于 GitOps 的工作流,通过要求将系统的特征定义为 Git 存储库中的文件,可以更轻松地开发、部署、维护和
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 6 年前。
命令行货币转换器应用程序,提示用户输入源货币、源货币代码和目标货币代码,例如 C:\workspace> java CurrencyConverter 100.50 EUR GBP 应用程序返回源金额
得到这个实体: /** * @ORM\Table(name="shop_payment_details") * @ORM\Entity(repositoryClass="Acme\ShopBund
我有一个原则实体,无需调用 persist 或 flush 即可持久保存到数据库中。 我在下面很简单地重现了这个问题。正如您将看到的,此脚本从名为 MyEntity 的数据库表中加载一行,并获取一个以
在我的编程实践中,我经常遇到客户端和服务器端脚本之间数据重复的问题。 在这种情况下,我们可以讨论客户端的 JavaScript 和服务器端的 PHP 或 C# (ASP.NET)。 比方说,我有一段
简介 我在写关于继承问题的硕士论文并解决了一些问题 表明存在继承问题的指标。 像下面的例子: 示例 public static String getAnimalNoise(Animal animal)
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
当我注意到this answer时,我一直在阅读里氏替换原理。 。它有一个 Circle 和一个 ColoredCircle 类型,其中 ColoredCircle 的构造函数需要一个额外的参数; 颜
这段代码是否违反了DRY原则? if (notAuthorized) { return sendErrorCode(new ForbiddenException()) } else if (n
我在查询中使用 Doctrine 2 的结果缓存来检索用户(消息传递应用程序)的新消息数量: $query->useResultCache(true, 500, 'messaging.nb_new_m
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 8 年前。 Improve this ques
如何设置包含类名的变量,例如 android.util.Log 中的 TAG,同时尊重 Dont-Repeat-Yourself? 以下是一些可能性: 在 Google 代码中,它的常用用法如下 pu
我有以下查询: $roles = array(); $roles[] = 'ROLE_SUPER_ADMIN'; $roles[] = 'ROLE_ADMIN';
下面的代码违反了哪一条 SOLID 原则? public class A { void hello(){ //some code here } } public class B ext
我目前有一个 Message_Repository 类,它有如下方法: getLocationDetailsByID($messageId), getCustomerDetailsById($mess
我不知道它到底叫什么,但现在我将它称为“非空测试”。在 C# 8 中有一个新的行为允许测试一个对象是否不为空,例如: Foo foo = new Foo(); if(foo is { }) {
我正在学习 Doctrine。我在多对多关系中有两个实体 Article 和 Category,我正在尝试获取所有不是特定文章的类别。 文章实体: class Article extends Base
在阅读了一本书和一篇在线文章中有关 SOLID 代码的内容后,我想重构一个现有的类,使其与“SOLID”兼容。 但我想我迷路了,尤其是依赖注入(inject):当我想实例化类的一个对象时,我需要“注入
我的项目中有类似的东西,这个项目已经完成了(它正在运行)我只想知道 SOLID 原则是否可以接受 static public class Tools { static public GetPr
我是一名优秀的程序员,十分优秀!