Introducing Design Patterns in First Year Object-Oriented Programming

ANDY C. H. LAW

Kwantlen University College

ABSTRACT

 

A new approach of teaching first year object-oriented programming is to teach students in a constructivist learning environment in which students acquire skills through solving simulated, real-world problems that are “owned” by them. Students are better motivated if real-world skills are presented and demonstrated. Such an approach is pedagogically important that maintains student interest and motivation. Design patterns, which are the best practice of applying object-oriented techniques in real-world software development, are introduced with appropriate, interesting, and engaging examples in the first year Java programming at Kwantlen University College. Students are found to have better understanding of how these real-world techniques can be applied in problem solving.

 

Keywords: Object-Oriented Programming, Design Patterns

 


 

1. Introduction

 

The instructional strategy proposed by constructivists is claimed to provide a better motivated learning environment than that of the traditional [1]. In constructivist-learning environment design, instructors select an appropriate, interesting, engaging, but ill-defined problem for students. Jonassen [1] describes an ill-defined problem as one has unstated goals, constraints, possesses multiple solutions and multiple criteria for evaluating solutions, and requires students to make judgments and more. Constructivists emphasize that a problem or learning goal must be “owned” by the student. The purpose of this paper is to share my experience of incorporating the constructivist strategy in teaching the first year object-oriented programming at Kwantlen University College. Since demonstrating real-world problem skills is one of the requirements suggested by constructivists, I have introduced in the course design patterns which are the best practice and real-world techniques adopted in software development. Section 2 describes the examples and activities when singleton and composite design patterns are introduced to my students. Section 3 discusses the course implementation plan. Section 4 concludes the achievement of using the new approach, and discusses the arguments made against such an approach.

 

 

2. Design Patterns

 

Design patterns are descriptions of the ways that different types of problem can be solved. They are intended to embody good design practice and are based on proven software development principles [2]. Selected design patterns are introduced in the first year Java programming [3] with the aim to maintain student’s interest and motivation.  

 

 

2.1. Singleton Pattern

 

A singleton pattern, which belongs to the class of creational design pattern, is introduced when I cover the concepts of abstraction and encapsulation. All creational patterns deal with ways to create instances of classes. In many cases, the exact nature of how the object is created could vary with the needs of the program. The best practice is to abstract the object initiation process into a class. The singleton pattern assures that there is one and only one instance of a class created, and provides a global point of access to it.

          During the lectures, examples have been selected or designed in such a way that they are appropriate, interesting, and engaging. I have used the example of building an electronic book (EBOOK) class. Suppose the college library has an EBOOK and anyone can sign out and view the EBOOK object.  Students are asked to implement the EBOOK class in Java, and most of them start with a simple approach of making the EBOOK object as a global data. Obviously, it is not an object-oriented approach because it violates the rule of encapsulation. They will then improve the solution by creating an EBOOK class because the class provides encapsulation. An EBOOK object can then be accessed through its methods. However, the EBOOK object cannot be referenced by its class name, but its object identifier (i.e. variable name). This ends up another problem that students must think of a good name for an EBOOK object and the object must be declared globally. A better solution would be the EBOOK object can be referenced by its class name. Students then learn that they can use the static method approach to overcome the problem. The method can be invoked by any client by referencing the class name as EBOOK.getEBookInstance(). To retrieve the book content, a client may send a message as EBOOK.getEBookContent().

          If the college holds only one license of the EBOOK, the above solution becomes incomplete. That is, the library has only one copy of the EBOOK and there is at almost one student can sign out and view the EBOOK object at one time. Students will then learn to use a private class constructor which prevents the EBOOK information from access by any clients, which implies that the only one who can create the EBOOK object should be the EBOOK class itself. The design specification in UML [4] and the Java implementation of EBOOK are shown in Figure 1.

 

 

public class EBOOK {

   private static EBOOK theEBook;

   private static boolean theEBookIsAvailable = true;

   private EBOOK () {}

   public static EBOOK getEBookInstance() {

        if (theEBookIsAvailable) {

            if (theEBook == null) {

                theEBook = new EBOOK ();

            }

            theEBookIsAvailable = false;

            return theEBook;

        }

        else {

            return null;   // EBook not available

        }

    }

    public static void getEBookContent() {

                //View EBook Content

    }

    public static void releaseTheEBook() {

        theEBookIsAvailable = true;

    }

}

Figure 1. EBOOK class (right) and its implementation (left Java code)

 

          In summary, the following activities are conducted to support the constructivist instructional strategy [5]: (1) Select appropriate problems, (2) Provide appropriate examples to enable case-base reasoning, (3) Provide relevant advices just-in-time. The process of demonstrating key concepts and techniques involves a number of learning activities [6] which are documented in Table 1.

 

 

Problems

Proposed solutions by students

Concepts learned and reflections

How to implement an EBOOK object so that it can be accessed by any clients?

Declare EBOOK as global data

The solution violates the rule of data encapsulation. Abstraction and Encapsulation should be applied.

How to ensure data encapsulation?

Declare EBOOK as class. EBOOK object can then be accessed by clients through its methods.

The EBOOK object has to be referenced by its object identifier. This ends up a problem of finding a good name for a globally declared object identifier.

How to ensure encapsulation without globally declaring an object identifier?

Use static method approach. The method getEBookInstance() is invoked by any client by referencing the class name instead of object identifier.

Students learned when, why and how static method is applied.

How to ensure only one instance of EBOOK is created?

Use a private class constructor which prevents the EBOOK information from access by any clients. This implies that only the EBOOK class can create an EBOOK object.

Students learned when, why and how private class constructor is applied. This is a singleton pattern.

 

Table 1. Learning Activities of Singleton Pattern

 

2.2. Composite Pattern

 

The EBOOK object is further made up of different types of learning media: text, sound, and video. Since these learning objects have attributes and operations (e.g. setContent(), getContent(), countMEDIA(), add(), remove(), etc.) in common, most students will define primitive objects TEXT, AUDIO, and VIDEO as subclasses from a base class called MEDIA. But a MEDIA object may contain a list of MEDIA objects (MEDIA_LIST) instead of the primitive objects. The composite pattern, which belongs to the class of structural pattern, can be applied when this kind of structural problem is encountered.

          In our example, all the subclasses of MEIDA have to overridden countMEDIA(), add(), and remove(). For an MEDIA_LIST object, an invocation of the countMEDIA(), for example, results in it sending a countMEDIA( ) message to each of its components in turn. Combining inheritance and aggregation, the relationships among MEDIA, MEDIA_LIST, TEXT, AUDIO, and VIDEO form the composite pattern. The composite pattern suggests that both the composite and the component objects should have the same interface. The advantage of such a pattern is that clients will treat both the composite and the component objects in the same way. The composite pattern in UML [4] and the Java implementation of MEDIA, VIDEO, and MEDIA_LIST are shown in Figure 2a and 2b respectively.

 

Figure 2a. Composite pattern in UML

 

MEDIA.java

 

public abstract class MEDIA {

   LinkedList mediaList;

   MEDIA composite;

   String name;

 

   public abstract int countMEDIA();

   public abstract boolean add(MEDIA aMedia);

   public abstract boolean remove(MEDIA aMedia);

   public abstract ListIterator createListIterator();

   public void setComposite(MEDIA aComposite) {composite = aComposite;}

   public MEDIA getComposite() {return composite;}

   public void setContent(String aName) {name = aName;}

   public String getContent() {return name;}

}

 

 

VIDEO.java

 

public class VIDEO extends MEDIA

{

    public VIDEO(String aVideo) {this.setContent(aVideo);}

    public int countMEDIA() {return 1;}

    public boolean add(MEDIA aMedia) {return false;}

    public boolean remove(MEDIA aMedia) {return false;}

    public ListIterator createListIterator() {return null;}

}

 


MEDIA_LIST

 

public class MEDIA_LIST extends MEDIA

{

   public MEDIA_LIST(String aMediaList)

   {

       mediaList = new LinkedList();

       this.setContent(aMediaList);

   }

 

   public int countMEDIA()

   {

       int totalMEDIA = 0;

       ListIterator listIterator = this.createListIterator();

       MEDIA tempMEDIA;

       while (listIterator.hasNext())

       {

           tempMEDIA = (MEDIA)listIterator.next();

           totalMEDIA = totalMEDIA + tempMEDIA.countMEDIA();

       }

       return totalMEDIA;

   }

 

   public boolean add(MEDIA aMEDIA)

   {

       aMEDIA.setComposite(this);

       return mediaList.add(aMEDIA);

   }

 

   public boolean remove(MEDIA aMEDIA)

   {

       ListIterator listIterator = this.createListIterator();

       MEDIA tempMEDIA;

       while (listIterator.hasNext())

       {

           tempMEDIA = (MEDIA)listIterator.next();

           if (tempMEDIA == aMEDIA)

           {

               listIterator.remove();

               return true;

           }

       }

       return false;

   }

 

   public ListIterator createListIterator()

   {

       ListIterator listIterator = mediaList.listIterator();

       return listIterator;

   }

}

 

Figure 2b. Java implementations of MEDIA, VIDEO and MEDIA_LIST

 

The process of demonstrating key concepts and techniques of composite pattern involves a number of learning activities which are documented in Table 2.

 

Problems

Proposed solutions by students

Concepts learned and reflections

What is the best way to define class TEXT, AUDIO and VIDEO?

TEXT, AUDIO and VIDEO are similar because they are classified as learning media. Generalization can be applied.

A base class called MEDIA is defined. TEXT, AUDIO and VIDEO become subclasses of MEDIA. But the structure does not show that a MEDIA object may contain a list of MEDIA objects.

What is the best way to represent a MEDIA object may contain a list of MEDIA objects.

A list of MEDIA objects is best represented using aggregation.

MEDIA_LIST, which aggregates MEDIA, is defined. Is there a more efficient way to define MEDIA_LIST instead of creating it from scratch?

What is the best way to define MEDIA_LIST? 

Reuse whatever we have defined. MEDIA_LIST can be defined as a subclass of MEDIA. This is more efficient because it enables operations to be inherited and to be overridden with the same signature.

Students learned that they can actually combine inheritance and aggregation in class design. In MEDIA_LIST, they can override an operation to invoke the relevant operation from MEDIA using a simple loop construct.

 

Table 2. Learning Activities of Composite Pattern

 

 

3. Course Planning

 

The following implementation plan is adopted in the first year Java programming course curriculum and can be used as a reference model when delivering a similar course. The major topics are covered in the textbook [7]. The two design patterns covered in the lectures and labs are singleton and composite. Students are required to explore in their final project a selected design pattern.

 

Week

Topics

Selected Design Patterns

1

Primitive Types, Strings, and Console

 

2

Flow of Control

 

3

Defining Classes

 

4

Objects and Methods

 

5

 

Singleton (lecture and lab)

6

Array

 

7

Inheritance

 

8

 

Composite (lecture and lab)

9

Exception Handling

 

10

Streams and File I/O

 

11

Dynamic Data Structure

 

12

Recursion

 

13

Window Interfaces Using Swing

 

14

 

Explore a selected pattern (student project)

 

Table 3. Course Plan: Introduction to Object-Oriented Programming

 

 

4. Conclusion

 

A new approach of teaching first year object-oriented programming has been presented and discussed in the paper. Such an approach has incorporated the constructivist instructional strategy. Design patterns have been covered in the course to fulfill the requirements of the strategy. The approach is found pedagogically important that maintains student interest and motivation. Many people argue that design patterns are too advanced for first year computer science students and should be covered only in the third or fourth year of study. Others argue that the concepts should be covered when students begin to learn object-oriented programming [8]. Undoubtedly, computer science students are better motivated if real-world, best practice skills are presented and demonstrated. Such an approach is consistent with what constructivists have proposed. One major reason why design patterns are covered in senior year courses traditionally is that examples used by most references are too complex for first year students. To be able to introduce design patterns to first year students, the examples presented in the class must be designed in such a way that they are appropriate, interesting, engaging, and “owned” by students. The ownership of the problems has been emphasized by constructivists and educators when designing an active learning environment [9].

 

 

References

  1. David Jonassen. “Designing Constructivist Learning Environments”, Instructional Design Theories and Models, 1999.
  2. E. Gamma, R. Helm, R. Johnson,  and J. Vlissides. “Design Patterns”,  Addison Wesley, 1995.
  3. Kwantlen University College. “CPSC1204 course outline: Introduction to Computer Programming II”, 1999.
  4. G. Booch, J. Rumbaugh and I. Jacobson. “The Unified Modeling Language User Guide”, Addison-Wesley, 1999.
  5. J. Lave. “The Practice of Learning in Understanding Practice: Perspectives on Activity and Context”, Cambridge University Press, 1993.
  6. Carl Andersen, David Traum, K. Purang, Darsana Purushothaman, Don Perlis. “Mixed Initiative Dialogue and Intelligence via Active Logic”, University of Maryland, 1999.
  7. Walter Savitch. “Java : An Introduction to Computer Science & Programming”, Pearson Education Inc, 2004.
  8. Alan Shalloway and James R. Trott, “Design Patterns Explained”, Addison Wesley, 2002.
  9. Andy Law. “Designing Learning Environment with Initiatives”, Presented at the open House, Simon Fraser University, 2003.

 

 

Andy C. H. Law
Department of Computing Sciences and
Information Systems
Kwantlen University College

12666
-72nd Ave., Surrey, B.C., Canada V3W 2M8
Andy.Law@kwantlen.ca