S/W Design and Architecture

Adapter Pattern

Objectives

During this activity:

This activity helps students develop the following skills, values and attitudes: ability to analyze and synthesize, capacity for identifying and solving problems, and efficient use of computer systems.


Activity Description

This activity can be developed individually or in pairs.

  1. Create a directory called adapter and a subdirectory src within it. Inside the src folder create three files called: simple_queue.rb, queue_adapter.rb, and adapter_test.rb.

    All three Ruby source files must start with a comment containing title, date, and the authors' personal information. For example:

    # Adapter Pattern
    # Date: 11-Mar-2019
    # Authors:
    #          A00456654 Thursday Rubinstein 
    #          A01160611 Anthony Stark
    
  2. The content of the file simple_queue.rb is as follows:

    # File: simple_queue.rb
    
    # IMPORTANT: Do not modify the following class in any way!
    
    class SimpleQueue
    
      def initialize
        @info =[]
      end
    
      def insert(x)
        @info.push(x)
        self
      end
    
      def remove
        if empty?
          raise "Can't remove if queue is empty"
        else
          @info.shift
        end
      end
    
      def empty?
        @info.empty?
      end
    
      def size
        @info.size
      end
    
      def inspect
        @info.inspect
      end
    
    end
    

    The SimpleQueue class contains a First-In First-Out (FIFO) data structure that implements the following methods:

    Method Signature Description
    insert(x) Inserts x at the back of this queue. Returns this queue.
    remove Removes and returns the element at the front of this queue. Raises an exception if this queue happens to be empty.
    empty? Returns true if this queue is empty, otherwise returns false.
    size Returns the number of elements currently stored in this queue.

    Write an adapter class called QueueAdapter (place it in the file called queue_adapter.rb) that makes a SimpleQueue instance (as implemented above) behave like a Last-In First-Out (LIFO) stack with the following interface:

    Method Signature Description
    initialize(q) Initializes a new stack, using q as the adaptee.
    push(x) Inserts x at the top of this stack. Returns this stack.
    pop Returns nil if this stack is empty, otherwise removes and returns its top element.
    peek Returns nil if this stack is empty, otherwise returns its top element without removing it.
    empty? Returns true if this stack is empty, otherwise returns false.
    size Returns the number of elements currently stored in this stack.

  3. The following unit test verifies the correct behavior of the QueueAdapter class. Place the test class in the adapter_test.rb source file.
    # File: adapter_test.rb 
    
    require 'minitest/autorun'
    require 'simple_queue'
    require 'queue_adapter'
    
    class QueueAdapterTest < Minitest::Test
    
      def test_queue_adapter
        q = SimpleQueue.new
        qa = QueueAdapter.new(q)
        assert q.empty?
        assert qa.empty?
        assert_nil qa.pop
        assert_same qa, qa.push("Foo")
        assert_equal "Foo", qa.peek
        refute q.empty?
        refute qa.empty?
        assert_same qa, qa.push("Bar")
        assert_equal "Bar", qa.peek
        assert_same qa, qa.push("Baz").push("Quux")
        assert_equal 4, q.size
        assert_equal 4, qa.size
        assert_equal "Quux", qa.peek
        assert_equal "Quux", qa.pop
        assert_equal "Baz", qa.peek
        assert_equal "Baz", qa.pop
        assert_equal "Bar", qa.peek
        assert_equal "Bar", qa.pop
        assert_equal "Foo", qa.peek
        assert_same qa, qa.push("Goo")
        assert_equal "Goo", qa.peek
        assert_equal "Goo", qa.pop
        assert_equal "Foo", qa.peek
        assert_equal 1, qa.size
        assert_equal "Foo", qa.pop
        assert_nil qa.peek
        assert_nil qa.pop
        assert q.empty?
        assert qa.empty?
        assert_equal 0, q.size
        assert_equal 0, qa.size
      end
    
    end
    
  4. Write and generate your program’s documentation as described in: Documenting Ruby Programs.

Deliverables

Create a compressed tarball file with the full contents of the adapter directory (including the generated HTML documentation in the doc subdirectory). Call this file adapter.tgz. From a terminal, you can use the following command to create this file (make sure to run it at the same level where the adapter folder resides):

tar czf adapter.tgz adapter

Upload Instructions

To deliver the adapter.tgz file, please provide the following information:

Request PIN

Only one team member needs to upload the file.

Due date is Monday, March 11.

Evaluation

This activity will be evaluated using the following criteria:

50% Implementation of program requirements.
50% Documentation.
1 The program and/or documentation was plagiarized.