Docsity

Understanding Design Patterns and JUnit: Structural & Behavioral Patterns with JUnit in XP, Study notes of Computer Science

Toggle sidebar
bg1
9/15/03 Notes
Design Pattern & JUnit
-------------------------------------
1 Design Patterns
1.1 What is Design Pattern?
• The design patterns are descriptions of communicating objects and classes that are customized to
solve a general design problem in a particular context.
• A design pattern names, abstracts and identifies the key aspects of a common design structure that
make it useful for creating a reusable object-oriented design.
• The design pattern identifies the participating classes and instances, their roles and collaborations
and the distribution of responsibilities.
Design Patterns fall into many different categories – structural, creational, behavioral, fundamental
design, partitioning, and concurrency patterns, but here we will only discuss two types – Structural and
Behavioral patterns. Please refer to [1] for more patterns.
1.2 Structural Patterns
1.2.1 Adapter
Intent
• Convert the interface of a class into another interface clients expectation – Adapter lets classes
work together that couldn't otherwise because of incompatible interfaces.
• Also Knows as: Wrapper
1/12
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Understanding Design Patterns and JUnit: Structural & Behavioral Patterns with JUnit in XP and more Study notes Computer Science in PDF only on Docsity!

Design Pattern & JUnit

1 Design Patterns

1.1 What is Design Pattern?

  • The design patterns are descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context.
  • A design pattern names, abstracts and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design.
  • The design pattern identifies the participating classes and instances, their roles and collaborations and the distribution of responsibilities. Design Patterns fall into many different categories – structural, creational, behavioral, fundamental design, partitioning, and concurrency patterns, but here we will only discuss two types – Structural and Behavioral patterns. Please refer to [1] for more patterns. 1.2 Structural Patterns 1.2.1 Adapter Intent
  • Convert the interface of a class into another interface clients expectation – Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
  • Also Knows as: Wrapper

Structure Figure 1

  • Target - defines the domain-specific interface that Client uses.
  • Client - collaborates with objects conforming to the Target interface.
  • Adaptee - defines an existing interface that needs adapting.
  • Adapter - adapts the interface of Adaptee to the Target interface.
  • Collaborations - Clients call operations on an Adapter instance. In turn, the adapter calls Adaptee operations that carry out the request. Example Figure 2 Consider a drawing editor that lets users draw and arrange graphical elements (lines, polygons, text, etc.) into pictures and diagrams. The drawing editor's key abstraction is the graphical object, which has an editable shape and can draw itself. The interface for graphical objects is defined by an abstract class called Shape. The editor defines a subclass of Shape for each kind of graphical

Structure Figure 3

  • Component
    • Declares the interface for objects in the composition.
    • Implements default behavior for the interface common to all classes, as appropriate.
    • Declares an interface for accessing and managing its child components
    • Defines an interface for accessing a component’s parent in the recursive structure, and implements it if that is appropriate.
  • Leaf
    • Represents leaf objects in the composition. A leaf has no children.
    • Defines behavior for primitive objects in the composition.
  • Composite
    • Defines behavior for components having children.
    • Stores child components.
    • Implements child-related operations in the Component interface.
  • Client
    • Manipulates objects in the composition through the Component interface.

Example: Figure 4 Line, Rectangle and Text define primitive graphical objects which each define specific draw() methods. Picture defines an aggregate of Graphic objects and draw() calls draw() for all component objects. The abstract Graphic class declares all methods common to simple and composite objects. Code: http://www.dofactory.com/Patterns/PatternComposite.aspx#csharp 1.3 Behavioral Patterns 1.3.1 Command Intent: Encapsulate a request as an object, thereby letting you

  • parameterize clients with different requests,
  • queue or log requests, and
  • support undoable operations. Line +Draw() Rectangle +Draw() Text +Draw() forall g in graphics g.Draw() add g to list of graphics 1+ Graphic +Draw() +Add(Graphic) +Remove(Graphic) +GetChild(int) Picture +Draw() +Add(Graphic g) +Remove(Graphic) +GetChild(int) 1+

Code : http://www.dofactory.com/Patterns/PatternCommand.aspx#csharp 1.3.2 Template Method Design Pattern Intent

  • Define the skeleton of an algorithm in an operation, deferring some steps to subclasses.
  • Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure. Structure Figure 7
    • AbstractClass (Application)
      • defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm.
      • implements a template method defining the skeleton of an algorithm. The template method calls primitive operations as well as operations defined in AbstractClass or those of other objects.
    • ConcreteClass (MyApplication)
      • implements the primitive operations to carry out subclass-specific steps of the algorithm. ConcreteClass +PrimitiveOperation1 () +PrimitiveOperation2 () AbstractClass +TemplateMethod () +PrimitiveOperation1 () +PrimitiveOperation2 () TemplateMethod () { ... PrimitiveOperation1(); ... PrimitiveOperation2(); ... }

Example Figure 8 Code: http://www.dofactory.com/Patterns/PatternTemplate.aspx#csharp 1.3.3 Observer Intent

  • Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
  • Also known as: Dependents, Publish-Subscribe Structure

2 JUnit and Extreme Programming

2.1 What is JUnit? JUnit is an open source Java testing framework used to write and run repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:  Assertions for testing expected results  Test fixtures for sharing common test data  Test suites for easily organizing and running tests  Graphical and textual test runners 2.2 Design of JUnit Figure 11 JUnit is designed around the following patterns: the Command pattern, the Composite pattern, and the template pattern. A TestCase is a command object. Any class that contains test methods should subclass the TestCase class. A TestCase can define any number of public testXXX() methods. When you want to check the expected and actual test results, you invoke a variation of the assert() method.

TestCase subclasses that contain multiple testXXX() methods can use the setUp() and tearDown() methods to initialize and release any common objects under test, referred to as the test fixture. Each test runs in the context of its own fixture, calling setUp() before and tearDown() after each test method to ensure there can be no side effects among test runs. TestCase instances can be composed into TestSuite hierarchies that automatically invoke all the testXXX() methods defined in each TestCase instance. A TestSuite is a composite of other tests, either TestCase instances or other TestSuite instances. The composite behavior exhibited by the TestSuite allows you to assemble test suites of test suites of tests, to an arbitrary depth, and run all the tests automatically and uniformly to yield a single pass or fail status. 2.3 What is extreme programming? Extreme Programming (XP) was conceived and developed to address the specific needs of software development conducted by small teams in the face of vague and changing requirements. This new lightweight methodology challenges many conventional tenets, including the long-held assumption that the cost of changing a piece of software necessarily rises dramatically over the course of time. XP recognizes that projects have to work to achieve this reduction in cost and exploit the savings once they have been earned. Fundamentals of XP include:  Distinguishing between the decisions to be made by business interests and those to be made by project stakeholders.  Writing unit tests before programming and keeping all of the tests running at all times.  Integrating and testing the whole system-several times a day.  Producing all software in pairs, two programmers at one screen.  Starting projects with a simple design that constantly evolves to add needed flexibility and remove unneeded complexity.  Putting a minimal system into production quickly and growing it in whatever directions prove most valuable. XP emphasizes not just testing, but testing well. Tests are automated. Tests are created before the code is written, while the code is written, and after the code is written. 2.4 Relationship between JUnit and Extreme Programming JUnit is one of the testing frameworks for Extreme Programming