Top Banner
CS342: Software Design Oct 9, 2017
27

CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

May 27, 2018

Download

Documents

lyliem
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

CS342: Software Design Oct 9, 2017

Page 2: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

Facade pattern

Observer pattern

Outline

Page 3: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

Human player replaces a card1. Main <--> UserPlayer2. Main ->CardPile 3. CardPile -> Card -> Main4. Main -> Card -> UserPlayer

Homework 1 classes

Game

ComputerPlayer UserPlayer

Card

CardPile

12

3

3

4

Page 4: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

Player Base class: common fields and methods for user and computer- Add a card to hand- Discard a card- Has flus, has four suit, has etc- Calculate rank- Cal # of cards u can discard- Get hand

Project 1 class redesigned

Five CardDraw (Main): UI and orchestration- Prompt # of players- Trigger dealing- User's turn- AI's turn- Display results

Game Session: main logic and workflow- Set up pile- Instantiate Players- Deal to Players- Players replace cards- Decide results and winners

Com

pute

r Pla

yer

- AI f

unct

ion

Use

r Pl

ayer

- Int

erac

tive

sele

ctio

n

Card Pile- Shuffle- Deal a card

Car

d- N

umbe

r, su

it, s

tring

Page 5: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

Home theater components

Page 6: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

To watch a movie, you need to...

Page 7: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

Treat components as a subsystem

● Orchestrates functions of

components to perform a higher

level task

● Provide a simplified interface to

client.

● Integrate, not encapsulate:

subsystem still available to client

● Decouples client from

componentts

Introducing HomeTheaterFacade

Page 8: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

HomeTheaterFacade classes

Page 9: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

Provides a unified

interface to a set of

interfaces in a subsystem.

● Avoid tight coupling

between client and

subsystem

● Better structured and

easier to read code

Facade pattern defined

Page 10: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

Guideline: an object’s method only calls

methods that belong to

● The object itself

● Objects passed in as a parameter

● Any objects the method creates

● Any components of the object

Implying don’t call methods of an object

returned by calling other methods

● Otherwise, we increase the number of

objects that the class direct knows

Talk only to your immediate

friends..

● Reduce interactions

between objects

● If lots of objects have

dependencies with each

other, it’s hard to make

changes, and systems

becomes fragile

The Principle of Least Knowledge

Page 11: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

Do’s and Dont’s

Page 12: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

Facade and Principle of least knowldge

Page 13: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

Weather monitoring application

Page 14: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

Weather monitoring application

Page 15: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

Coding to concrete implementation

● When we need to add, remove, modify display features, will need to change client

● Display objects have the same method signature, but different behaviors (sounds

familiar?)

● Encapsulate them -- but how?

● Observation pattern

Issues with the implementation

Page 16: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

One to many dependency

between objects

● When source/subject

/publisher state changes,

all dependents/observer

/subscribers are notified

and updated

automatically

● There are many different

ways to implement

Observer pattern: publish & subscribe

Page 17: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

Class diagramLoosely coupled objects

● Subjects and observers

barely know each other

● A subject class only

implements the

interface, it doesn’t know

or care who subscribe to

it

● Observer can be added

any time

● Reuse subjects and

observers

● Changing either doesn’t

affect the other

Page 18: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

Weather station class design

Page 19: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

Weather station code

Page 20: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

Observer 1: current condition display

Page 21: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

Main function (client)

Page 22: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

We have to repeat some code

across all classes

implementing “Subject”

● registerObserver,

removeObserver,

notifyObserver

● Can we create some

framework so we don’t

copy and paste them?

I’m still not 100% happy with my code...

Page 23: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

Java’s built-in observer pattern

Page 24: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

Built-in vs. home-brew code for subject

Page 25: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

Observer using built-in support

Page 26: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

Built-in: no guarantee order of observer notification

Is it too much to ask the order to be consistent?

Page 27: CS342: Software Design - University of Illinois at Chicago · Weather monitoring application. ... Class diagram Loosely coupled objects ... Implements a communication system

Implements a communication system

between mutually interacting software

applications in a service-oriented

architecture (SOA)

● Distributed computing

● A service does something and

publishes an event on the bus

● Subscribing services picks up the

event and take actions

● Typical Observer pattern

● Agile, loosely couple services

Enterprise Service Bus (ESB)