1. mini seminar intro

25
Mini Seminars/Design patterns... Leonid M.

Upload: leonid-maslov

Post on 12-Dec-2014

1.322 views

Category:

Documents


1 download

DESCRIPTION

The first seminar in the mini-seminars periodical sessions I've prepared and lead in my spare time while being employed at Exigen Services. Kudos, guys! Since these presentations were spare time hobby - I've decided to share them :) Hopefully someone will find them useful. The intro is - what designs patters are about, some simple examples and a lots of colorful images.

TRANSCRIPT

Page 1: 1. Mini seminar intro

Mini Seminars/Design patterns...

Leonid M.

Page 2: 1. Mini seminar intro

Veicināt atvērtu komunikācijas vidi

Identificēt stipras / vājas eksistējošas ekspertīzes puses

Efektīva dalīšanas ar interesantu / noderīgu / jaunu informāciju

Mini semināru mērķi

Page 3: 1. Mini seminar intro

Design Patterns- Theory- Name a few: Strategy, Bridge ...- Pattern vocabularies:

Patterns of Enterprise Application Architecture …- “Not a pattern” pattern- Anti-patterns- Design Puzzles- Testability & Patterns: Google Testing Blog

Mini seminar #2: Best Practices- Mylyn & Attlassian connectors- … ?

Mini seminar #1: Content

Page 4: 1. Mini seminar intro

Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice

Christopher Alexander, Sara Ishikawa, MurraySilverstein, Max Jacobson, Ingrid Fiksdahl-King, and Shlomo Angel.A Pattern Language. Oxford University Press, NewYork, 1977.

Design Pattern

Page 5: 1. Mini seminar intro

Design Pattern

Problem

Cons.Solution

Name

Page 6: 1. Mini seminar intro

Encapsulate what varies

Favor compositon over inheritance

Program to interfaces, not implementations

Strive for loosely coupled designs between objects what interact

Turtles all the way down

Page 7: 1. Mini seminar intro

Shared vocabularies: benefits

Recognition Communicatio

n compactness Code

readability Identified

consequences and impacts

Best practices reused

Page 8: 1. Mini seminar intro

GOF Name Intent Also Known As Applicability Structure Participants Collaborations Consequences Implementation Sample Code Known Uses Related Patterns

Definition / Vocabularies

Page 9: 1. Mini seminar intro

Strategy pattern

or

Page 10: 1. Mini seminar intro

Synonyms: Policy

Intent: Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Strategy

Page 11: 1. Mini seminar intro

Sample code

class StrategyExample {

public static void main(String[] args) {

Context context;

// Three contexts following different strategies context = new Context(new ConcreteStrategyMultiply()); int resultA = context.executeStrategy(2, 3);

context = new Context(new ConcreteStrategyAdd()); int resultB = context.executeStrategy(resultA, 1); }

}

public String getAbsolutePath() { return fs.resolve(this); // NT file system, Unix file system, etc… }

Example #1:

Example #2: Code snippet from java.io.File

Page 12: 1. Mini seminar intro

JDK java.net.URL – use protocol handlers as

strategy to open connection java.io.File – use underlying file system as

strategy to manage filesEclipse Almost everything is strategy. Why so?

Code next door

Page 13: 1. Mini seminar intro

Adapter vs Decorator

Page 14: 1. Mini seminar intro

Adapter Synonyms: Wrapper

Intent: Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

Page 15: 1. Mini seminar intro

public class ApproverJiraUserAdapter implements ApproverResolveStrategy<User> { private final ApproverResolveStrategy<String> delegator; public ApproverJiraUserAdapter(ApproverResolveStrategy<String> delegator,

UserManager userManager) { this.delegator = delegator; this.userManager = userManager; }…. /* * (non-Javadoc) * * @see *

com.exigen.jira.plugins.sr.approval.strategies.ApproverResolveStrategy#resolve(com.exigen.jira.plugins.sr.approval

* .strategies.StrategyContext, java.util.Collection) */ public List<User> resolve(StrategyContext context, List<User> resolventChain)

throws ApproversResolveException { try { log.debug("Converting to user objects"); return toUsers(getDelegator().resolve(context, toStrings(resolventChain))); } catch (EntityNotFoundException e) { throw new ApproversResolveException(e); } }

….

ESWIJI, live code sample

ApproveResolveStrategy<String>

ApproveResolveStrategy<User>

resolve(..String ..)

List<String>

Client

resolve(..User ..) List<User>

Page 16: 1. Mini seminar intro

JDK java.io.OutputStreamWriter java.io.StringReader…

java.awt.event.MouseAdapter…

Code next door

Page 17: 1. Mini seminar intro

Synonyms: Wrapper

Intent: Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

Decorator

Page 18: 1. Mini seminar intro

Decorator, GOF sample(Best illustration ever)

leonardinius
HTML sample (web sample) would be appreciated
Page 19: 1. Mini seminar intro

JDK java.io.BufferedInputStream

◦ java.io.* javax.swing. JScrollPane

Code next door

Page 20: 1. Mini seminar intro

What’s the difference?

Adapter vs Decorator

Page 21: 1. Mini seminar intro

Chain of Responsibility

Intent: Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles.

Page 22: 1. Mini seminar intro

public class WelcomeFilter implements Filter{

public void init(FilterConfig filterConfig) { welcome = filterConfig.getInitParameter("welcome"); if(welcome == null) welcome = "index.html"; }

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

throws IOException, ServletException { String path = ((HttpServletRequest)request).getServletPath(); if(welcome != null && path.endsWith("/")) request.getRequestDispatcher(path + welcome).forward(request, response); else chain.doFilter(request, response); }…}

Code sample

Page 23: 1. Mini seminar intro

JDK java.util.logging.Logger

javax.servlet.Filter

Code next door

Page 24: 1. Mini seminar intro

Application: Email spam filtering system. Both HTML and Text formats should be supported.

Multiple spam recognition algorithms are present and several could be used simultaneously.

Multiple weekly report approaches should be supported: XML, HTML files on filesystem;+ email with report attachment.

Pattern puzzle

Page 25: 1. Mini seminar intro

Questions