csc 211 introduction to design patterns. intro to the course syllabus about the textbook – read...

24
CSC 211 Introduction to Design Patterns

Upload: violet-johnston

Post on 31-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the

CSC 211

Introduction to Design Patterns

Page 2: CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the

Intro to the course

• Syllabus

• About the textbook– Read the introduction and Chapter 1

• Good attendance is the key to an easy ride

Page 3: CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the

Background

• CS1 - Programming and Control Structures• CS2 - Data Structures - common patterns for

storing and retrieving data

• CSC211 - Design Patterns - common architectural patterns– Broader, more sophisticated view

Page 4: CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the

A little about design patterns• This course is an introduction to software design patterns.

• Each pattern represents a best practice solution to a software problem in some context.

• Design patterns are just convenient ways of reusing object oriented code between projects and programmers.

• Write down and catalog common interactions between

objects that programmers have frequently found useful.

Page 5: CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the

What’s a design pattern?

• Model, view, controller (MVC) pattern in Java Swing

• In other words, design patterns describe how objects communicate without become entangled in each other’s data models and methods.

• Keeping this separation has always been an objective of good OO programming

Page 6: CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the

OOP – Review of Basics

• Abstraction• Class• Object• Instance

Page 7: CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the

Inheritance

• When one class is defined to include methods and fields from another class

• Subclass vs. Superclass–Child vs. parent

• How do we code that in Java?

Page 8: CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the

Substitution Principle

• Let q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where S is a subtype of T

• In other words, if S is a subtype of T, then objects of type T in a program may be replaced with objects of type S without altering any of the desirable properties of that program

Page 9: CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the

Substitution Principle’s Effects

• Preconditions cannot be strengthened in a subclass.• Remember method preconditions?

• Postconditions cannot be weakened in a subclass.

• No new exceptions should be thrown by subclass methods – except where exceptions are subtypes of superclass

exceptions

Page 10: CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the

Abstract Class

• A class that cannot be instantiated• Why would we do that?

Page 11: CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the

Interface

• Defines a set of method signatures• A class that implements the interface

promises it will implement signature methods• Response to Java not allowing inheritance

from multiple superclasses• Essentially allows a subclass to be substituted

for multiple superclasses. – However only one of those superclasses can

contain more than method signatures.

Page 12: CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the

Interface and abstract class• Java has a class like form called an interface that can be used to encapsulate only

abstract methods and constants.• Think of an interface as a blueprint or a design specification.• A class that uses this blueprint is a class that implements the interface.• Another way of thinking of an interface is as a pure abstract class, wherein all the

details are deferred.• An interface is allowed to have only abstract methods and constants. • Interface methods can’t be static.• An interface is allowed to have only public methods, and by convention the

keyword public is omitted.• Because all methods in an interface are abstract, the keyword abstract is also

omitted.• A new class implementing the interface uses the keyword implements.• The class that implements the interface must provide an implementation for all

methods in the interface.• The difference between an interface and an abstract class becomes very important

when you want to create a class that implements more than one interface.• You can’t extend more than one class, but you can use a class to implement many

interfaces, in addition to extending one class.

Page 13: CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the

Type Polymorphism

• Declaring an instance of a subclass to be of a type of its superclass or an interface its class implements

• Ex.Purrer kitty = new HouseCat();

Page 14: CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the

Is-A versus Has-A

• A sub-class has an “Is-A” relationship with its superclass

• A class has an “Is-A” relationship with any interface it implements

• A class has a “Has-A” relationship with any class that is the type of one of its instance variables

Page 15: CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the

What are design patterns?

• Common ways that classes are structured to solve similar types of problems

• Frameworks for classes, objects, and interfaces that can be applied to a wide variety of situations

• Thinking about the design of the system at a higher level

• A solution to a problem in a context -- (textbook def.)

Page 16: CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the

Design Patterns Give us Vocabulary

• Shared vocabulary that we use to discuss design alternatives

• Data structures began that vocabulary at a lower level

• That vocabulary lets us describe alternatives efficiently

Page 17: CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the

What design patterns aren’t

• They aren’t data structures– Data structures can be implemented once and

shared between many applications– Even if two applications use the same patterns,

each will have to implement the details for their own problems.

• They aren’t libraries or APIs

Page 18: CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the

Design Patterns and Thought

• Design Patterns are ways of thinking about potential solutions to complex problems

• They obey good OO design principles and have been shown to be widely applicable

• We need to be able to think at a higher level

Page 19: CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the

Our interest

• What makes a design “good”?

• What design principles support good designs?

• Proven design techniques

Page 20: CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the

Strategy for Building Big Systems

• How do we know what to build?

• How will we know that it works?

• How will we deal with changes that come up as we learn more about the requirements and the system?

Page 21: CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the

Test Driven Development

• Write automated tests before writing the code

• Iterative Development:– Red - write a test and watch it fail– Green - write the simplest code that can make the

test pass– Refactor - clean up the code to eliminate

duplication

Page 22: CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the

Why do we write the test first?

• Help us plan the code we’re going to write• Help us focus on how people will use our code

(not just what it has to do)

Page 23: CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the

How do the tests help?

• We know the system works• We can refactor with confidence• Serve as design documentation– Javadocs document structure– Tests document behavior– These work well together

Page 24: CSC 211 Introduction to Design Patterns. Intro to the course Syllabus About the textbook – Read the introduction and Chapter 1 Good attendance is the

Homework

• For lab (We’ll do a lab to practice TDD)– Read about JUnit testing framework at www.junit.org

• For next week– Read book from beginning through the Strategy Pattern

• In general– Quizzes on Tuesday– Cover a pattern Starting on Tuesday with a lab later in the

week– Read about the pattern BEFORE you come to class on

Tuesday (it’s on the quiz!)