On Github simplonve / exo3_poo
Avril-Octobre 2015
Sources :
Practical Object-Oriented Design in Ruby - by Sandi Metz
http://rubylearning.com/satishtalim/tutorial.html https://en.wikibooks.org/wiki/A-level_Computing/AQA/Problem_Solving,_Programming,_Operating_Systems,_Databases_and_Networking/Programming_Concepts/Object-oriented_programming_(OOP) https://robots.thoughtbot.com/back-to-basics-polymorphism-and-ruby
=> to gain reusability
A “hand”
The “hand” is a class. <= Abstraction here !
Your body has two objects of the type "hand" :
Functions are controlled by a set of electrical signals sent through your shoulders
Shoulder = interface
The hand class is being reused to create the left hand and the right hand by slightly changing the properties of it.
A "thing" that can perform a set of related activities.
The Hand (object) can grip something,
A Student (object) can give their name or address.
An object is always an instance of a class.
Class is composed of three things:
This is an art
But you have SOLID
SOLID are 5 principles that you must follow when design a class
1 SRP - The Single Responsibility Principle
A class should have one, and only one, reason to change
2 OCP - The Open Closed Principle
Should be able to extend any classes' behaviors, without modifying the classes
3 LSP - The Liskov Substitution Principle
A subclass can be used anywhere its superclass would do
4 ISP - The Interface Segregation Principle
Make fine grained interfaces that are client specific
5 DIP - The Dependency Inversion Principle
Depend on abstractions, not on concretions.
Stop talking !
Refactors are needed, not because the design is clear, but because it isn’t.
À partir des commandes et résultats suivant, retrouver les classes à écrire.
player_1 = Player.new "Ryu" player_2 = Player.new "Ken" player_1.hp = 100 player_1.force = 10 player_2.hp = 200 player_2.force = 5 player_1.hit player_2 # => Ken perd 10 hp ! puts player_2.hp # => 190 20.times do player_2.hit player_1 # => Ryu perd 5 hp ! end # => Ryu a perdu !