17
Java - Better with Design Narayann Swaami Agile Technical Coach Societe Generale

Better java with design

Embed Size (px)

Citation preview

Page 1: Better java with design

Java - Better with Design

Narayann SwaamiAgile Technical Coach

Societe Generale

Page 2: Better java with design

Currently work as :

Agile Technical Coach @http://www.socgensolutions.com/

16 yrs poking around, 10+ years on Java

Interests:

Clean code, TDD, Parallel/Concurrent programming

About me

Page 3: Better java with design

Summary

There are many ways of approaching best practices

Learn on your own

Learn from Industry standard books

Learn from conferences/meetups

Page 4: Better java with design

How this came about

Coaching teams - working with them

Analyze what worked AND what did not work

Inspect and Adapt!

Page 5: Better java with design

What we will learn today

Interesting best practices - From “Refactoring To Patterns”

Page 6: Better java with design

How we do this

→ μ- Workshop → Two - three examples [ Time-Dependent ] → Refactoring workflow → Interactivity

Page 7: Better java with design

Better OOP is better Java

Why?

Page 8: Better java with design

Better Design is Better Java

Why?

Page 9: Better java with design

What comes to mind?

S.O.L.I.D

Page 10: Better java with design

S.O.L.I.D in 120 seconds

SRP - Responsibility/Ease of change of a class

OCP - Strategy/Template Method + Interface

LSP - Car Is-a Vehicle [ Square Is-a Rectangle? ]

ISP - Many specific Interfaces

DIP - High Level / Low level modules ⇒ abstractions ⇐ details

Page 11: Better java with design

Creating classes - move away from Constructor?Creating classes

● Use creation methods [not necessarily factory method - overkill]

● Chain Constructors● Collecting Parameter

Consider ⇒ 3D Printed Building What comes to mind?

● 3D Printed Building● Area● Dimensions● Floors

Highlights ⇒ Intention-revealing names for

creation Methods ⇒ One constructor calling others in a chain that follows a logical Sequence (move to creation Method)

Page 12: Better java with design

Replace Conditional logic with Strategy

● For one of several variants of an algorithm● Use a separate strategy for each variant and call accordingly

● 3DPrinter - how to print a building?

Page 13: Better java with design

Code Study: Encapsulate classes with Factory

● “Program to interface, not to implementation”

● “Client does not need to know where the class resides”

● “Access through a common interface”

● Use intention-revealing creation Methods

Why?

See http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/Collections.java

Collections.synchronizedXYZ()

How?

Page 14: Better java with design

Encapsulate Composite with Builder

● Let’s evaluate a Spec for the 3D Printed Building

● Like configuration files

● Creation of composites can be error-prone

● The XML Tag is joined by a composite● Loose Coupling between client and

composite

Why How

● Create a builder● Use builder to produce a one-node

Composite . Add a method to the builder to obtain the result of its build

● Make the builder capable of building children.

● Make the builder capable of setting those attributes and values.

● Refactor to make it simpler

Page 15: Better java with design
Page 16: Better java with design

Reference

Page 17: Better java with design

धन्यवादः।