COMP2511 Tute05

Agenda

  • Admin Stuff
  • Law of Demeter
  • Our first design patterns!
    • Composite Pattern
    • Factory Pattern

Admin Stuff

  • Assignment 1 is due
    • Get started if you haven't already
    • You can run dryrun tests on GitLab by using the optional trigger pipelines (refer to spec)
  • Get feedback on assignment UML,
    • Feel free to come to me or the LA during the lab

Observer Pattern

Observer Pattern

What is it?

Behavioural pattern which defines a subscription mechanism that notifies observers about any event that happens to the subject that they are observing

 

What problem does it solve?

Modelling a n-to-many relationship may be difficult. If
not done well, it typically leads to tight coupling

 

When should we use this pattern?

When changing the state of one object requires changing other objects in turn

Observer Pattern

Subject

Maintains a list of observers and notifies them of state changes. During this notification, they are passing data (push/pull)

public void registerObserver(Observer o);
public void removeObserver(Observer o);
public void notifyObservers(data);	// Calls update() of observers

Observer

Register (and unregister) themselves on a subject and to update their state when they are notified.

// Two possible options for update()
public void update(Subject obj);	// PULL data from subject
public void update(data);	// PUSH data to observers

Observer Pattern

Model the system in Java!

Structure

Composite Pattern

Composite Pattern

What is it?

Structural design pattern that allow objects to be composed into a tree structure such that individual objects and composites of such objects can be treated in the same way.

 

When to use?

Model a group of classes that forms a hierarchy

Composite Pattern

In Unix based operating systems, directories are just files that can contain other files

Composite Pattern

Leaf Nodes

Composite Nodes

  • A composite stores a collection of children components (either Leaf and/or composite)
  • Performs operations on its children 
  • Single part objects, are always located at the leaves of the tree
  • Performs operations directly on the object

Composite Pattern

UML Diagram

 

  • Leaf and Composite both implement the same interface
  • Instances of Leaf and Composite can be treated as the same
  • A Composite can hold more than one Leaf
  • Produces a tree-like structure that leans itself very well towards recursion

Composite Pattern

Use the composite pattern to design a simple calculator that can be used to evaluate an arithmetic expression.

 

Your calculator should be able to:

  • Add two expressions
  • Subtract two expressions
  • Multiply two expressions
  • Divide two expressions

Composite Pattern

Tree representation

exp1 = 42 

exp2 = ((1 + 2) - ((3 / 4) * (5))


 

 

Composite Pattern

UML Diagram

composite nodes

leaf node

Factory Pattern

Factory Pattern

Why use it?

  • Reduce coupling between client code (creation logic) and the specific classes being instantiated
  • Single responsibility principle: construction of every class in the family is handled at the same place
  • Open-Closed principle: introducing new classes into the family simply requires a new factory subclass 

What is it?

Creational design pattern that lets you define an interface for creating objects of a family in a superclass but allows subclasses to alter the type of object that will be created.

Factory Pattern

Imagine we are developing a game with different characters!

public class Game {
	public static void main(String[] args) {
        Game game = new Game();
        game.addCharacter(new King(0, 0));
        game.addCharacter(new Dragon(0, 1));
        game.addCharacter(new Queen(2, 2));
        game.play();
    }
}

Factory Pattern

Factory Pattern

Refactor the code such that when the characters are created, they are put in a random location in a grid of length of length 5.

 

Here, because our number of characters that are needed to be created is relatively small, we can place all in a single CharactorFactory class.

Factory Pattern

  • Abstract the construction of the character objects - this means we are not dealing directly with a constructor.
  • Especially, we abstract the generating of random numbers to the factory and simply call CharacterFactory.createKing etc, which makes our code more clean and less likely for Game to cause problems with creating the objects itself.

How does the Factory Pattern allow us to abstract construction of objects, and how will it improve our design with this new requirement?

There is another version of the Factory pattern where we create a factory for each concrete product.

EntityFactory

KingFactory

QueenFactory

Character

King

Queen

  • PRO: Avoids violation of Open-Closed principle
  • CON: For many products, have to create many factories/files

Abstract Factory Pattern

Abstract Factory Pattern

When to use it?

  • When code needs to work with variants of each family of products
  • Many designs start by using the Factory Pattern and evolve towards Abstract Factory

What is it?

Creational design pattern that lets you produce families of related objected without specifying concrete classes. 

Abstract Factory Pattern

Say we decide to further extend our game, so that we have several variants.

For example, products King + Queen + Knight + Dragon are available in different variants:

KingdomA, KingdomB

Abstract Factory Pattern

The abstract factory is the interface with all the creation methods in the product family. 

For each family variant, we create a separate factory class

Abstract Factory Pattern

KingdomACharacterFactory

KingdomBCharacterFactory

CharacterFactory

KingdomAKing

KingdomBKing

King

COMP2511 Tute04

By Chirag Sawlani

COMP2511 Tute04

  • 174