2004 10 TemplateMethodPattern - Net Objectives...Design Patterns Explained: A New Perspective on...

13
BB N Ne et t O Ob bj je ec ct ti i v v v e es s bjectives N N e e t t O O b b j j e e c c t t i i v v e e s s Address: 275 118th Avenue SE Suite 115 Bellevue, WA 98005 Telephone: (425) 688-1011 Email: [email protected] Volume 1, Issue 10 October 2004 In This Issue: The Template Method Pattern - p.1 Technical Book Recommendation - p.2 Personal Book Recommendation - p.5 Seminars We Can Give - p.7 Whats Happening At Net Objectives This Month - p.11 What We Do - Net Objectives Courses - p.12 The Template Method Pattern Articles and events of interest to the software development oommunity Overview In this chapter This chapter continues the e-commerce case study discussed thus far in Chapters 9, 16, and 18. In this chapter, I introduce the Template Method pattern by discussing additional requirements for the case study. I present the intent of the Template Method pattern. I describe the key features of the Template Method pattern. I describe how the Template Method pattern can eliminate redundancy.. I describe some of my experiences using the Template Method pattern in practice. Alan Shalloway, Net Objectives, [email protected] James R.Trott, Millennium Relief & Development Services, [email protected] (Chapter 19 from the Second Edition of their book: Design Patterns Explained: A New Perspective on Object-Oriented Design, just released this October) Design patterns do not exist in isolation, but work in concert with other design patterns to help you create more robust applications. In this book, you will gain a solid understanding of twelve core design patterns and a pattern used in analysis. You will gain enough of a foundation that you will be able to read the design pattern literature, if you want to, and possibly discover patterns on your own. Most importantly, you will be better equipped to create flexible and complete software that is easier to maintain. Overview -------------------------------------------------------------------------------------------------------------------- 1 More Requirements for the International e-commerce Case Study ---------------------------------------- 2 The Template Method Pattern----------------------------------------------------------------------------------------- 2 Applying the Template Method to the International e-commerce Case Study --------------------------- 2 Using the Template Method Pattern to Reduce Redundancy ------------------------------------------------ 3 Field Notes: Using the Template Method Pattern --------------------------------------------------------------------------9 Summary-------------------------------------------------------------------------------------------------------------------------------------11 Review Questions------------------------------------------------------------------------------------------------------- 11

Transcript of 2004 10 TemplateMethodPattern - Net Objectives...Design Patterns Explained: A New Perspective on...

BB

NNNeeettt OOObbbjjjeeeccctttiiivvveeesss

bjectives

NNeett OObbjjeeccttiivveess

Address: 275 118th Avenue SE Suite 115 Bellevue, WA 98005 Telephone: (425) 688-1011 Email: [email protected]

Volume 1, Issue 10 October 2004

In This Issue:

The Template Method Pattern

- p.1 Technical Book Recommendation

- p.2 Personal Book Recommendation

- p.5 Seminars We Can Give

- p.7 What�s Happening At Net Objectives This Month

- p.11 What We Do - Net Objectives Courses

- p.12

TThhee TTeemmppllaattee MMeetthhoodd PPaatttteerrnnArticles and events of interest to the software development oommunity

Overview In this chapter This chapter continues the e-commerce case study discussed thus far in Chapters 9, 16, and 18.

In this chapter,

� I introduce the Template Method pattern by discussing additional requirements for the case study.

� I present the intent of the Template Method pattern.

� I describe the key features of the Template Method pattern.

� I describe how the Template Method pattern can eliminate redundancy..

� I describe some of my experiences using the Template Method pattern in practice.

Alan Shalloway, Net Objectives, [email protected]

James R.Trott, Millennium Relief & Development Services, [email protected]

(Chapter 19 from the Second Edition of their book: �Design Patterns Explained: A New Perspective on Object-Oriented Design�, just released this October)

Design patterns do not exist in isolation, but work in concert with other design patterns to help you create more robust applications. In this book, you will gain a solid understanding of twelve core design patterns and a pattern used in analysis. You will gain enough of a foundation that you will be able to read the design pattern literature, if you want to, and possibly discover patterns on your own. Most importantly, you will be better equipped to

create flexible and complete software that is easier to maintain.

Overview --------------------------------------------------------------------------------------------------------------------1 More Requirements for the International e-commerce Case Study ----------------------------------------2 The Template Method Pattern-----------------------------------------------------------------------------------------2 Applying the Template Method to the International e-commerce Case Study ---------------------------2 Using the Template Method Pattern to Reduce Redundancy ------------------------------------------------3 Field Notes: Using the Template Method Pattern --------------------------------------------------------------------------9 Summary-------------------------------------------------------------------------------------------------------------------------------------11 Review Questions------------------------------------------------------------------------------------------------------- 11

2

Street Address: 25952SE 37th Way Issaquah WA 98029 Telephone: (425) 313-3065 Email: [email protected]

Address: 275 118th Avenue SE Suite 115 Bellevue, WA 98005 Telephone: (425) 688-1011 Email: [email protected]

Volume 1, Issue 10 October 2004 ©2004, Net Objectives, Al Rights Reserved

bjectives

More Requirements for the International e-commerce Case Study

New requirement: access multiple SQL database systems In the process of writing the international e-commerce application, suppose I get a new requirement to support both Oracle� and SQL Server� databases. Both of these systems are based on SQL (Structured Query Language), the common standard that makes it easier to use databases. Yet, even though this is a common standard at the general level, there are still differences in the details.

This steps are the same at the general level� For example, I know that in general, when executing queries on these databases, I will use the following steps:

1. Format the CONNECT command.

2. Send the database the CONNECT command.

3. Format the SELECT command.

4. Send the database the SELECT command.

5. Return the selected dataset.

. . . but the details differ The specific implementations of the databases differ, however, requiring slightly different formatting procedures.

The Template Method Pattern

Standardizing on the steps The Template Method is a pattern intended to help us generalize a common process - at an abstract level - from a set of different procedures. According to the Gang of Four, the intent of the Template Method pattern is to

Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Redefine the steps in an algorithm without changing the algorithm�s structure.1

In other words, although there are different methods for connecting and querying Oracle databases and SQL Server databases, they share the same conceptual process. The Template Method pattern gives us a way to capture this common ground in an abstract class while encapsulating the differences in derived classes. The Template Method pattern is about controlling a sequence common to different processes.

Applying the Template Method to the International e-commerce Case Study

The details are varying In international e-commerce case study, the variations in database access occur in the particular implementations of the steps involved. Figure 19-1 illustrates this.

1 Gamma, E., Helm, R., Johnson, R., Vlissides, J., Design Patterns: Elements of Reusable Object-Oriented Software, Boston, Mass.: Addison-Wesley, 1995, p. 325.

Technical Book Recommendation: Design Patterns Explained: A New Perspective on Object Oriented Design, 2nd Edition. (ISBN: 0321247140) This is greatly expanded and covers topics we alluded to had great importance. In particular, Commonality Variability Analysis and lots on factories. Worth getting even if you've read the first one (much longer and better).

AVAILABLE NOW

3

bjectives

Volume 1, Issue 10 October 2004 ©2004, Net Objectives, All Rights Reserved

Address: 275 118th Avenue SE Suite 115 Bellevue, WA 98005 Telephone: (425) 688-1011 Email: [email protected]

How this works: virtual methods for the steps that vary I have created a method called doQuery that handles the query I need to perform. I pass it the name of the database and the query specification. The doQuery method follows the five general steps above, providing virtual methods for the steps (such as formatConnect and formatSelect) that must be implemented differently.

The doQuery method is implemented as follows. As shown in Figure 19-1, it first needs to format the CONNECT command required to connect to the database. Although the abstract class (QueryTemplate) knows this format needs to take place, it doesn�t know how to do it. The exact formatting code is supplied by the derived classes. This is true for formatting the SELECT command as well.

The Template Method pattern manages to do this because the method call is made via a reference pointing to one of the derived classes. That is, although QueryControl has a reference of type QueryTemplate, it is actually referring to an OracleQT or an SQLSvrQT object. Thus, when the doQuery method is called on either of these objects, the methods resolved will first look for methods of the appropriate derived class. Let�s

say our QueryControl is referring to an OracleQT object. Since OracleQT does not override QueryTemplate, the QueryTemplate�s doQuery method is invoked. This starts executing until it calls the formatConnect method. Since the OracleQT object was requested to perform doQuery, the OracleQT�s formatConnect method is called. After this, control is returned to the QueryTemplate�s doQuery method. The code common to all queries is now executed until the next variation is needed�the formatSelect method. Again, this method is located in the object that QueryControl is referring to (OracleQT in this example).

When a new database is encountered, the Template Method provides us with a boilerplate (or template) to fill out. We create a new derived class and implement the specific steps required for the new database in it.

Using the Template Method Pattern to Reduce Redundancy

Eliminating redundancy can lead to abstractions Many times in my consulting practice, I work with teams of people who are very sharp but who do not yet have strong backgrounds in object-orientation. They are transitioning to agile

Figure 19-1 Using the Template Method pattern to perform a query.

4

Street Address: 25952SE 37th Way Issaquah WA 98029 Telephone: (425) 313-3065 Email: [email protected]

Address: 275 118th Avenue SE Suite 115 Bellevue, WA 98005 Telephone: (425) 688-1011 Email: [email protected]

Volume 1, Issue 10 October 2004 ©2004, Net Objectives, Al Rights Reserved

bjectives

Copy-and-paste and changing code leaves redundancy Figure 19-2 shows an example of some "code" that I will use to show the shortcomings of copy-and-paste. Note that I am just showing the "code" as a sequence of letters, because the details of the code do not matter. I am trying to help you see the problems of

methods, they appreciate the need for concepts such as eliminating redundancy, having strong cohesion, and having loose coupling; however, they do not know how to go about getting there.

On one occasion, I was asking the team lead to describe a typical problem they had. He related to me how they had to support systems for different partner companies. The rules were pretty much the same, but there were always subtle differences. The code was becoming increasingly hard to maintain because it had so many "if-then-else" statements scattered throughout to check which situation was current and how to handle it. He could only see two alternatives, neither of them very good:

� Continue putting in more "if-then-else" statements, further degrading the code

� Copy-and-paste the code for each case, resulting in duplication

Using "if-then-else" statements or switches is the common approach taken here. The difficulty with this is switch creep. At first, a few "if-then-else" statements or switches is not bad. But at some point (around 57 of them?) the code is really difficult to understand. The advantage of the copy-and-pasteapproach is that although it doesn't seem particularly elegant, at least each section is clear because it only relates to one situation.

Because I knew the Template Method pattern, I could offer a third alternative. I will illustrate the alternative in two steps. First, I will show what happens when people copy-and-paste and then update the code. It turns out that even if all of the code is changed, there is still a process common to both - if not there wouldn't be a reason to do the copy-and-paste. Second, I'll show how once this duplication of process is recognized, one can refactor the code to eliminate it.

After I use copy-and-paste to change the code, I might end up with a new sequence of code, as shown in Figure 19-3. Note that in this copy-and-paste operation, some of the a�s have been converted to A�s, b�s to B�s, etc. Where a letter has changed to a capital, this indicates code that was changed after the copy-and-paste. Where it remains lower-case, this is code that was simply duplicated. The new row of X�s is code that was added to the new case, but did not exist at all in the original.

aaa aaa aaa aaabb bb bb bb bb cccc cccc cccc d d d d d d d d d eee eee eee eee fffff ff fffff ff ggg gggg ggg h hh h hh h hh h iiiii iiiii iiiii iiiii

AAA A AAAA AXXX XX XX XBB BB B cccc cccc ccccDDD D DDD DEE EEEE E Efffff ff fffff ff GGG G G GGGGHHH HHH HH Hiiiii iiiii iiiii iiiii

Copy the code and paste it to new area and

make your changes

Using copy and paste to create new code from existing code results in redundancies

Figure 19-2 The original code.

Figure 19-3 The original code and the new code.

copy-and-paste more easily.

5

bjectives

Volume 1, Issue 10 October 2004 ©2004, Net Objectives, All Rights Reserved

Address: 275 118th Avenue SE Suite 115 Bellevue, WA 98005 Telephone: (425) 688-1011 Email: [email protected]

Different types of duplication There are at least two types of duplication here. The first is the more obvious: the lines with c�s, f�s, and i�s, are duplicated code. I copied-and-pasted the original code, and then changed most, but not all of it. The part I didn't change is obviously redundant. However, there is another duplication. There is a sequence of operations that is common to both code fragments. In other words, this type of copy-and-paste is mostly employed when there is a well-defined sequence of steps but the implementation of some of the steps has changed. I illustrate this in Figure 19-4.

The steps point out those things that are conceptually the same, but that are implemented in different ways (the difference illustrated here by the use of capital and lower-case letters, and by the addition of new code in some cases, shown as X�s). So, for example, in the original class step 1 consisted of lower-case a's and b's, but in the new (second) class, it consisted of capital A's, X's, and B's. The step exists in both classes, and so that fact is redundant, even though the implementation is not.

aaa aaa aaa aaabb bb bb bb bb

cccc cccc cccc

d d d d d d d d d eee eee eee eee

fffff ff fffff ff

aaa aaa aaa aaabb bb bb bb bb cccc cccc cccc d d d d d d d d d eee eee eee eee fffff ff fffff ff ggg gggg ggg h hh h hh h hh h iiiii iiiii iiiii iiiii

ggg gggg ggg h hh h hh h hh h

iiiii iiiii iiiii iiiii

AAA A AAAA AXXX XX XX XBB BB B cccc cccc ccccDDD D DDD DEE EEEE E Efffff ff fffff ff GGG G G GGGGHHH HHH HH Hiiiii iiiii iiiii iiiii

AAA A AAAA AXXX XX XX XBB BB B

cccc cccc cccc

DDD D DDD DEE EEEE E E

fffff ff fffff ff

GGG G G GGGGHHH HHH HH H

iiiii iiiii iiiii iiiii

Step

1St

ep 2

Step

3St

ep 4

Step

5St

ep 6

Step

1St

ep 2

Step

3St

ep 4

Step

5St

ep 6

Compare the code to identify those redundancies and refactor into steps

Personal Book Recommendation from Alan Shalloway:Think And Grow Rich by Napolean Hill. (ISBN: 0449214923) Great book, bad title. Really about how to manifest what you want in your life. If you want money - get money. If you

want something else, get something else. Actions I took directly based on recommendations from this book led to my two companies.

Figure 19-4 Comparing the code to identify redundancies

6

Street Address: 25952SE 37th Way Issaquah WA 98029 Telephone: (425) 313-3065 Email: [email protected]

Address: 275 118th Avenue SE Suite 115 Bellevue, WA 98005 Telephone: (425) 688-1011 Email: [email protected]

Volume 1, Issue 10 October 2004 ©2004, Net Objectives, Al Rights Reserved

bjectives

Some details missing To actually implement this takes filling in several details. First of all, the steps in the derived classes need to be declared as abstract and/or protected methods in the base class. Secondly, there may have been local method variables used between the steps. These either need to be passed in and returned or made as data members of the class. Those common to both steps should be put in the base class.

Eliminating the duplication The Template Method pattern could be used to eliminate duplication here. It prescribes a base class that implements the step sequence. Each case then has its own derivative class to implement the specific steps. This is shown inFigure 19-5.

About the authors -- Alan Shalloway Alan Shalloway is the CEO and senior consultant of Net Objectives. Since 1981, he has been both an OO consultant and developer of software in several industries. Alan is a frequent speaker at prestigious conferences around the world, including: SD Expo, Java One, OOP, OOPSLA. He is the primary author of Design Patterns Explained: A New Perspective on Object-Oriented Design and is currently co-authoring three other books in the software development area. He is a certified Scrum Master and has a Masters in Computer Science from M.I.T. James Trott James Trott is a knowledge management consultant, collaboration specialist, and knowledge engineer. He has Master of Science in Applied Mathematics, an MBA, and a Master of Arts in Intercultural Studies. He has twenty years experience as a pattern-based analyst, working in the petroleum, aerospace, software, and financial industries. He divides his consulting efforts between corporate work and humanitarian work with Millennium Relief and Development Services.

Step 1

cccc cccc cccc

Step 3

fffff ff fffff ff

Step 5

iiiii iiiii iiiii iiiii

...results in a simpler �template�. Each diamond is a candidate for separate methods

someMethod () { methodForStep1()

cccc cccc cccc cccc cccc ccccmethodForStep3()

fffff ff fffff ff fffff ff fffffmethodForStep5()

iiiii iiiii iiiii iiiii iiiii iiiii iiiii }}

BaseClass

methodForStep1() {aaa aaa aaa aaabb bb bb bb bb }methodForStep3() {d d d d d d d d d eee eee eee eee }methodForStep5() {ggg gggg ggg h hh h hh h hh h }

MyClass

methodForStep1() {AAA A AAAA AXXX XX XX XBB BB B }methodForStep3() {DDD D DDD DEE EEEE E E}methodForStep5() {DDD D DDD DEE EEEE E E}

MySecondClass

Figure 19-5 Leads to the Template Method pattern

7

bjectives

Volume 1, Issue 10 October 2004 ©2004, Net Objectives, All Rights Reserved

Address: 275 118th Avenue SE Suite 115 Bellevue, WA 98005 Telephone: (425) 688-1011 Email: [email protected]

Seminars We Can Give

Transitioning to Agile � More and more companies are beginning to see the need for Agile Development. In this seminar, we discuss what problems agility will present and how to deal with these. Test-First Techniques Using xUnit and Mock Objects � This seminar explains the basics of unit testing, how to use unit tests to drive coding forward (test-first), and how to resolve some of the dependencies that make unit testing difficult. Pattern Oriented Development: Design Patterns From Analysis To Implementation � This seminar discusses how design patterns can be used to improve the entire software development process - not just the design aspect of it. Agile Planning Game � The Planning Game was created by Kent Beck and is well described in his excellent book: Extreme Programming Explained. Unfortunately, the Planning Game as described is not complete enough - even for pure, XP teams. This seminar describes the other factors which must often typically be handled. Comparing RUP, XP, and Scrum: Mixing a Process Cocktail for Your Team - This seminar discusses how combining the best of some popular processes can provide a successful software development environment for your project. Design Patterns and Extreme Programming � Patterns are often thought of as an up-front design approach. That is not accurate. This seminar illustrates how the principles and strategies learned from patterns can actually facilitate agile development. This talk walks through a past project of the presenter. Introduction to Use Cases � In this seminar we present different facets of the lowly Use Case; what they are, why they're important, how to write one, and how to support agile development with them. Unit Testing For Emergent Design � This seminar illustrates why design patterns and refactoring are actually two sides of the same coin.

Net Objectives Consulting Division The consultants at Objective Result are industry and technology experts who have been carefully selected and trained by Net Objectives educators. Objective Result provides Project Management, Development, and Quality Assurance consulting services. The driving force behind Objective Result is a firm belief in the natural synergy that exists between ongoing well trained consultants following the techniques taught through the Net Objectives courses.

Contact Peter Stroeve at [email protected] if you want to join Net Objectives Consulting

Check www.netobjectives.com/events/pr_main.htm#UpcomingPublicCourses For a complete schedule of upcoming Public Courses

in Western Washington State, Midwest, and Northern California and information on how to register

Check www.netobjectives.com/events/pr_main.htm#FreeSeminars for Public Seminars in

Western Washington State, Midwest, and Northern California

8

Street Address: 25952SE 37th Way Issaquah WA 98029 Telephone: (425) 313-3065 Email: [email protected]

Address: 275 118th Avenue SE Suite 115 Bellevue, WA 98005 Telephone: (425) 688-1011 Email: [email protected]

Volume 1, Issue 10 October 2004 ©2004, Net Objectives, Al Rights Reserved

bjectives

An approach to avoid the duplication in the first place Of course, refactoring to fix a bad situation after it is identified is preferable to just leaving it alone. However, refactoring to avoid the bad situation in the first place is preferable even more. In this situation, if we recognized the Template Method pattern approach's viability when the second system became a requirement, we could take the following approach.

1. First, refactor the first solution (using theExtract Method) to pull out the code that will change. This is shown in Figure 19-6. By the way, "pull out the code that will change" should sound a little similar to the Gang of Four's advice, "find what varies and encapsulate it." Instead of specializing the method, I have the "someMethod" method contain other methods that will change.

2. Second, create a base class that contains the method (someMethod) that will not change. The code now looks like the code in Figure 19-5 for BaseClass and MyClass (I have not yet writtenMySecondClass).

3. Write MySecondClass. Note that adding the new function now will not affect any other code except for the factory (and possibly not that).

Of course, wherever I indicate that I should "extract method", you might notice that this would not have been necessary if I'd been programming by intention in the first place. Good practices like this one tend to show up again and again, which is why they're good practices in the first place. Knowing patterns helps guide refactoring This is a generally good approach for refactoring. When a new requirement needs to be handled, take this two-step approach:

�Leadership is about coping with change. Management is about coping with complexity.�

� John P. Kotter: "What Leaders Really Do"

Figure 19-6 Refactoring out the steps that will change

9

bjectives

Volume 1, Issue 10 October 2004 ©2004, Net Objectives, All Rights Reserved

Address: 275 118th Avenue SE Suite 115 Bellevue, WA 98005 Telephone: (425) 688-1011 Email: [email protected]

1. Refactor your code without adding any function so the new function can go in following the Open-Closed Principle

2. Add your new code, touching only the factory and the new code.

Knowing patterns will help you follow this approach in many different situations.

Knowing Patterns Can Help Code Quality Without Causing Extra Work

The story I just related is an example about how knowing patterns can help one follow the practices of eXtreme Programming (XP). A skilled XP designer would know to consider (and do if appropriate) something like the Template Method pattern in the case described. If this were the best way to handle avoiding redundancy, the pattern would be implemented. The XP designer may not think of themselves as "following the pattern" but knowledge of the pattern gives one an option they may not think of otherwise. Unfortunately, a designer not yet experienced in XP may just never recognize that such a solution is available. They may not even recognize that by copying and pasting, they are creating a redundant procedure even if they change all of the code.. This again confirms my point that patterns are guides. They can be used as effectively in an XP style coding project as they can in an up-front design one.

Field Notes: Using the Template Method Pattern

The Template Method is not coupled Strategies Sometimes a class will use several different Strategy patterns. When I first looked at the class diagram for the Template Method pattern, I thought, �Oh, the Template Method pattern is simply a collection of Strategies that work together.� This is dangerous (and usually incorrect) thinking. While it is not uncommon for several Strategies to appear to be connected to each other, designing for this can lead to inflexibility.

The Template Method pattern is applicable when there are different, but conceptually similar processes. The variations for each process are coupled together because they are associated with a particular process. In the example I presented, when I need a format a CONNECT command for an Oracle database, if I need a format a QUERY command, it�ll be for an Oracle database as well.

10

Street Address: 25952SE 37th Way Issaquah WA 98029 Telephone: (425) 313-3065 Email: [email protected]

Address: 275 118th Avenue SE Suite 115 Bellevue, WA 98005 Telephone: (425) 688-1011 Email: [email protected]

Volume 1, Issue 10 October 2004 ©2004, Net Objectives, Al Rights Reserved

bjectives

The Template Method Pattern:

Key Features

Intent Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Redefine the steps in an algorithm without changing the algorithm�s structure.

Problem There is a procedure or set of steps to follow that is consistent at one level of detail, but individual steps may have different implementations at a lower level of detail.

Solution Allows for definition of sub-steps that vary while maintaining a consistent basic process.

Participants and Collaborators

The Template Method consists of an abstract class that defines the basic TemplateMethod (see figure below) classes that need to be overridden. Each concrete class derived from the abstract class implements a new method for the Template.

Consequences Templates provide a good platform for code reuse. They also are helpful in ensuring the required steps are implemented. They bind the overridden steps together for each Concrete class, and so should only be used when these variations always and only occur together.

Implementation Create an abstract class that implements a procedure using abstract methods. These abstract methods must be implemented in subclasses to perform each step of the procedure. If the steps vary independently, each step may be implemented with a Strategy pattern.

Figure 19-7 Generic structure of the Template Method pattern.

11

bjectives

Volume 1, Issue 10 October 2004 ©2004, Net Objectives, All Rights Reserved

Address: 275 118th Avenue SE Suite 115 Bellevue, WA 98005 Telephone: (425) 688-1011 Email: [email protected]

To join a discussion about this article, please go tohttp://www.netobjectivesgroups.com/6/ubb.x

and look under the E-zines category.

What�s Happening at Net Objectives This Month

We are pleased to unveil the second installment of our monthly streamzines, �Patterns and Forces�:

http://www.netobjectives.com/streamzines/CurrentStreamzine In this presentation, Scott Bain explores the relationship between patterns and the forces

in the problem domain.

�Streamzines� are recorded multimedia presentations, 15 � 60 minutes long, in which one of our consultants will explore a particular software issue in depth. This offers a taste of one

our seminars or courses, but here you can interrupt, rewind, and pause the instructor at will.

If you are not currently receiving links to monthly Ezines and Streamzines, as well as to upcoming public courses and seminars, become a member of our mailing list.

Fill out and submit our online form at: http://www.netobjectives.com/subscribe.htm

Summary

In this chapter Sometimes, I have a set of procedures that I must follow. The procedures are common at a high level, but implementing some of the steps can vary. For example, querying a SQL database is fairly routine at a high level, but some of the details�say, how to connect to the database�can vary based on details such as the platform.

The Template Method pattern allows me to define the sequence of steps and then override those steps that need to change.

The Template Method pattern also allows me to avoid introducing redundancy when I need to support multiple systems that behave conceptually the same but have different implementations for each of their steps.

Review Questions

Observation 1. The Template Method pattern makes the

method call in a special way. What is that?

Interpretation 1. According to the Gang of Four, the intent of

the Template Method pattern is to "Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Redefine the steps in an algorithm without changing the algorithm�s structure" What does this mean?

2. The Gang of Four calls this a "Template Method". Why do they do this?

3. What is the difference between the Strategy pattern (chapter 9) and the Template Method pattern?

12

Street Address: 25952SE 37th Way Issaquah WA 98029 Telephone: (425) 313-3065 Email: [email protected]

Address: 275 118th Avenue SE Suite 115 Bellevue, WA 98005 Telephone: (425) 688-1011 Email: [email protected]

Volume 1, Issue 10 October 2004 ©2004, Net Objectives, Al Rights Reserved

bjectives

Go

to h

ttp:

//w

ww

.net

obje

ctiv

es.c

om/s

ubs

crib

e.h

tm t

o su

bscr

ibe

to o

ur

mai

ling

list

and

rece

ive

futu

re e

zin

es a

nd

sem

inar

an

nou

nce

men

ts

Net Objectives - What We Do

Net Objectives provides enterprises with a full selection of training, coaching and consulting services. Our Vision is �Effective software development without suffering�. We facilitate software development organizations migration to more effective and efficient processes. In particular, we are specialists in agility, effective analysis, design patterns, refactoring and test-driven development. We provide a blend of training, follow up coaching and staff supplementation that enables your team to become more effective in all areas of software development. Our engagements often begin with an assessment of where you are and detail a plan of how to become much more effective. Our trainers and consultants are experts in their fields (many of them published authors). When you�ve taken a course from Net Objectives, you will see the world of software development with new clarity and new insight. Our graduates often tell us they are amazed at how we can simplify confusing and complicated subjects, and make them seem so understandable, and applicable for everyday use. Many of our students remark that it is the best training they have ever received. The following courses are among our most often requested. This is not a complete list, though it is representative of the types of courses we offer Agile Project Management - This 2-day course analyzes what it means to be an agile project, and provides a number of best practices that provide and/or enhance agility. Different agile practices from different processes (including RUP, XP and Scrum) are discussed. Agile Use Case Analysis - This 3-day course provides theory and practice in deriving software requirements from use cases. This course is our recommendation for almost all organizations, and is intended for audiences that mix both business and software analysts. Design Patterns Explained: A New Perspective on Object-Oriented Design - This 3-day course teaches several useful design patterns as a way to explain the principles and strategies that make design patterns effective. It also takes the lessons from design patterns and expands them into both a new way of doing analysis and a general way of building application architectures. Test-Driven Development: Iterative Development, Refactoring and Unit Testing - This 3-day course teaches how to use either Junit, NUnit or CppUnit to create unit tests. Lab work is done in both Refactoring and Unit Testing together to develop very solid code by writing tests first. The course explains how the combination of Unit Testing Refactoring can result in emerging designs of high quality. Effective Object-Oriented Analysis, Design and Programming In C++, C#, Java, or VB.net - This 5-day course goes beyond the basics of object-oriented design and presents 14 practices which will enable your developers to do object-oriented programming effectively. These practices are the culmination of the best coding practices of eXtreme Programming and of Design Patterns. Software Dev Using an Agile (RUP, XP, SCRUM) Approach and Design Patterns - This 5-day course teaches several design patterns and the principles underneath them, the course goes further by showing how patterns can work together with agile development strategies to create robust, flexible, maintainable designs

If you are interested in any of these offerings, if your user group or company is interested in Net Objectives making a free technical presentation to them, or if you would like to be notified

of upcoming Net Objectives events, please visit our website, or contact us by the email address or phone number below:

www.netobjectives.com ! [email protected] ! 404-593-8375

Business-Driven Software Development™Assessments, Consulting, Training, and Coaching

Business-Driven Software Development (BDSD) is Net Objectives’ proprietary integration of Lean-Thinking with Agile methods across the business, management and development teams to maximize the value delivered from a software development organization. BDSD has built a reputation and track record of delivering higher quality products faster and with lower cost than other methodsBDSD goes beyond the first generation of Agile methods such as Scrum and XP by viewing the entire value stream of develop-ment. Lean-Thinking enables product portfolio management, release planning and critical metrics to create a top-down vision while still promoting a bottom-up implementation.BDSD integrates business, management and teams. Popular Agile methods, such as Scrum, tend to isolate teams from the busi-ness side and seem to have forgotten management’s role altogether. These are critical aspects of all successful organizations. In BDSD:

• Business provides the vision and direction; properly select-ing, sizing and prioritizing those products and enhancements that will maximize your investment

• Teams self-organize and do the work; consistently delivering value quickly while reducing the risk of developing what is not needed

• Management bridges the two; providing the right environ-ment for successful development by creating an organiza-tional structure that removes impediments to the production of value. This increases productivity, lowers cost and improves quality

Become a Lean-Agile EnterpriseAll levels of your organization will experience impacts and require change management. We help prepare executive, mid-management and the front-line with the competencies required to successfully change the culture to a Lean-Agile enterprise.Prioritization is only half the problem. Learn how to both prioritize and size your initiatives to enable your teams to implement them quickly.Learn to come from business need not just system capability. There is a disconnect between the business side and development side in many organizations. Learn how BDSD can bridge this gap by providing the practices for managing the flow of work.

Why Net ObjectivesWhile many organizations are having success with Agile methods, many more are not. Much of this is due to organizations either starting in the wrong place (e.g., the team when that is not the main problem) or using the wrong method (e.g., Scrum, just because it is popular). Net Objectives is experienced in all of the Agile team methods (Scrum, XP, Kanban, Scrumban) and integrates business, management and teams. This lets us help you select the right method for you.

Assessments

See where you are, where you want to go, and how to get there.

Business and Management Training

Lean Software Development

Product Portfolio Management

Enterprise Release Planning

Productive Lean-Agile Team Training

Team training in Kanban, Scrum

Technical Training in ATDD, TDD, Design Patterns

Roles Training

Lean-Agile Project Manager

Product Owner

Assessments

See where you are, where you want to go, and how to get there.

Business and Management Training

Lean Software Development

Product Portfolio Management

Enterprise Release Planning

Productive Lean-Agile Team Training

Team training in Kanban, Scrum

Technical Training in ATDD, TDD, Design Patterns

Roles Training

Lean-Agile Project Manager

Product Owner

For more information about Business Driven Software Development ServicesContact Mike Shalloway at Net Objectives: [email protected] / 404-593-8375

NetObjectives.com / 1-888-LEAN-244 (888-532-6244)

Team

LeanEnterprise

Management

Business

Lean for Execu�vesProduct Por olioManagement

Kanban / Scrum / ATDD

Design Pa�erns / TDD

Lean-Management

Project Management