1. mini seminar intro
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
Mini Seminars/Design patterns...
Leonid M.
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
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
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
Design Pattern
Problem
Cons.Solution
Name
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
Shared vocabularies: benefits
Recognition Communicatio
n compactness Code
readability Identified
consequences and impacts
Best practices reused
GOF Name Intent Also Known As Applicability Structure Participants Collaborations Consequences Implementation Sample Code Known Uses Related Patterns
Definition / Vocabularies
Strategy pattern
or
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
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
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
Adapter vs Decorator
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.
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>
JDK java.io.OutputStreamWriter java.io.StringReader…
java.awt.event.MouseAdapter…
Code next door
Synonyms: Wrapper
Intent: Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
Decorator
Decorator, GOF sample(Best illustration ever)
JDK java.io.BufferedInputStream
◦ java.io.* javax.swing. JScrollPane
Code next door
What’s the difference?
Adapter vs Decorator
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.
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
JDK java.util.logging.Logger
javax.servlet.Filter
Code next door
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
Questions