Programming SOLID

865 views
704 views

Published on

My

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
865
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Programming SOLID

    1. 1. S.O.L.I.D.applications Ladislav Martincik
    2. 2. Contents - Class, instance, object - DRY - S.O.L.I.D.ALL the time discussion please!
    3. 3. WHat is what? class Klass; end instance = Klass.new Object.new
    4. 4. SOLID- Single responsibility principle- Open/closed principle- Liskov substitution principle- Interface segregation principle- Dependency inversion principle
    5. 5. SINGLE RESP. P. object should have only a single responsibility - AR::Base - Presistency only - AR::Controller - REquest care only
    6. 6. SRP - Basicclass User < AR::Base def can_modify_article? article article.user == self end before_save :create_activity def create_activity Activity.new :message => New user #{name} endend
    7. 7. SRP - Basicclass User < AR::Base; endclass ArticlePolicy def can_user_modify_article? user, article article.user == user endendclass UserObserver def after_save record Activity.new :message => New user #{record.name} endend
    8. 8. SRP - Advancedclass User < AR::Baseendclass ArticleForSignup include ActiveModel::Validations validate :name, :presence => true attr_accessor :nameend
    9. 9. OPEn/close p.an object should be open forextensions but close for modifications - ruby is dynamic lang. so modifying object is its nature - meta-programming (use with care)
    10. 10. Ocp - basicclass Array def to_s self.map { |x| x.to_s }.join(, ) endend
    11. 11. Ocp - intermediateclass Invoice def to_s formater = HTMLFormater.new formater.format self endendclass Invoice def to_s formater = HTMLFormater.new formater.format self endend
    12. 12. Dependency Inj.depend upon abstractions, do not uponconcretions - link from outside in, not inside out
    13. 13. DI - BASICclass Error def initialize @reporter = Reporter.new end def report @reporter.report self endendclass Error def initialize reporter = Reporter.new @reporter = reporter endend
    14. 14. DI - More abstractclass Error def initialize(options = {}) @reporter = options[:reporter] || Reporter.new endendclass Error def initialize(options = {}) @reporter = (options[:reporter_name] || reporter).classify.new endend
    15. 15. Liskov D. P. objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program - method missing - super-class is smarter than its children
    16. 16. Liskov - AR::Baseclass User < AR::Base # create_table :users { |t| t.text :logger }end# We expect ...user = User.newuser.logger = "syslog-ng"# => "syslog-ng"# but we getuser = User.newuser.logger = "syslog-ng"# => #<ActiveSupport::BufferedLogger:0x00000000b6de38 ..>
    17. 17. Interface seg. many client specific interfaces are better than one general purpose interface - one smart controller
    18. 18. ISPclass Signup < ApplicationController def create if params[:type] == founder item = Founder.new(params) end ... endendclass FounderSignup < ApplicationControllerendclass AdvisorSignup < ApplicationControllerend
    19. 19. DRYDO not repeat the shit
    20. 20. Why?If your application is successful youwill have to change it as newrequirements will come or newimprovements will be introduced.Refactor not because you know theabstraction but because you wantto find it.
    21. 21. REality 2 imp. Customers should come to you with specific, tangible, measurable, external objectives that you can than implement and come up with abstractions. The reason is because people relate very well to concrete stuff in our world then to abstract, emotional descriptions.
    22. 22. examples - Make my customers feel more happy, What means more happy? - The signup has to be simple. What it means simple? - I need more leads/customers to come to my shop.. How do you measure more?
    23. 23. Best practice The path from abstract descriptions of our customers to concrete examples and back to abstract concepts so we can change stuff easily in future.

    ×