CS342: Software Design. November 21, 2017

Similar documents
The Strategy Pattern Design Principle: Design Principle: Design Principle:

CS342: Software Design. Oct 9, 2017

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

SDC Design patterns GoF

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Design patterns. Jef De Smedt Beta VZW

Design Patterns. Dr. Rania Khairy. Software Engineering and Development Tool

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

Threads Questions Important Questions

Socket attaches to a Ratchet. 2) Bridge Decouple an abstraction from its implementation so that the two can vary independently.

Topics in Object-Oriented Design Patterns

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

The Adapter Pattern. Interface with anything!

Keywords: Abstract Factory, Singleton, Factory Method, Prototype, Builder, Composite, Flyweight, Decorator.

Using Design Patterns in Java Application Development

Design Patterns. SE3A04 Tutorial. Jason Jaskolka

Object-Oriented Oriented Programming

CSE 70 Final Exam Fall 2009

Object Oriented Paradigm

Design Patterns. (and anti-patterns)

Object-oriented Software Design Patterns

Last Lecture. Lecture 26: Design Patterns (part 2) State. Goals of Lecture. Design Patterns

CS 370 Design Heuristics D R. M I C H A E L J. R E A L E F A L L

CS 351 Design of Large Programs Singleton Pattern

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

DESIGN PATTERN - INTERVIEW QUESTIONS

Object-Oriented Design

Design Patterns Reid Holmes

An Introduction to Patterns

Design Patterns Design patterns advantages:

MSO Object Creation Singleton & Object Pool

Design Patterns (Part 2) CSCE Lecture 18-10/25/2016 (Partially adapted from Head First Design Patterns by Freeman, Bates, Sierra, and Robson)

be used for more than one use case (for instance, for use cases Create User and Delete User, one can have one UserController, instead of two separate

Design Patterns #3. Reid Holmes. Material and some slide content from: - GoF Design Patterns Book - Head First Design Patterns

Object-Oriented Design

An Introduction to Patterns

Composite Pattern. IV.4 Structural Pattern

MSO Lecture 12. Wouter Swierstra (adapted by HP) October 26, 2017

Brief Note on Design Pattern

Java Threads. COMP 585 Noteset #2 1

Object-Oriented Concepts and Design Principles

From Design Patterns: Elements of Reusable Object Oriented Software. Read the sections corresponding to patterns covered in the following slides.

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Software Design COSC 4353/6353 D R. R A J S I N G H

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City

Applying Design Patterns to accelerate development of reusable, configurable and portable UVCs. Accellera Systems Initiative 1

CS342: Software Design. Oct 3, 2017

Last Lecture. Lecture 17: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 4448/ Spring Semester, 2005

Object-Oriented Design

The Design Patterns Matrix From Analysis to Implementation

Tuesday, October 4. Announcements

OODP Session 4. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

Object Oriented Programming (II-Year CSE II-Sem-R09)

Software Design COSC 4353/6353 D R. R A J S I N G H

1 Software Architecture

Performance Throughput Utilization of system resources

CS 556 Distributed Systems

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

CSE 219 COMPUTER SCIENCE III STRUCTURAL DESIGN PATTERNS SLIDES COURTESY: RICHARD MCKENNA, STONY BROOK UNIVERSITY.

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

Software Architecture With ColdFusion: Design Patterns and Beyond Topics Outline Prepared by Simon Horwith for CFUnderground 6

Design Patterns: Structural and Behavioural

Software Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.

Introduction to Software Engineering: Object Design I Reuse & Patterns

Software Engineering

Lecture 20: Design Patterns II

DESIGN PATTERNS FOR MERE MORTALS

Application Architectures, Design Patterns

Adapter pattern. Acknowledgement: Freeman & Freeman

Design Pattern- Creational pattern 2015

What is Design Patterns?

05. SINGLETON PATTERN. One of a Kind Objects

Slide 1. Design Patterns. Prof. Mirco Tribastone, Ph.D

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

INTERNAL ASSESSMENT TEST III Answer Schema

Multitasking. Multitasking allows several activities to occur concurrently on the computer Levels of multitasking: Process based multitasking

Taming Dava Threads. Apress ALLEN HOLUB. HLuHB Darmstadt

SOFTWARE PATTERNS. Joseph Bonello

Design of Software Systems (Ontwerp van SoftwareSystemen) Design Patterns Reference. Roel Wuyts

Animation Part 2: MoveableShape interface & Multithreading

Applying the Factory Method Design Pattern

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

SOLID DESIGN PATTERNS FOR MERE MORTALS

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

Design Patterns 2. Page 1. Software Requirements and Design CITS 4401 Lecture 10. Proxy Pattern: Motivation. Proxy Pattern.

CIS233J Java Programming II. Threads

Summary of the course lectures

Dr. Xiaolin Hu. Review of last class

Software Engineering Prof. Rushikesh K.Joshi IIT Bombay Lecture-15 Design Patterns

MultiThreading. Object Orientated Programming in Java. Benjamin Kenwright

Java Threads. Written by John Bell for CS 342, Spring 2018

Design Patterns. Gunnar Gotshalks A4-1

APPLYING DESIGN PATTERNS TO SCA IMPLEMENTATIONS

3 Product Management Anti-Patterns by Thomas Schranz

CS11 Java. Fall Lecture 7

Writing your own Java I/O Decorator p. 102 Tools for your Design Toolbox p. 105 Exercise Solutions p. 106 The Factory Pattern Baking with OO

Transcription:

CS342: Software Design November 21, 2017

Runnable interface: create threading object Thread is a flow of control within a program Thread vs. process All execution in Java is associated with a Thread object. Main thread Thread begins by run() method of implementation of Runnable

How does this work?

Create a thread by inherit Thread class This is so convenient, right?

Why do we favor implementing Runnable interface? By subclassing Thread, the new object is an instance of Thread It getting heavy and dirty Exposing all public properties and function I want my class to have ability to thread, not special case of a thread Implement vs. Inherit Example: I want to be able to program with Java..

Thread methods and coordination Deprecated methods resume, stop, suspend Unexpected behavior, deadlock sleep() method: make thread to sit idle for specified time interrupt(): wakes up a thread join(): block the caller until thread completes. yield(), wait(), notify(), notifyall()

Thread scheduling - round-robin Scheduler picks up next thread when Sleeps, by calling Thread.sleep( ) or wait( ) Waits for a lock, in order to run a synchronized method Blocks on I/O, for example, in a read( ) or accept( ) call Explicitly yields control, by calling yield( ) Terminates, by completing its target method or with a stop( ) call (deprecated)

Thread scheduling - time slicing Thread processing is chopped up so that each thread runs for a short period

Encapsulation Take the parts that vary and encapsulate them, so that later you can alter or extend the parts that vary without affecting those that don t Separating what changes from what stays the same Program to an interface, not to an implementation

Has-a vs. Is-a

Strategy pattern Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

Facade pattern defined 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

Home theater components

HomeTheaterFacade 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 components

The Principle of Least Knowledge 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 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

Observer pattern: publish & subscribe 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

Class diagram Loosely 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

Factory pattern definition The Factory Method Pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses

Factory (creator) classes

Product class

Parallel class hierarchy

The Dependency Inversion Principle Depend upon abstractions. Do not depend upon concrete classes. Further step beyond Program to an interface, not to an implementation High-level components should not depend on low-level components Both high level and low level components should depend on abstractions Both PizzaStore and concrete pizza classes depend on Pizza abstraction

The Open-Closed Principle Classes should be open for extension, but closed for modification Allow classes to be easily extended to incorporate new behavior without modifying existing code Make it resilient to change and flexible to take new functionality to meeting changing requirements There are multiple OOP techniques to implement the principle. Observer pattern: add/remove observers

Decorator pattern The Decorator Pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality

Decorators Decorators have the same supertype as the objects they decorate. You can use one or more decorators to wrap an object. Given that the decorator has the same supertype as the object it decorates, we can pass around a decorated object in place of the original (wrapped) object. The decorator adds its own behavior either before and/or after delegating to the object it decorates to do the rest of the job. Objects can be decorated at any time, so we can decorate objects dynamically at runtime with as many decorators as we like.

Singleton pattern: Create one-of-a-kind objects There is only one instance of the class Ensures a class has only one instance, and provides a global point of access to it. Create a class and letting it manage a single instance of itself, preventing any other class from creating a new instance on its own Whenever you need an instance, just query the class and it will hand you back the single instance Usage: thread pools, caches, dialog boxes, objects that handle preferences and registry settings, objects used for logging, and objects that act as device drivers to devices like printers and graphics cards

Singleton benefits and risk There is only one instance of the class Better resource usage: you don t have to create a new object when you need it and then destroy it after you are done Performance gain Commonly used in SOA: instantiate a service One of a kind means clients may fight to use it! Thread-safe?

How to make singleton thread-safe? 1. Use synchronized modifier -- expensive, decrease performance 100x. Use it only when getinstance() isn t critical 2. Eagerly create instance. 3. Double check locking: First check to see if an instance is created, and if not, THEN use synchronizes (see next page).

Double check locking volatile ensures multiple threads handle the uniqueinstance variable correctly when it is being initialized to the Singleton instance.

Iterator pattern Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. Allows traversal of the elements of an aggregate without exposing the underlying implementation Places the task of traversal on the iterator object, not on the aggregate, which simplifies the aggregate interface and implementation, and places the responsibility where it should be

Single responsibility principle A class should have only one reason to change When design class, assign it with one responsibility and one only A class should only do one thing and do it well Cohesion: a measure of how closely a class or a module supports a single purpose or responsibility How closely methods/functions of a class are related High cohesion class == adhering to single responsibility principle Single responsibility principle: intra-class structure; Least knowledge : inter-class High cohesion, low coupling == good design