S/W Design and Architecture

Observer 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 observer and a subdirectory src within it. Inside the src folder create two files called: twitter.rb and twitter_test.rb.

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

    # Observer Pattern
    # Date: 02-Mar-2020
    # Authors:
    #          A00456654 Thursday Rubinstein 
    #          A01160611 Anthony Stark
    
  2. In the twitter.rb file write a class called Twitter that implements the Observer pattern. An instance of this class should be able to behave as a subject (observable object) and as an observer at the same time. Check the following test class to see the expected interface that must be supported by the Twitter class:

    # File name: twitter_test.rb
    
    require 'minitest/autorun'
    require 'stringio'
    require 'twitter'
    
    class TwitterTest < Minitest::Test
    
      def setup
        @out = StringIO.new
        @old_stdout = $stdout
        $stdout = @out
      end
    
      def teardown
        $stdout = @old_stdout
      end
    
      def test_twitter_alices_adventures_in_wonderland
        a = Twitter.new('Alice')
        k = Twitter.new('King')
        q = Twitter.new('Queen')
        h = Twitter.new('Mad Hatter')
        c = Twitter.new('Cheshire Cat')
    
        a.follow(c)
        k.follow(q)
        h.follow(a).follow(q).follow(c)
        q.follow(q)
    
        a.tweet "What a strange world we live in."
        k.tweet "Begin at the beginning, and go on till you come "  \
          "to the end: then stop."
        q.tweet "Off with their heads!"
        c.tweet "We're all mad here."
    
        assert_equal \
          "Mad Hatter received a tweet from Alice: What a strange " \
            "world we live in.\n"                                   \
          "King received a tweet from Queen: Off with their "       \
            "heads!\n"                                              \
          "Mad Hatter received a tweet from Queen: Off with their " \
            "heads!\n"                                              \
          "Queen received a tweet from Queen: Off with their "      \
            "heads!\n"                                              \
          "Alice received a tweet from Cheshire Cat: We're all "    \
            "mad here.\n"                                           \
          "Mad Hatter received a tweet from Cheshire Cat: "         \
            "We're all mad here.\n",                                \
          @out.string
      end
    
      def test_twitter_star_wars
        y = Twitter.new('Yoda')
        o = Twitter.new('Obi-Wan Kenobi')
        v = Twitter.new('Darth Vader')
        p = Twitter.new('Padmé Amidala')
    
        p.follow(v)
        v.follow(p).follow(y).follow(v)
    
        y.tweet "Do or do not. There is no try."
        o.tweet "The Force will be with you, always."
        v.tweet "I find your lack of faith disturbing."
        o.tweet "In my experience, there's no such thing as luck."
        y.tweet "Truly wonderful, the mind of a child is."
        p.tweet "I will not condone a course of action that will "  \
          "lead us to war."
    
        assert_equal \
          "Darth Vader received a tweet from Yoda: Do or do not. "  \
            "There is no try.\n"                                    \
          "Padmé Amidala received a tweet from Darth Vader: I find "\
            "your lack of faith disturbing.\n"                      \
          "Darth Vader received a tweet from Darth Vader: I find "  \
            "your lack of faith disturbing.\n"                      \
          "Darth Vader received a tweet from Yoda: Truly wonderful,"\
            " the mind of a child is.\n"                            \
          "Darth Vader received a tweet from Padmé Amidala: I will "\
            "not condone a course of action that will lead us to "  \
            "war.\n",                                               \
          @out.string
      end
    
    end
    
  3. 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 observer directory (including the generated HTML documentation in the doc subdirectory). Call this file observer.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 observer folder resides):

tar czf observer.tgz observer

Upload Instructions

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

Request PIN

Only one team member needs to upload the file.

Due date is Monday, March 2.

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.