DS6: Design Patterns Design Studio
10 points
November 4, 2016


This design studio will help practice implementing the abstract factory and observer patterns.

1. Preparatory tasks

  1. Read the slides on Design Patterns, in particular, the abstract factory and observer patterns.

  2. Read the additional reading material posted on the progress page.

  3. Take the Canvas quiz PreQ6.

2. In-class tasks

You will work on the following tasks as a team.

  1. Download the zip file available here

  2. Unzip the files. You should find two folders, AbstractFactory and Observer.

  3. Load these folders as two separate projects in Eclipse.

  4. Work on the Observer example first.

    1. Run the Observer example using Watch2Windows as the main class:

      • One observer window lists all the selected colors in order.

      • The other observer window displays the currently selected color and changes the background color accordingly.

    2. Understand the Observer example.

      The ColorFrame and ListFrame classes implement the Observer interface. In these classes, the constructor ensures that each observer gets registered with the subject state is the sendNotify method. This is like the update method we discussed in the slides.

      The Watch2Windows class implements the Subject interface. It maintains the state that gets selected using Java Swing buttons. You don't need to understand this part of the code now. Just note that the Watch2Windows class implements the registerInterest method, which is used by observers to register themselves with the subject. The Watch2Windows class also contains the main method.

    3. [5 points] Extend the Observer example.

      Add a new type of observer window. Make it filter events, so that it changes every second click on the main window. It should not change after every click. The window displays one text field, which lists the time of clicking (HH:MM:SS), using the current selected color. The implementation can be done by by copy-pasting either ColorFrame or ListFrame.

    4. Show one of the instructors or TAs that the observer program works.

  5. Work on the Abstract Factory Example next.
    1. Run the AbstractFactory example using Gardener as the main class.

    2. Understand the AbstractFactory example.

      The Garden interface is the supertype in the Abstract Factory hierarchy. note that every garden must contain a plant that grows in the shade, one that grows in the center, and one that grows in the border.

      The AnnualGarden, PerennialGarden, and VeggieGarden classes are concrete factories that implement the Garden interface. The AnnualGarden factory creates a plant that is annual and also grows in the shade, another plant that is annual and also looks nice in the center of the garden, and a third plant that is annual and also looks nice in the border of the garden. Similarly the other factories also produce plants for the shade, center, and border, but have the characteristic that the plant is perennial or is a vegetable.

      The Gardener class contains the main method. It sets up the panel of radio buttons for selecting the factory on the left (e.g., Vegetable, Annual, and Perennial). The panel for selecting the position of the plant using buttons is in the bottom right. The panel showing the actual garden layout is in the top left.

      The Plant class represents a product. For simplicity, there are no subtypes of Plant. Instead the actual plant name is provided as a String argument to the Plant constructor.

      The classes Command and CheckCommand are used to implement the Command design pattern, which you don't really need to know at this point.

    3. [5 points] Implement the abstract factory pattern:

      Add a new type of garden by implementing a new factory that implements Garden. Add a new type of area (e.g., Bright), which means you need to change the interface Garden and all the existing implementations of Garden.

      Choose an appropriate set of plants to go with the gardens and areas. Use your imagination for naming the plans. This isn’t a botany test. Modify the GUI to include the new type of garden and type of area.

      Make sure that the Gardener class shows the new radio button for the new type of garden as well as a new button for the new area.

    4. Show one of the instructors or TAs that the abstract factory program works.

  6. Package the folders for both Abstract Factory and Observer in a zip file.

  7. One person in your team needs to submit the zip file in the Canvas dropbox for DS6.