Singleton Design Pattern and Stratego Class Implementation, Lab Reports of Computer Science

Instructions on how to implement the singleton design pattern in the stratego class. The singleton design pattern ensures that a class has one instance and provides a global point of access to it. The document also includes modifications to the tokenhandler, stratego, board, space, and token classes for labs 6 and 7. The description covers the purpose of the singleton design pattern, its implementation in the stratego class, and modifications to existing classes.

Typology: Lab Reports

Pre 2010

Uploaded on 07/23/2009

koofers-user-r3u
koofers-user-r3u 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 3230 Lab #File 6
Patterns, Polymorphism, Algorithms, and GUI 3
Design Patterns
The idea of a design pattern was first used by Christopher Alexander (The Timeless Way of
Building, 1979, New York: Oxford University Press), an architect, to describe a language of
building. Alexander’s ideas have had a great impact on computer science (see Vol. 1, p. 363 of
the course text book). Although there are now many books in print that describe useful design
patterns used to solve programming problems, one of the first, and still one of the best and most
influential, is Design Patterns: Elements of Reusable Object-Oriented Software by Gamma,
Helm, Johnson, & Vlissides (1997, Boston: Addison-Wesley).
Gamma, Helm, Johnson, & Vlissides (often referred to as the gang of four), describe 23 common
design patterns. Java utilizes two of their design patterns: factory method, and iterator. Java
also uses the model-view-controller design pattern first used in Smalltalk (see Vol. 1, pp. 364-
368). Alexander maintains that, “Each pattern describes a problem which occurs over and over
again in our environment, and 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 sam way twice” (p.
x). Gamma, Helm, Johnson, & Vlissides formalize design patterns, in the context of software
engineering by specifying four components of each pattern: (a) a pattern name, (b) the problem,
(c) the solution, and (d) the consequences of using the pattern (p. 3).
The Singleton Design Pattern
The singleton is a simple but nevertheless useful design pattern. According to Gamma, Helm,
Johnson, & Vlissides, it’s purpose is to “ensure a class has one instance, and provide a global
point of access to it” (p. 127). We will apply the singleton pattern to the Stratego class.
There are several ways of implementing a singleton, but only one is considered here (including a
simplification that is possible in the case of Stratego).
1. Define a static instance variable that points to the single instance of Stratego in the
program:
private static Stratego instance = null;
2. Make the constructor private (what does this accomplish and why is it important?)
3. Define a Stratego operation/method to create an instance of Stratego (but see step 5)
public void makeStratego()
{if (instance != null)
return;
instance = new Stratego();
}
Some programmers will combine this functionality with the operation describe in step 4.
pf3
pf4
pf5

Partial preview of the text

Download Singleton Design Pattern and Stratego Class Implementation and more Lab Reports Computer Science in PDF only on Docsity!

CS 3230 Lab #File 6

Patterns, Polymorphism, Algorithms, and GUI 3

Design Patterns

The idea of a design pattern was first used by Christopher Alexander ( The Timeless Way of

Building , 1979, New York: Oxford University Press), an architect, to describe a language of

building. Alexander’s ideas have had a great impact on computer science (see Vol. 1, p. 363 of

the course text book). Although there are now many books in print that describe useful design

patterns used to solve programming problems, one of the first, and still one of the best and most

influential, is Design Patterns: Elements of Reusable Object-Oriented Software by Gamma,

Helm, Johnson, & Vlissides (1997, Boston: Addison-Wesley).

Gamma, Helm, Johnson, & Vlissides (often referred to as the gang of four), describe 23 common

design patterns. Java utilizes two of their design patterns: factory method, and iterator. Java

also uses the model-view-controller design pattern first used in Smalltalk (see Vol. 1, pp. 364-

368). Alexander maintains that, “Each pattern describes a problem which occurs over and over

again in our environment, and 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 sam way twice” (p.

x). Gamma, Helm, Johnson, & Vlissides formalize design patterns, in the context of software

engineering by specifying four components of each pattern: (a) a pattern name, (b) the problem,

(c) the solution, and (d) the consequences of using the pattern (p. 3).

The Singleton Design Pattern

The singleton is a simple but nevertheless useful design pattern. According to Gamma, Helm,

Johnson, & Vlissides, it’s purpose is to “ensure a class has one instance, and provide a global

point of access to it” (p. 127). We will apply the singleton pattern to the Stratego class.

There are several ways of implementing a singleton, but only one is considered here (including a

simplification that is possible in the case of Stratego).

1. Define a static instance variable that points to the single instance of Stratego in the

program:

private static Stratego instance = null;

2. Make the constructor private (what does this accomplish and why is it important?)

3. Define a Stratego operation/method to create an instance of Stratego (but see step 5)

public void makeStratego()

if (instance != null)

return;

instance = new Stratego();

Some programmers will combine this functionality with the operation describe in step 4.

4. Define a static access method in Stratego that returns a reference to the single instance:

public static Stratego getInstance()

return instance;

This allows you to write such statements as Stratego.getInstance().win();

or tray == Stratego.getInstance().getBlueTray(); anywhere in the

program — that is, you can access the public features of the Stratego class where ever

doing so is convenient. Step 3 and 4 are often combined in practice:

public static Stratego getInstance()

if (instance == null)

instance = new Stratego();

return instance;

5. A simplification is possible in this program because the Stratego class is the entry point

to the entire program. Step 3 is not needed in the current program because main is

defined inside of Stratego, which allows it (main) to call the private constructor.

Therefore, you should initialize instance in the Stratego constructor: instance = this;

Modifications to Existing Classes

TokenHandler

  • The TokenHandler class provided with lab #4 also has code need for labs 6 and 7. This

code is included but is commented out. Search for lab 6 code and remove the comments.

However, leave in place the comments for code intended for lab.7.

  • Line 226: “target.getToken().reveal();” should be commented out as a lab.7 feature.
  • Note the comment and statement at lines 66 and 67.

Stratego

Modify the provided Stratego.java as follows (or merge with your Stratego as is appropriate):

  • Make Stratego a singleton as described above
  • Add a “Start” button at the bottom of the frame; when pressed, it should set the mode to

PLAY

  • Add accessor methods to get the trays and the field:
    • public int getMode()
    • public Board getBlueTray()
    • public Board getRedTray()
    • public Board getField() (used in lab 7)
    • Note that the action of the win method won’t make any sense until lab 7
  • The provided Stratego class defines a method named fillTray, which you may use or

discard (it is only used by the constructor to fill the red and blue trays)

Token

  • Add the following two methods (the algorithm for validateMove is presented below):
  • Validates the attempted move of a token from one space to another.
  • @param start the space where the token attempting to move is
  • currently located.
  • @param end the space to which the token is trying to move.
  • @return true if the move is allowed by the rules of the game,
  • otherwise false. */

public boolean validateMove(Space start, Space end)

  • Determines the victor when one token strikes another.
  • This token is attacking or striking the attacked token.
  • @param attacked the token being attacked.
  • @return 1 if this token wins (if the attacked token is
  • a flag of this token's rank is greater than the attacked
  • token's rank); -1 if the attacked token wins (the attacked
  • token is a bomb or if the attacked token's rank is greater
  • than this token's rank); or 0 if the ranks of the two tokens
  • are equal. */

public int strike(Token attacked)

  • Create four additional Token classes as illustrated in the UML diagram:

The Token class (which only has two of its methods illustrated in the UML diagram) will

continue to represent most of the game tokens. The FixedToken class, which only

overrides the validateMove method, will represent Bombs, Flags, and Lakes. The other

classes are self-explanatory.

ScoutToken will be written in class as an example.

+valida teMove(start Sp ace, end : S pace) : boolean +str ike( attacked : Toke n) : int

Token

+valida teMove(start : S pace, en d : Space) : boo lean

FixedToken +valida teMove(start : S pace, en d : Space) : boo lean

ScoutToken

+strike (attacked : Token) : int

MinerTok en +strike(attacke d : To ken ) : int

SpyToken

Token validateMove

public boolean validateMove(Space start, Space end)

If Stratego is in SETUP mode

If the token’s player and Stratego’s player are not the same or the end space is not empty,

then the move is not valid: return false.

Get the board containing the end space (this could be one of the trays or the field – see

ScoutToken for an example).

If the token’s player is BLUE and either the board is the blue tray or the row of the end

space is < 4, then the move is valid: return true

Else, if the token’s player is RED and either the board is the red tray or the row of the

end space is > 5, then the move is valid: return true

Else return false

If the move is only one row or one column, then it is valid: return true

FixedToken validateMove

If this is a lake, no movement is allowed: return false

If Stratego is in the SETUP mode, then use (i.e., call) token’s validateMove.

Otherwise, no movement is allowed: return false.

MinerToken strike

If the attacked token is a BOMB, the miner wins: return 1.

Else, use (i.e., call) the token’s strike method.

SpyToken strike

If the attacked token is a MARSHAL, the spy wins: return 1.

Else, use (i.e., call) the token’s strike method.

Grading

Demonstrate your program during lab time.