Slide 1

Slide 1 text

Uncle Bob Special Robert C. Martin

Slide 2

Slide 2 text

Robert C. Martin a.k.a Uncle Bob

Slide 3

Slide 3 text

SOLID Principles Principles of OOP

Slide 4

Slide 4 text

SOLID • Single responsibility principle • Open / closed principle • Liskov substitution principle • Inteface segregation principle • Dependency inversion principle

Slide 5

Slide 5 text

Single responsibility principle ױੌ ଼੐ ਗ஗ A class should have only a single responsibility (i.e. changes to only one part of the software's specification should be able to affect the specification of the class). A class should have only one reason to change. E.g A module complies and prints a report. It can be changed for two reasons: a) the content of the report could change or b) the format of the report could change. Pros It makes the class more robust.

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

Open / closed principle ѐߑ / ತࣧ ਗ஗ Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification. A module will be said to be open if it is still available for extension. For example, it should be possible to add fields to the data structures it contains, or new elements to the set of functions it performs. A module will be said to be closed if [it] is available for use by other modules. This assumes that the module has been given a well-defined, stable description (the interface in the sense of information hiding).

Slide 8

Slide 8 text

Open / closed principle package com.topjavatutorial; public class CreditCard { private int cardType; public int getCardType() { return cardType; } public void setCardType(int cardType) { this.cardType = cardType; } public double getDiscount(double monthlyCost){ if (cardType == 1) { return monthlyCost * 0.02; } else { return monthlyCost * 0.01; } } } Violation

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Liskov substitution principle ѐߑ / ತࣧ ਗ஗ Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program. • Contravariance of method arguments in the subtype. • Covariance of return types in the subtype. • No new exceptions should be thrown by methods of the subtype, except where those exceptions are themselves subtypes of exceptions thrown by the methods of the supertype. A great example illustrating LSP (given by Uncle Bob in a podcast I heard recently) was how sometimes something that sounds right in natural language doesn't quite work in code.

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Interface segregation principle ੋఠಕ੉झ ܻ࠙ ਗ஗ Many client-specific interfaces are better than one general-purpose interface. Pros It keeps a system decoupled and thus easier to refactor, change, and redeploy. E.g Xerox printer system

Slide 13

Slide 13 text

Interface segregation principle public interface Order { void purchase(); void processPaypalPayment(); } public class OnlineOrder implements Order { … } public class InPersonOrder implements Order { …? } Violation

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Dependency inversion principle ੄ઓҙ҅ ৉੹ ਗ஗ One should depend upon abstractions, [not] concretions. A. High-level modules should not depend on low-level modules. Both should depend on abstractions. B. Abstractions should not depend on details. Details should depend on abstractions. Pros Simpler to see a good thinking principle as a coding pattern Easy to mocking for testing

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Package Principles

Slide 19

Slide 19 text

Package Principles Principles of package cohesion • Reuse-release Equivalence Principle (REP) • Common-Reuse Principle (CRP) • Common-Closure Principle (CCP) Principles of package coupling • Acyclic Dependencies Principle (ADP) • Stable-Dependencies Principle (SDP) • Stable-Abstractions Principle (SAP)

Slide 20

Slide 20 text

The package must be created with reusable classes.
 “Either all of the classes inside the package are reusable, or none of them are.” The classes must also be of the same family. Classes that are unrelated to the purpose of the package should not be included. Pros
 A package constructed as a family of reusable classes tends to be most useful and reusable. Reuse-release Equivalence Principle

Slide 21

Slide 21 text

CCP states that the package should not have more than one reason to change. If changes were to happen in an application dependent on a number of packages, ideally one only want changes to occur in one package, rather than in a number of them. This helps to identify classes that are likely to change, and package them together for the same reasons. If the classes are tightly coupled, they belong to the same package. Common-Closure Principle

Slide 22

Slide 22 text

The CRP states that classes that tend to be reused together belong in the same package together. It is a way of helping to decide which classes belong in which package. When depending on a package, one wants to make sure that the classes are inseparable and interdependent, which is also handy when culling classes that do not belong in the package. Common-Reuse Principle

Slide 23

Slide 23 text

In a development cycle with multiple developers, cooperation and integration needs to happen in small incremental releases. The ADP states that there can be no cycles in the dependency structure, and that when an incremental release is made, the other developers can adopt and build upon it. Acyclic Dependencies Principle

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

Breaking the Cycle. 1. Apply the Dependency Inversion Principle. We could create an abstract base class that has the interface that MyDialogs needs. We could then put that abstract base into MyDialogs and inherit it into MyApplication. This inverts the dependency between MyDialogs and MyApplication thus breaking the cycle. 2. Create a new package that both MyDialogs and MyApplication depend upon. Move the class(es) that they both depend upon into that new package. Acyclic Dependencies Principle

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Designs, by nature of the environments they are used in or by, are changing. Thus, package design needs to support change as well. The SDP states that any packages one wants to be volatile should not be depended upon by a package that is difficult to change. Stable-Dependencies Principle

Slide 28

Slide 28 text

The SAP says that a stable package should also be abstract, so that its stability does not prevent it from being extended. It also states that an unstable package should be concrete, since its instability allows the concrete code within it to be easily changed. Stable-Abstractions Principle

Slide 29

Slide 29 text

Clean Architecture

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

Advantages Independent of Frameworks
 The architecture does not depend on the existence of some library of feature laden software. This allows you to use such frameworks as tools, rather than having to cram your system into their limited constraints. Testable
 The business rules can be tested without the UI, Database, Web Server, or any other external element. Independent of UI
 The UI can change easily, without changing the rest of the system. A Web UI could be replaced with a console UI, for example, without changing the business rules. Independent of Database
 You can swap out Oracle or SQL Server, for Mongo, BigTable, CouchDB, or something else. Your business rules are not bound to the database. Independent of any external agency
 In fact your business rules simply don’t know anything at all about the outside world.

Slide 32

Slide 32 text

Entities Entities encapsulate Enterprise wide business rules. An entity can be an object with methods, or it can be a set of data structures and functions. It doesn’t matter so long as the entities could be used by many different applications in the enterprise.

Slide 33

Slide 33 text

Use Cases The software in this layer contains application specific business rules. It encapsulates and implements all of the use cases of the system. These use cases orchestrate the flow of data to and from the entities, and direct those entities to use their enterprise wide business rules to achieve the goals of the use case.

Slide 34

Slide 34 text

Interface Adapters The software in this layer is a set of adapters that convert data from the format most convenient for the use cases and entities, to the format most convenient for some external agency such as the Database or the Web. It is this layer, for example, that will wholly contain the MVC architecture of a GUI. The Presenters, Views, and Controllers all belong in here. The models are likely just data structures that are passed from the controllers to the use cases, and then back from the use cases to the presenters and views.

Slide 35

Slide 35 text

Frameworks and Drivers The outermost layer is generally composed of frameworks and tools such as the Database, the Web Framework, etc. Generally you don’t write much code in this layer other than glue code that communicates to the next circle inwards.

Slide 36

Slide 36 text

Crossing boundaries Point inwards Dependency Inversion Principle Don’t pass Entities or DB rows Don’t violate Dependency Rule

Slide 37

Slide 37 text

Is it Good for Us ? Let’s think about it.

Slide 38

Slide 38 text

Thank you.