如何设计应用程序时牢记 SOLID 原则和设计模式
假设 ruby 中的应用程序启动时有两种模式:命令行模式和文件模式。
当给定参数 ruby myprogram input.txt output.txt 时,它会根据输入文件中的某些命令生成输出。此外,当未提供任何参数时,它会向我们显示命令提示符。使用以下命令。
create_class_with_capacity 40
create_student_with_marks Alex 70
create_student_with_marks Mathew 30
create_student_with_marks John 55
..
create_student_with_marks Sylvia 70 etc...
fail_student_roll_no 12
=> Student with roll number 12 #{student} failed
give_marks_to_roll_no 70 1
=>Student with roll number 1 Alex got 70 marks
find_all_students_with_marks 70
=> Alex, Peter , Russell , Mark etc...
如何设计这样的应用程序并牢记 RSpec、TDD、Cucumber、SOLID 和模式。 我直接问的是这里应该是什么对象来设计什么应该是一个模块(如果适用)等等。?以及如何判断这里需要测试什么,不需要测试什么?根据面向对象设计设计最合适的机制。
另外请参考一些书籍或博客来学习 ruby 的面向对象设计原则和实践。
Say an application in ruby when started has two modes : commandline mode and filemode
When given a parameterruby myprogram input.txt output.txt, it generates an output based on some commands in input file. also when not provided with any parameter it presents us with a command prompt. with following commands.
create_class_with_capacity 40
create_student_with_marks Alex 70
create_student_with_marks Mathew 30
create_student_with_marks John 55
..
create_student_with_marks Sylvia 70 etc...
fail_student_roll_no 12
=> Student with roll number 12 #{student} failed
give_marks_to_roll_no 70 1
=>Student with roll number 1 Alex got 70 marks
find_all_students_with_marks 70
=> Alex, Peter , Russell , Mark etc...
How to design such an application keeping RSpec,TDD , Cucumber , SOLID and Patterns in Mind.
What i am directly asking is What should be Objects here to design what should be a module if applicable etc.. ? and how to gauge what needs to be tested here and what not ? Designing the most Appropriate Mechanism in terms of Object Oriented design.
Also Please Refer To some books or blogs to learn these kind of object oriented design principles and practices for ruby.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你是说你必须写这个应用程序吗?这是作业吗?
在我看来,您似乎患有分析瘫痪。有太多的流行语在你的脑海中盘旋。
别再担心模式之类的事情了。将问题分解为多个部分并开始编写一些代码。
我提出的一个建议是将 I/O 排除在我们的课程之外。将所有与与用户交互无关的逻辑放在基类中。这样,如果您被要求创建一个基于 Web 的 UI 来替换您的文本版本,它们仍然可以工作。
必须测试什么?您认为可能会破坏的所有代码。
最合适的面向对象设计方法?做显而易见的事情:名词是问题陈述中的潜在对象,动词是潜在的方法。
以下是我看到的一些潜在对象:Student、Course、Roll。
以下是一些可能的方法: Student 和 Course 的 CRUD 操作;设定成绩并将学生标记为不及格。
事情没那么复杂。想“简单”,让一些东西发挥作用,然后完善它。
更新:
如果您能描述您所做的事情,会更容易回答您。
如果我认为是家庭作业问题,我怀疑这不会有太大影响。与其让我们猜测,您为什么不阅读 SOLID 并开始查看您创建的类,问问自己它是否符合这些原则。
图案?被高估了。别担心他们。
Are you saying that you have to write this application? Is this homework?
Sounds to me like you're suffering from analysis paralysis. There are far too many buzzwords zooming around in your head.
Stop worrying about patterns and what not. Decompose the problem into pieces and start writing some code.
The one recommendation I'd make is to keep I/O out of our classes. Put all the logic that has nothing to do with interacting with users in the base classes. That way they'll still work if you're asked to create a web-based UI to replace your text version.
What has to be tested? All the code that you think might break.
The most appropriate method of object-oriented design? Do the obvious thing: nouns are potential objects in your problem statement, verbs are potential methods.
Here are some potential objects that I see: Student, Course, Roll.
Here are some potential methods: CRUD operations for Student and Course; setting grades and marking a Student as failing.
It's not that complicated. Think "simple" and get something working, then refine it.
UPDATE:
It'd be easier to answer you if you could describe what you did.
If it's the homework problem that I think it is, I doubt that it would make much difference. Rather than make us guess, why don't you read SOLID and start looking at the classes you created ask yourself if it conforms to those principles.
Patterns? Overrated. Don't worry about them.