DOC PREVIEW
CU-Boulder CSCI 5448 - Additional Design Patterns

This preview shows page 1-2-15-16-31-32 out of 32 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Additional Design PatternsChain of ResponsibilityandFlyweightJason RobisonCSCI 5448The Chain of Responsibility PatternChain of Responsibility●Ted has a job handling customer complaints for the most popular breakfast food in the country, Fruity Freakies cereal.TedChain of Responsibility●Some complaints aren't as important. Ted just handles these complaints by himself.The color of my cereal box waspea green instead of lime green.Aaargh.Chain of Responsibility●But other complaints are more important. These types of complaints Ted needs to send to one of his bosses. Ted tries his best to forward the requests to the right person. There were animal parts in my cereal. It's not really that big a deal though,sorry to bother you.Someone else is going toneed to handle this one.Chain of Responsibility●Some problems need to go to Ted's immediate supervisor. Really important problems need to go all the way to the CEO.●Ted has been working at Fruity Freakies for a while and has a decent idea of where to forward certain requests, for the most part.Chain of Responsibility●But Ted has gotten a little sick of dealing with things by hand, and decides to write a computer program. Ted took many software classes in college, and indeed later switched from computer science to customer service (he just didn't get to interact with enough angry people in CS for his tastes). But Ted remembers the lessons learned from his study of software design and thinks he can design a software system he can use to deal with all the beaurocratic decisions for him.Chain of Responsibility●Ted writes the following code:if ( problemType == 1 ) { Ted ted = new Ted(); ted.handleProblem(problem);}else if ( problemType == 2 ) { Supervisor supervisor = new Supervisor(); supervisor.handleProblem(problem);}else if ( problemType == 3 ) { MiddleManager manager = new MiddleManager(); manager.handleProblem(problem);}else if ( problemType == 4 ) { CEO ceo = new CEO(); ceo.handleProblem(problem);}Chain of Responsibility●And he feels great about his solution! Even better, it works!●Well... kind of. Two big problems develop:●First, the CEO can be a cranky fellow and doesn't like too many things demanding his attention. It's always possible someone lower on the chain could conceivably handle a problem even if it is very important. ●Second, Freaky Fruities is big on “process”. The company often tinkers with the job responsibilities of its managers. And so Ted is often changing around his if statements.●Dang, Ted thinks. I should have remembered design principles from my object oriented analysis and design class – encapsulate what varies.Chain of Responsibility●Ted remembers a presentation from that same OOA&D class on a pattern called the Chain of Responsibility. It stands out in his mind because the presentation coincidentally featured a character also named Ted who was also interested in customer service. In fact, it was that presentation that made Ted start thinking about changing his life and going into customer service at all.Chain of Responsibility●Introducing the Chain of Responsibility PatternThese concrete classes areGoing to be Ted, the supervisor, The middle manager, and the CEO(and anyone else who couldHandle a problem)This is an interface we'll create.Chain of Responsibility●The handlers will be chained together, and once someone in the chain can handle a problem, we stop.I can handle this one.Break the chain here,no reason to bother the CEOsuccessor.handle() successor.handle()Chain of Responsibility●Uses and advantages:●Use in cases where a client doesn't care who handles a request, only that it is handled.●Decouples clients from handlers of their requests.●Drawbacks:●There is no guarantee a request will be handled at all – it could fall off the end of the chain.Chain of Responsibility●First Ted does the easy part and codes up the interface:public interface ProblemHandler { public void handleProblem();}Chain of Responsibility●Next, codes up his classes to make them usable by the CoR pattern (as it is frequently abbreviated). Here's his MiddleManager class:public class MiddleManager implements ProblemHandler { private ProblemHandler successor; public void setSuccessor(ProblemHandler handler) { successor = handler; } public void handleProblem(Problem problem) { if (canHandle(problem)) { System.out.println("Middle manager handling the problem, " + "stopping the chain..."); } else { successor.handleProblem(problem); } }}Chain of Responsibility●And finally, Ted edits his client program to make use of the chain:ProblemHandler ted = new Ted();ProblemHandler supervisor = new Supervisor();ProblemHandler middleManager = new MiddleManager();ProblemHandler ceo = new CEO();ted.setSuccessor(supervisor);supervisor.setSuccessor(middleManager);middleManager.setSuccessor(ceo);ted.handleProblem(problem);Notice how we can nowcode to an interface insteadof an implementationSetting up the chain is aseasy as setting the successors.If the chain changes later, this is the only place we need to changeitChain of Responsibility●Now Ted can go back to what he really loves: the people.Chain of Responsibility●Variations●In classic CoR, the chain stops when one object in the chain handles the request. One variation is to keep passing the request along, even after it has been handled.●Another variation is that in addition to continuing to pass the request along the chain, handlers may modify the request itself. In order to keep the original request unchanged, this is sometimes done with the aid of the Decorator pattern – each handler that handles the request may decorate the request.Chain of Responsibility●Known modern uses:●Microsoft Windows hook framework:–http://msdn.microsoft.com/en-us/library/ms644959●Java servlet filters:–http://www.oracle.com/technetwork/java/filters-137243.htmlThe Flyweight PatternFlyweight●Bill is the lead developer for SandwichCorp. SandwichCorp is the worldwide leader in delivering fresh sub sandwiches right to your door. It's Bill's job to work on the software SandwichCorp uses to place orders online and let the individual stores know how to prepare and deliver the


View Full Document

CU-Boulder CSCI 5448 - Additional Design Patterns

Documents in this Course
Django

Django

42 pages

ENRS

ENRS

30 pages

PhoneGap

PhoneGap

22 pages

Load more
Download Additional Design Patterns
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Additional Design Patterns and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Additional Design Patterns 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?