SOLID PHP & Code Smell Wrap-Up

Slides:



Advertisements
Similar presentations
Design Principles & Patterns
Advertisements

General OO Concepts and Principles CSE301 University of Sunderland Harry R. Erwin, PhD.
Lecture 9 Improving Software Design CSC301-Winter 2011 – University of Toronto – Department of Computer Science Hesam C. Esfahani
SOLID Object Oriented Design Craig Berntson
5/17/2015 OO Design: Liskov Substitution Principle 1.
Inheritance and object compatibility Object type compatibility An instance of a subclass can be used instead of an instance of the superclass, but not.
17.10 Java Beans Java beans are a framework for creating components in Java. AWT and Swing packages are built within this framework Made to fit in with.
1 Software Maintenance and Evolution CSSE 575: Session 6, Part 1 The “SEAM” Model Steve Chenoweth Office Phone: (812) Cell: (937)
Developed by Reneta Barneva, SUNY Fredonia Component Level Design.
TEST-1 7. Object-Oriented Design Principles. TEST-2 The Pillars of the Paradigm Abstraction Encapsulation Hierarchy –Association, Aggregation –Inheritance.
1 OO Design Principles Project Group eXtreme Programming Md. Abul Bashar 07/09/2004.
Object-oriented metrics Design decisions: Class Cohesion Open-Closed Single Responsibility Interface Segregation Dependency Inversion Liskov Substitution.
Building SOLID Software with Dependency Injection Jeremy Rosenberg.
CLASS DESIGN PRINCIPLES Lecture 2. The quality of the architecture What is a good design? It is the design that at least does not have signs of “bad”.
1 OO Design Novosoft, 2001 by V. Mukhortov. 2 OO Design Goals  Flexibility Changes must be localized  Maintainability Modules requiring changes can.
Ch:10 Component Level Design Unit 4. What is Component? A component is a modular building block for computer software Because components reside within.
Company Confidential – Do Not Duplicate 2 Copyright 2008 McLane Advanced Technologies, LLC S.O.L.I.D. Software Development Achieving Object Oriented Principles,
OODP Prudent OO Design. OODP-2 The Pillars of the Paradigm Abstraction Encapsulation Hierarchy –Association, Aggregation –Inheritance Polymorphism.
© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini.
CSE 301 Exam Revision Lecture
Introduction to SOLID Principles. Background Dependency Inversion Principle Single Responsibility Principle Open/Closed Principle Liskov Substitution.
Class Design III: Advanced Inheritance Additional References “Object-Oriented Software Development Using Java”, Xiaoping Jia, Addison Wesley, 2002 “Core.
S.O.L.I.D. Software Development 12 January 2010 (Martin Verboon, Patrick Kalkman, Stan Verdiesen)
Башкирцев (Старовер) Станислав JavaTalks OOD Principles.
SOLID Principles in Software Design
Design Principles iwongu at gmail dot com.
 What is SOLID  The S in SOLID  The O in SOLID  The L in SOLID  The I in SOLID  The D in SOLID  Questions.
Software Design Principles
Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency.
OO Design Principles Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
1 Software Engineering: A Practitioner’s Approach, 6/e Chapter 11a: Component-Level Design Software Engineering: A Practitioner’s Approach, 6/e Chapter.
Design for testability as a way to good coding Simone Chiaretta Architect, Council of the EU December 9 th,
High Cohesion Low Coupling Old Standards for Object Oriented Programming.
Five design principles
Evaluating an Object-Oriented Design ©SoftMoore ConsultingSlide 1.
1. Perspectives on Design Principles – Semantic Invariants and Design Entropy Catalin Tudor 2.
CHAPTER 3 MODELING COMPONENT-LEVEL DESIGN.
Principles of Object Oriented Design
PRINCIPLES OF OBJECT ORIENTED DESIGN S.O.L.I.D. S.O.L.I.D Principles What is SOLID?  Acrostic of 5 Principles:  The Single Responsibility Principle.
SOLID Design Principles
These courseware materials are to be used in conjunction with Software Engineering: A Practitioner’s Approach, 6/e and are provided with permission by.
Session 33 More on SOLID Steve Chenoweth Office: Moench Room F220 Phone: (812) Chandan Rupakheti Office: Moench.
BASICS OF CODE DESIGN.  Modular  Reusable  Easy to Read  Maintainable  Testable  Easy to Change  Easy to Understand THE GOAL.
F-1 © 2007 T. Horton CS 4240 Principles of SW Design More design principles LSP, OCP, DIP, … And another pattern Decorator.
Microsoft Advertising 16:9 Template Light Use the slides below to start the design of your presentation. Additional slides layouts (title slides, tile.
14 Jul 2005CSE403, Summer'05, Lecture 09 Lecture 09: Fundamental Principles and Best Practices for Software Design Valentin Razmov.
Mantas Radzevičius ifm-2/2
What is Agile Design? SRP OCP LSP DIP ISP
Course information Old exam Resit Report Result and walkthrough
Software Design Principles
Software Architecture & Difference from Design
Copyright © by Curt Hill
Adaptive Code Via C#
Agile Software Development
Adaptive Code Umamaheswaran Senior Software Engineer
TechEd /17/2018 6:14 PM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered.
Software Design Principles
11/29/2018 © 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks and/or trademarks.
15 letters that will change your code
COMPUTER 2430 Object Oriented Programming and Data Structures I
The SOLID Principles.
A (partial) blueprint for dealing with change
European conference.
Software Development An overview of careers in Business Application Development Sources: Dice.com, money.CNN.com, InfoWorld.com, money.USNews.com,
1.
Dependency Inversion principle
Some principles for object oriented design
Interface Segregation Principle
Liskov Substitution Principle (LSP)
Chapter 10 – Component-Level Design
Presentation transcript:

SOLID PHP & Code Smell Wrap-Up LSP: Liskov substitution principle ISP: Interface segregation principle DIP: Dependency inversion principle

SOLID PHP & Code Smell Wrap-Up LSP: Liskov substitution principle DIP: Dependency inversion principle ISP: Interface segregation principle

Review SOLID is an acronym for a set of design principles by Robert “Uncle Bob” Martin SRP: Single Responsibility principle – Objects should only have one responsibility OCP: Open/closed principle – Objects open for extension but closed for modification

LSP: Liskov substitution principle Objects should always be able to stand-in for their ancestors Named after Barbara Liskov who first introduced the principal

DIP: Dependency inversion principle Define interfaces from the bottom-up, not top-down Depend on interfaces, not on concrete classes

ISP: Interface segregation principle “many client specific interfaces are better than one general purpose interface.”

LSP Example: Rectangle & Square The Rectangle and Square are the “standard” example of an LSP problem. A Square IS-A Rectangle which happens to have the same width and height: Square IS-A Rectangle

LSP Example: Rectangle & Square Lets look at an implementation and unit test Squares don’t BEHAVE like rectangles, so they violate LSP BEHAVIOUR Square Rectangle

More Obvious LSP Violations class FooMapper { function fetchAll() { return array( 'a588ea995c5c74827a24d466d14a72101'=>'Alpha', 'z4c853bae4a5e427a8d6b9bf33140bb2e'=>'Omega'); } class BarMapper extends FooMapper { $result = new stdClass(); $result->a588ea995c5c74827a24d466d14a72101 = 'Alpha'; $result->z4c853bae4a5e427a8d6b9bf33140bb2e = 'Omega'; return $result;

More Obvious LSP Violations class FooView { function display(FooModel $foo) { /* ... */ } } class BarView extends FooView { function display(BarModel $bar) { /* ... */ }

More Obvious LSP Violations Removing functionality from an ancestor: class DecoyDuck extends Duck { function fly() throw new Exception( "Decoy ducks can't fly!"); }

Is this an LSP Violation? class Foo { function getMapper() { return new FooMapper(); } class Bar extends Foo { return new BarMapper(); No. Return values should be covariant to overridden method return types.

Preventing LSP Violations Design by Contract with Unit Tests Think in terms of BEHAVES-LIKE instead of IS-A when creating subclasses Don’t remove functionality from ancestors

Preventing LSP Violations Method parameters should be the same or less restrictive in what they will accept (invariant or contravariant) Method return values should be the same or more restrictive in what is returned (invariant or covariant)

Smells like an LSP Violation Any time you need to know the type of an object (instanceof, is_a, etc) When the parameters or return type of an overridden method are different from its ancestor Throwing new exceptions

DIP: What is Inversion? Conventional MVC Dependencies: Controller Model View Storage

DIP: What is Inversion? Inverted MVC Dependencies: Model Service Interface Controller View Service Interface Model Storage Service Interface View Storage

DIP Concepts Concrete classes only have dependencies to interfaces High level components build interfaces for the services they need Low level components depend on those high level service interfaces

Evolving towards the DIP class FooControllerConventional { function indexView() { $model = new FooModel(); $viewData = $model->fetchAll(); $view = new FooView(); $view->render($viewData); }

Evolving towards the DIP class FooControllerDI { private $_model; private $_view; function __construct(FooModel $model, FooView $view) { $this->_model = $model; $this->_view = $view; } function indexView() { $viewData = $this->_model->fetchAll(); $this->_view->render($viewData);

Evolving towards the DIP interface ModelServiceInterface { function fetchAll(); //… Other required methods } interface ViewServiceInterface { function render($viewData);

Evolving towards the DIP class FooControllerDIP { private $_model; private $_view; function __construct(ModelServiceInterface $model, ViewServiceInterface $view) { $this->_model = $model; $this->_view = $view; } function indexView() { $viewData = $this->_model->fetchAll(); $this->_view->render($viewData);

Smells like a DIP Violation Any dependency by one class on another concrete class: “Hard Dependency Smell”

Interface Segregation Principle Useful for “fat” classes. These classes: May violate SRP May have an “extra” method for a specific client May have one responsibility which can be further segregated

ISP Example: User Model class UserModel { /** * @param string $userId * @param string $password * @return string Authentication Token; empty string on failure */ function doLogin($userId, $password) {} * @param string $token * @return bool function authenticate($token) {} } Most clients just want to authenticate. Only the login controller uses doLogin().

ISP Example: User Model interface UserLoginClient { function doLogin($userId, $password); } interface UserAuthenticationClient { function authenticate($token);

Smells Like an ISP Violation Fat Classes, “God Objects” Methods only used by a few clients but pollute the interface used by all clients

Thanks Folks! Thanks for joining me for the SOLID wrap up! If we have time, I have some code we can refactor to play with LSP, DIP and ISP Slides and code will be posted shortly Next week: CakePHP and Facebook Apps!