Башкирцев (Старовер) Станислав JavaTalks OOD Principles.

Slides:



Advertisements
Similar presentations
Object Oriented Design Principles Arnon Rotem-Gal-Oz Product Line Architect.
Advertisements

Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Design Principles & Patterns
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
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
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Design Patterns: someone has already.
Test Driven Development Derived from Dr. Fawcett’s notes Phil Pratt-Szeliga Fall 2009.
Specifications Liskov Chapter 9 SWE 619 Last Updated Fall 2008.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Intro to Generic Programming Templates and Vectors.
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
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”.
CIT 590 Intro to Programming Style Classes. Remember to finish up findAllCISCourses.py.
CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
Company Confidential – Do Not Duplicate 2 Copyright 2008 McLane Advanced Technologies, LLC S.O.L.I.D. Software Development Achieving Object Oriented Principles,
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
© 2004 Capgemini - All rights reserved SOLID - OO DESIGN PRINCIPLES Andreas Enbohm, Capgemini.
SWE 619 © Paul Ammann Procedural Abstraction and Design by Contract Paul Ammann Information & Software Engineering SWE 619 Software Construction cs.gmu.edu/~pammann/
Effective C#, Chapter 1: C# Language Elements Last Updated: Fall 2011.
CSE 301 Exam Revision Lecture
Introduction to SOLID Principles. Background Dependency Inversion Principle Single Responsibility Principle Open/Closed Principle Liskov Substitution.
Systems Analysis & Design Methods VII OOD-principles.
23-Oct-15 Abstract Data Types. 2 Data types A data type is characterized by: a set of values a data representation, which is common to all these values,
Dependency Injection Technion – Institute of Technology Author: Gal Lalouche - Technion 2015 ©
S.O.L.I.D. Software Development 12 January 2010 (Martin Verboon, Patrick Kalkman, Stan Verdiesen)
SOLID Principles in Software Design
SWE © Solomon Seifu ELABORATION. SWE © Solomon Seifu Lesson 12-5 Software Engineering Design Goals.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Refactoring for Testability (or how I learned to stop worrying and love failing tests) Presented by Aaron Evans.
240-Current Research Easily Extensible Systems, Octave, Input Formats, SOA.
Alternative Architectures: Inversion of Control Mike Hadlow mikehadlow.blogspot.com.
Refactoring & Testability. Testing in OOP programming No life in flexible methodologies and for refactoring- infected developers without SOME kind of.
Elements of OO Abstraction Encapsulation Modularity Hierarchy: Inheritance & Aggregation 4 major/essential elements3 minor/helpful elements Typing Concurrency.
Design for testability as a way to good coding Simone Chiaretta Architect, Council of the EU December 9 th,
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
High Cohesion Low Coupling Old Standards for Object Oriented Programming.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
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.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
The Last Lecture CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
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.
SOLID Principles in Software Design
Session 33 More on SOLID Steve Chenoweth Office: Moench Room F220 Phone: (812) Chandan Rupakheti Office: Moench.
F-1 © 2007 T. Horton CS 4240 Principles of SW Design More design principles LSP, OCP, DIP, … And another pattern Decorator.
Dependency Inversion By Steve Faurie. Dependency Inversion Described in Agile Principles, Patterns and Practices in C# by Robert C. Martin.
Duke CPS Programming Heuristics l Identify the aspects of your application that vary and separate them from what stays the same ä Take what varies.
SOLID PHP & Code Smell Wrap-Up
CSCE 240 – Intro to Software Engineering Lecture 3.
Beginning Software Craftsmanship Brendan Enrick Steve Smith
Mantas Radzevičius ifm-2/2
Software Architecture & Difference from Design
Copyright © by Curt Hill
Specifications Liskov Chapter 9
Magento Technical Guidelines Eugene Shakhsuvarov, Software Magento
Adaptive Code Umamaheswaran Senior Software Engineer
Object Oriented Practices
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
A (partial) blueprint for dealing with change
European conference.
Object Oriented Design & Analysis
Liskov Substitution Principle (LSP)
Presentation transcript:

Башкирцев (Старовер) Станислав JavaTalks OOD Principles

2 План презентации Intro Don’t Repeat Yourself Open Closed Single Responsibility Interface Segregation Inversion of Control Liskov’s Substitution Q/A

3 OOD Principles. What’s that? Recipes, best practices how to write Clean, easy to understand code Maintainable (flexible, extendable) code

DRY Don’t Repeat Yourself Don’t duplicate code

5 Without DRY

If something changes?

With DRY

If something changes?

DRY Don’t Repeat Yourself Don’t duplicate code Names should be clear

Not clear names 1. No one would guess to look sorting method in this class. 2. Newbie always would write his own implementation.

Correct DRY clear, well-defined class names

DRY Don’t Repeat Yourself Don’t duplicate code Names should be clear Location should be clear

Not clear location No one would look for array utilities in such package

DRY. Pros & Cons Pros: Changes impact local area Once written is not repeated No ”new” error prone solutions Cons: Amount of classes grows

OCP Open Closed Principle - code should be closed for modification, but open for extension.

OCP. Example I want my clients to be able to see results of past games. Let’s keep up with NHL & NBA games.. 1. American customer 2. His thoughts 3. His ill imagination

OCP. Example Base class Creates specific objects according to...

OCP. Example Great! A lot of new clients, a lot of money from ads. But.. Would be great if my cliends would be able to get info about MLB games too!

OCP. Example New league was added We are changing already working class!

OCP. Example Why my clients see errors on every page?!! I pay you for work, not for errors! I loose my clients, make the program work right!

OCP. Example Element of array is compared with league name – NPE is possible!

OCP. Moral This example shows that changing code that already works is always bad idea. Follow OCP to escape this!

OCP. Example MlbSportInfoBuilder NbaSportInfoBuilder NhlSportInfoBuilder

OCP. Example Great! You job satisfies me! But now I want WNBA to be added

OCP. Example MlbSportInfoBuilder NbaSportInfoBuilder NhlSportInfoBuilder WnbaSportInfoBuilder New league was added without changing single line of code!

OCP. How it works OCP uses: Delegation Inheritance

OCP. Pros & Cons Pros: Adding new functionality without legacy code being changed Cons: May complicate system because of amount of classes being grown

SRP Single Responsibility Principle: Single class should have a single responsibility Class should have only one reason to change

SRP. Random thoughts Hm.. Strange that inflation is counted in CurrencyConverter.. Hm.. What if format of currency service changes? What if the format of inflation service changes? We’ll have to change this class in both cases! It’s not intuitive! It’s overloaded! We have to do something!

SRP. Separate Responsibilities Hm.. What if format of currency service changes? We change CurrencyConverter! Hm.. What if format of inflation service changes? We change InflationIndexCounter!

SRP & DRY Again two responsibilities: Authentication & getting user from database DRY violation!

SRP. Delegating responsibilities Now we don’t work directly with database! If we would want to use ORM, UserAuthenticator won’t change!

SRP. Pros & Cons Pros: Helps to follow DRY Lowers chances to change single class Class names correspond to what they do Cons: May complecate system by adding too much new classes

ISP Interface Segregation Principle – client code shouldn’t be obligated to depend on interfaces it doesn’t use.

ISP. Example Base interface of the person. All these methods are useful for current implementation of person.

ISP. Extra methods But we’re writting new module that considers a person only as a human being. So we need only one method eat() So our new implementation has two extra methods.

ISP. What these methods do?!

ISP. Fat/Polluted Interfaces It’s fat It’s POLLUTED

ISP. Interface Separating We separated Person into Person & Worker. Two conceptually different interfaces.

ISP. No extra methods Now we have only needed methods.

ISP. Legacy code What if we have ready implementation, but we don’t want to use fat interface?

ISP. Use Adapters Thin interface Fat interface

ISP. Pros & Cons Pros: No need to implement unnecessary methods Client code sees only what it should see Cons: Adding additional interfaces

IoCP Invertion of Control Principle says: Code to abstraction, not to implementation Objects that use other objects, shouldn’t create latter ones

IoCP. Coding to implementation

IoCP. How to test Crawler? It’s impossible to write unit test for Crawler, because you cannot mock parser.

IoCP. Let’s inject Now you can specify parser through constructor. You can inject dummy object while testing.

IoCP. Again doesn’t work Your parser doesn’t work with HTML that isn’t a valid XML!

IoCP. New Implementation HtmlParser DomBasedHtmlParser EnhancedHtmlParser

IoCP. But how do we replace? We cannot specify another implementaion!

IoCP. Let’s code to interface Now we use interface, so we can specify enhanced implementation of parser.

IoCP. Another look How do we inject objects if we work on framework/library? We cannot use IoC Containers, our clients don’t allow us extra dependencies, they don’t want to depend on Spring or Guice. We need leightweight decision.

IoCP. Let’s use Factories Let’s use Factories! Hm.. But we cannot write unit tests again!

IoCP. Let’s mix We have both: setter and factory in the same class. Now we have default implementation and possibility to change default behavior.

IoCP. Pros & Cons Pros: Classes don’t depend on concrete implementation Allows easily change implementation Allows write good unit tests Cons: Creating additional interfaces Creating Factories/depending on IoC containers

LSP Liskov’s Substitution Principle – derived types must be completely substitutable for their base types. LSP declares how to use inheritance correctly.

LSP. java.util.List Would be strange if these implementation would do different things here

LSP. Emu doesn’t fly! But emu doesn’t fly!

LSP. Emu indeed doesn’t fly Simply birds. Flying birds. Emu is simply a bird. It doesn’t have extra methods.

LSP. Array example Default implementation. It’s a temporal class that uses unefficient approach. But it does it’s work correctly.

LSP. Array example At last we wrote enhanced implementation that’s really efficient.

LSP. Again bugs! Your system always throws errors! It’s a chaos!!

LSP. Array example It sorts the original array! We have problems with synchronization. Implementation does its work, but it has side effects. It doesn’t satisfy interface contract! We cannot simply replace one implementation with another one, because they differ! We should correct its behaviour to copy original array and work with its copy.

LSP & equals() Colored point extends simple point and adds new field – color. But it works only with ColoredPoint!

LSP & equals This will print: true false This violates equals() contract! There is no correct dicision in this situation!

LSP & equals() The only correct way here is to use delegation instead of inheritance.

LSP & exceptions What if this method in our implementation threw IOException? How would we know about that? We work with interface List, not with implementation! That’s why Java doesn’t allow us to throw any checked exception that are not declared in base class.

LSP. Pros & Cons Pros: Allows not to think about concrete implementation, but code to abstraction Unambiguously defines contract all implementers should follow Allows to interchange implementation correctly, without side effects

The End Q/A