+ All Categories
Home > Technology > L04 Software Design 2

L04 Software Design 2

Date post: 01-Nov-2014
Category:
Upload: olafur-andri-ragnarsson
View: 86 times
Download: 5 times
Share this document with a friend
Description:
Framhald af umfjöllun um hlutbundna forritun og hönnun. Nú förum við yfir Generic Programming sem sem leið til að búa til sveigjanlega og endurnýtanlegar einingar. Skoðum líka reflection. Þá verður farið fyrir hvernig má hann laustengdar einingar og notum við frægan andarleik sem dæmi. Þá mun Code Horror Dude kíkja í heimsókn
Popular Tags:
54
Lecture 04 Software Design 2
Transcript
Page 1: L04 Software Design 2

Lecture 04Software Design 2

Page 2: L04 Software Design 2

Agenda Programming with Objects– Classes– Interfaces– Generic programming– Reflection

Software Design– Ducks…

Page 3: L04 Software Design 2

Reading Barbin Introduction, 1 Core Principles– Separation of concerns (SoC)– Coupling– Cohesion– Information Hiding

Don’t Repeat Yourself Polymorphism Optional:– http://docs.oracle.com/javase/tutorial/

Page 4: L04 Software Design 2

Generic Programming

Page 5: L04 Software Design 2

Generic Programming Programming in an data type independent

way– Same code is used regardless of the data type

Example– Sort can be applied to any data type– Generic collection

• Java Collection Framework

Design Principle– Always use the most generic data type

possible

Page 6: L04 Software Design 2

Generic Programming All classes extend Object– Allows generic algorithms and data structures

static int find (Object[] a, Object key){ int i; for (i=0;i<a.length;i++) if (a[i].equals(key)) return i; return -1;}

Employee[] staff = new Employee[10];Employee e1 = new Employee("Dilbert");

staff[x] = e1;int n = find(staff, e1);

Page 7: L04 Software Design 2

Generic Programming Generic collections– ArrayList is an example class that uses

Object ArrayList al = new ArrayList(); al.add (new Employee ("Dilbert")); al.add (new Employee ("Wally")); al.add (new Employee ("Alice"));

Iterator i = al.iterator(); Employee e; while (i.hasNext()) { e = (Employee)i.next(); System.out.println(e.getName()); }

DilbertWallyAlice

Page 8: L04 Software Design 2

Generic Programming Generic collections– The Collections class is another exampleList<Employee> list = new ArrayList<Employee>();

list.add (new Employee ("Dilbert"));list.add (new Employee ("Wally"));list.add (new Employee ("Alice"));

Collections.sort(list);for (Employee e: list){ System.out.println(e);}

AliceDilbertWally

Page 9: L04 Software Design 2

Reflection

Page 10: L04 Software Design 2

Reflection Reflection allows examination and manipulation

of objects at runtime– Get information about a class

• Fields, methods, constructors, and super classes• Constants and method declarations belong to an interface

– Create an instance of a class whose name is not known until runtime

– Get and set the value of an object's field, even if the field name is unknown to your program until runtime

– Invoke a method on an object, even if the method is not known until runtime

Page 11: L04 Software Design 2

Reflectionstatic void showMethods(Object o){ Class c = o.getClass(); Method[] theMethods = c.getMethods(); for (int i = 0; i < theMethods.length; i++) { String methodString = theMethods[i].getName(); System.out.println("Name: " + methodString); String returnString = theMethods[i].getReturnType().getName(); System.out.println(" Return Type: " + returnString); Class[] parameterTypes = theMethods[i].getParameterTypes(); System.out.print(" Parameter Types:"); for (int k = 0; k < parameterTypes.length; k ++) { String parameterString = parameterTypes[k].getName(); System.out.print(" " + parameterString); } System.out.println(); } } }

Page 12: L04 Software Design 2

Reflection

Bla

public class ReflectMethods{ public static void main(String[] args) { Polygon p = new Polygon(); showMethods(p); } Name: getBoundingBox

Return Type: java.awt.Rectangle Parameter Types:Name: contains Return Type: boolean Parameter Types: java.awt.geom.Point2D...Name: toString Return Type: java.lang.String Parameter Types:

Page 13: L04 Software Design 2

Reflection Reflection is very useful in frameworks– Infrastructure code– “plumbing” – The “Noise”

Examples– Create Java objects from XML descriptions– Load classes at runtime and invoke methods– Tools and utilities for development

Page 14: L04 Software Design 2

Dynamically Loading Classes Classes can be dynamically loaded at

runtime– Offers the flexibility to decide which class to

run dynamically– Class names can be specified in configuration

files Class class

Class instanceClass = Class.forName("RssFeedReader"); reader = (FeedReader)instanceClass.newInstance();

Page 15: L04 Software Design 2

A) BDB) DBC) BDCD) Compilation fails

QUIZclass Top { public Top(String s) { System.out.print("B"); } }public class Bottom2 extends Top { public Bottom2(String s) { System.out.print("D"); } public static void main(String [] args) { new Bottom2("C"); System.out.println(" "); } }

Page 16: L04 Software Design 2

A) BDB) DBC) BDCD) Compilation fails

QUIZ

class Top { public Top(String s) { System.out.print("B"); } }public class Bottom2 extends Top { public Bottom2(String s) { System.out.print("D"); } public static void main(String [] args) { new Bottom2("C"); System.out.println(" "); } }

Page 17: L04 Software Design 2

Software Design

Page 18: L04 Software Design 2

Object Oriented Design Design and implementation of software

needs to be of quality– Badly designed, well implemented = problem!– Well designed, badly implemented = problem!

CODEHORROR!!

CODE HORROR DUDE

Page 19: L04 Software Design 2

Object Oriented Design Good design

Is based on OO principles

Abstracts complex APIs such as J2EE

Is flexible and can be changed

Contains loosely coupled components

Page 20: L04 Software Design 2
Page 21: L04 Software Design 2

Example from Head First Design Patterns

Page 22: L04 Software Design 2

Getting Started SimUDuck is highly successful duck pond

simulation game Original design

Page 23: L04 Software Design 2

Change Request But now we need the ducks to FLY

Page 24: L04 Software Design 2

Problem! But not all duck fly – We forgot Rubber

Duck!

Page 25: L04 Software Design 2

How can we fix this? Just override fly and quack to do nothing

Page 26: L04 Software Design 2

We even think ahead We fix all non-flyable and non-quackable

ducks as wellCode smell!

Page 27: L04 Software Design 2

Which of the following are disadvantages of using inheritance to provide Duck behavior?

A) Code is duplicated across subclassesB) Runtime behavior changes are difficultC) We can’t make ducks danceD) Hard to gain knowledge of all duck behaviorsE) Ducks can’t fly and quack at the same timeF) Changes can unitentionally affect other ducks

QUIZ

✔✔

Page 28: L04 Software Design 2

The Problem The problem is this– Derived classes (RubberDuck) are forced to

inherit behaviour they don’t have– Derived classes (RubberDuck) needs to be

exposed to the inner workings of the superclass (Duck)

– Users of the base class (Duck) should expect same functionality

– Violation of the Liskov Substitution Principle

Page 29: L04 Software Design 2

The Liskov Substitution Principle

Subtypes must be substitutable for their base types. Code that uses references to base class must be able to use objects of derived classes without knowing it.

BarbaraLiskov

Page 30: L04 Software Design 2

The Liskov Substitution Principle All code operating with reference to the

base class should be completely transparent to the type of the inherited object

It should be possible to substitute an object of one type with another within the same class hierarchy

Inheriting classes should not perform any actions that will invalidate the assumptions made by the base class

Page 31: L04 Software Design 2

LSP Examplepublic class Rectangle {

protected int _width; protected int _height; public int getWidth() { return _width; } public int getHeight() { return _height; } public void setWidth(int width) { _width = width; } public void setHeight(int height) { _height = height; }}

Page 32: L04 Software Design 2

LSP Examplepublic class Square extends Rectangle {

public void setWidth(int width) { _width = width; _height = width; }

public void setHeight(int height) { _height = height; _width = _height; } }

Implementation convenience

Page 33: L04 Software Design 2

LSP Exampleimport junit.framework.Assert;import org.junit.Test;

public class RectangleTests {

@Test public void areaOfRectangle() {

Rectangle r = new Square(); r.setWidth(5); r.setHeight(2); // Will Fail - r is a square and sets // width and height equal to each other. Assert.assertEquals(r.getWidth() * r.getHeight(),10); }}

Page 34: L04 Software Design 2

Trying to fix the Problem Let’s try using interfaces– Flyable and Quackable Code duplication!

Page 35: L04 Software Design 2

What is the Problem? We tried this– Inheritance changes all subcasses– Interfaces cause code duplication

The problem is we are mixing different types of code in one type of classes

Fix– Separate Variation Design Principle– Take what varies and encapsulate it so it

wont affect the rest of the code

Page 36: L04 Software Design 2

Separate Variations

Identify the aspects of your application that vary and separate them from what stays the same

Page 37: L04 Software Design 2

Separation of Concerns Separate what changes from what stays

the same– Move duck behavior to a separte classes

FlyWithWings flyBehavior = new FlyWithWings();DATA TYPE IS TOO SPECIFIC

Page 38: L04 Software Design 2

Separation of Concerns But the Duck classes cannot use the

concrete behavior classes! – We need an interface or supertype

FlyBehavior flyBehavior = new FlyWithWings();

INTERFACE - POLYMORPHISIM

Page 39: L04 Software Design 2

The Interface Design Principle

Program to an interface, not an implementation

Page 40: L04 Software Design 2

Loose Coupling with Interfaces Advantages– The ability to change the implementing class of

any application object without affecting calling code

– Total freedom in implementing interfaces– The ability to provide simple test

implementations and stub implementations of application interfaces as necessary

Page 41: L04 Software Design 2

Program to an interfacesProgram to an implementation

Program to interface/subtype

Program to unknown creation

Dog d = new Dog();d.bark();

Animal animal = new Dog();animal.makeSound();

Animal animal = getAnimal();animal.makeSound();

Page 42: L04 Software Design 2

Program to an interfacesDependency Injection– Make the caller responsible for setting the

dependencyprivate Animal animal;

public setAnimal(Animal animal){ this.animal = animal;}...

animal.makeSound();

Injection happens here, in the set-method

LOOSE COUPLING = BEAUTIFUL!

Page 43: L04 Software Design 2

Implementing Behavior We can add new behaviors without

touching the Duck classes

Page 44: L04 Software Design 2

Integrating the Behavior The Duck classes will now delegate its

flying and quacking behavior

Behavior interfaces

Perform the Bahavior

Page 45: L04 Software Design 2

Integrating the Behavior Using the behavior

public class Duck { QuackBehavior quackBehavior; ...

public void performQuack() { quackBehavior.performQuack() }

}

We don’t care what kind of object this is, all we care is that it knows how to

quack!

Page 46: L04 Software Design 2

Integrating the Behavior Setting the behavior

public class MallardDuck extends Duck { public MallardDuck() { quackBehavior = new Quack(); flyBehavior = new FlyWithWings(); }

}

This is not programming to an interface!

Page 47: L04 Software Design 2

Setting Behavior Dynamically Add two new methods to the Duck class Dependency Injection

public void setFlyBehavior(FlyBehavior flyBehavior){ this.flyBehavior = flyBehavior}

public void setQuackBehavior(QuackBehavior quackBehavior){ this.quackBehavior = quackBehavior}

DuckFactory{ public Duck getMallardDuck() { Duck duck = new MallardDuck() duck.setFlyBehavior(new FlyWithWings()); duck.setQuackBehavior(new Quack()); return duck; }}

Page 48: L04 Software Design 2

Setting Behavior Dynamically The idea– Don´t think: Mallard is-a flying duck, think: it

has-a flying behavior– Putting two classes together where one is a

member in the other is a composition Creating systems using composition give

flexibilty– You can change the behavior at runtime

Page 49: L04 Software Design 2

Composition Design Principle

Favor composition over inheritance

Page 50: L04 Software Design 2

Object Composition Problems with concrete inheritance– Class hierarchy can get rigid– Difficult to change the implementation

Object Composition is more flexible – Allows the behaviour of an object to be altered

at run time, through delegating part of its behaviour to an interface and allowing callers to set the implementation of that interface

Page 51: L04 Software Design 2

Summary OO Programming is powerful– If used correctly– Remember Encapsulation, Interfaces,

Polymorphism Generic programming– Using classes, abstract classes and interfaces

can lead to powerful and flexible programs Reflection– Powerful for building infrastructure

Page 52: L04 Software Design 2

Job interview question

You are given the assignment of creating a component that needs to know sales statistics of Lottery tickets. You know that there is a another component in the system, Sale Server, that handles the sale. You need real-time information. What would you suggest?

EXERCISE

Page 53: L04 Software Design 2

Design Patterns Design pattern is a general solution to a common

problem in software design– Systematic approach for problems that reoccur in

software development– Not complete solution but starting point for design – Not code ready to use– Patterns have names and definitions– Built on common practices

Patterns should not be language dependant– However patterns apply for types of programming

languages

Page 54: L04 Software Design 2

Next Design Patterns


Recommended