Principles of Software Construction: Objects, Design, and - - PowerPoint PPT Presentation

principles of software construction objects design and
SMART_READER_LITE
LIVE PREVIEW

Principles of Software Construction: Objects, Design, and - - PowerPoint PPT Presentation

Principles of Software Construction: Objects, Design, and Concurrency Design for large-scale reuse: Libraries and frameworks Michael Hilton Bogdan Vasilescu 17-214 1 Administrivia Homework 4b due tonight 17-214 2 Key concepts from


slide-1
SLIDE 1

1

17-214

Principles of Software Construction: Objects, Design, and Concurrency Design for large-scale reuse: Libraries and frameworks

Michael Hilton Bogdan Vasilescu

slide-2
SLIDE 2

2

17-214

Administrivia

  • Homework 4b due tonight
slide-3
SLIDE 3

3

17-214

Key concepts from Tuesday

slide-4
SLIDE 4

4

17-214

Today: Libraries and frameworks for reuse

slide-5
SLIDE 5

5

17-214

Earlier in this course: Class-level reuse

  • Language mechanisms supporting reuse

– Inheritance – Subtype polymorphism (dynamic dispatch) – Parametric polymorphism (generics)*

  • Design principles supporting reuse

– Small interfaces – Information hiding – Low coupling – High cohesion

  • Design patterns supporting reuse

– Template method, decorator, strategy, composite, adapter, … * Effective Java items 26, 29, 30, and 31

slide-6
SLIDE 6

6

17-214

Reuse and variation: Family of development tools

slide-7
SLIDE 7

7

17-214

Reuse and variation: Web browser extensions

slide-8
SLIDE 8

8

17-214

Reuse and variation: Flavors of Linux

slide-9
SLIDE 9

9

17-214

Reuse and variation: Product lines

slide-10
SLIDE 10

10

17-214

The promise

# Products Cost Development with reuse Development without reuse

slide-11
SLIDE 11

11

17-214

Today: Libraries and frameworks for reuse

  • Terminology and examples
  • Whitebox and blackbox frameworks
  • Designing a framework
  • Implementation details
slide-12
SLIDE 12

12

17-214

Today: Libraries and frameworks for reuse

  • Terminology and examples
  • Whitebox and blackbox frameworks
  • Designing a framework
  • Implementation details
slide-13
SLIDE 13

13

17-214

Terminology: Library

  • Library: A set of classes and methods that provide reusable

functionality

  • Client calls library; library executes and returns data
  • Client controls

– Program structure – Control flow

  • E.g.: Math, Collections, Graphs, I/O, Swing

Library

public MyWidget extends JContainer { public MyWidget(int param) { // setup internals, without rendering } // render component on first view and resizing protected void paintComponent(Graphics g) { // draw a red box on his component Dimension d = getSize(); g.setColor(Color.red); g.drawRect(0, 0, d.getWidth(), d.getHeight()); } }

your code

slide-14
SLIDE 14

14

17-214

Terminology: Frameworks

  • Framework: Reusable skeleton code that can be customized into

an application

  • Framework calls back into client code

– The Hollywood principle: “Don’t call us. We’ll call you.”

  • Framework controls

– Program structure – Control flow

  • E.g.: Eclipse, Firefox, Spring, Swing

Framework

public MyWidget extends JContainer { public MyWidget(int param) { // setup internals, without rendering } // render component on first view and resizing protected void paintComponent(Graphics g) { // draw a red box on his component Dimension d = getSize(); g.setColor(Color.red); g.drawRect(0, 0, d.getWidth(), d.getHeight()); } }

your code

slide-15
SLIDE 15

15

17-214

A calculator example (without a framework)

public class Calc extends JFrame { private JTextField textField; public Calc() { JPanel contentPane = new JPanel(new BorderLayout()); contentPane.setBorder(new BevelBorder(BevelBorder.LOWERED)); JButton button = new JButton(); button.setText("calculate"); contentPane.add(button, BorderLayout.EAST); textField = new JTextField(""); textField.setText("10 / 2 + 6"); textField.setPreferredSize(new Dimension(200, 20)); contentPane.add(textfield, BorderLayout.WEST); button.addActionListener(/* calculation code */); this.setContentPane(contentPane); this.pack(); this.setLocation(100, 100); this.setTitle("My Great Calculator"); ... } }

slide-16
SLIDE 16

16

17-214

A simple example framework

  • Consider a family of programs consisting of a button and text

field only:

  • What source code might be shared? What code will differ?
slide-17
SLIDE 17

17

17-214

A calculator example (without a framework)

public class Calc extends JFrame { private JTextField textField; public Calc() { JPanel contentPane = new JPanel(new BorderLayout()); contentPane.setBorder(new BevelBorder(BevelBorder.LOWERED)); JButton button = new JButton(); button.setText("calculate"); contentPane.add(button, BorderLayout.EAST); textField = new JTextField(""); textField.setText("10 / 2 + 6"); textField.setPreferredSize(new Dimension(200, 20)); contentPane.add(textfield, BorderLayout.WEST); button.addActionListener(/* calculation code */); this.setContentPane(contentPane); this.pack(); this.setLocation(100, 100); this.setTitle("My Great Calculator"); ... } }

slide-18
SLIDE 18

18

17-214

A simple example framework

public abstract class Application extends JFrame { protected String getApplicationTitle() { return ""; } protected String getButtonText() { return ""; } protected String getInitialText() { return ""; } protected void buttonClicked() { } private JTextField textField; public Application() { JPanel contentPane = new JPanel(new BorderLayout()); contentPane.setBorder(new BevelBorder(BevelBorder.LOWERED)); JButton button = new JButton(); button.setText(getButtonText()); contentPane.add(button, BorderLayout.EAST); textField = new JTextField(""); textField.setText(getInitialText()); textField.setPreferredSize(new Dimension(200, 20)); contentPane.add(textField, BorderLayout.WEST); button.addActionListener((e) -> { buttonClicked(); }); this.setContentPane(contentPane); this.pack(); this.setLocation(100, 100); this.setTitle(getApplicationTitle()); ... }

slide-19
SLIDE 19

19

17-214

Using the example framework

public abstract class Application extends JFrame { protected String getApplicationTitle() { return ""; } protected String getButtonText() { return ""; } protected String getInitialText() { return ""; } protected void buttonClicked() { } private JTextField textField; public Application() { JPanel contentPane = new JPanel(new BorderLayout()); contentPane.setBorder(new BevelBorder(BevelBorder.LOWERED)); JButton button = new JButton(); button.setText(getButtonText()); contentPane.add(button, BorderLayout.EAST); textField = new JTextField(""); textField.setText(getInitialText()); textField.setPreferredSize(new Dimension(200, 20)); contentPane.add(textField, BorderLayout.WEST); button.addActionListener((e) -> { buttonClicked(); }); this.setContentPane(contentPane); this.pack(); this.setLocation(100, 100); this.setTitle(getApplicationTitle()); ... } public class Calculator extends Application { protected String getApplicationTitle() { return "My Great Calculator"; } protected String getButtonText() { return "calculate"; } protected String getInititalText() { return "(10 – 3) * 6"; } protected void buttonClicked() { JOptionPane.showMessageDialog(this, "The result of " + getInput() + " is " + calculate(getInput())); } private String calculate(String text) { ... } }

slide-20
SLIDE 20

20

17-214

Using the example framework again

public abstract class Application extends JFrame { protected String getApplicationTitle() { return ""; } protected String getButtonText() { return ""; } protected String getInitialText() { return ""; } protected void buttonClicked() { } private JTextField textField; public Application() { JPanel contentPane = new JPanel(new BorderLayout()); contentPane.setBorder(new BevelBorder(BevelBorder.LOWERED)); JButton button = new JButton(); button.setText(getButtonText()); contentPane.add(button, BorderLayout.EAST); textField = new JTextField(""); textField.setText(getInitialText()); textField.setPreferredSize(new Dimension(200, 20)); contentPane.add(textField, BorderLayout.WEST); button.addActionListener((e) -> { buttonClicked(); }); this.setContentPane(contentPane); this.pack(); this.setLocation(100, 100); this.setTitle(getApplicationTitle()); ... } public class Calculator extends Application { protected String getApplicationTitle() { return "My Great Calculator"; } protected String getButtonText() { return "calculate"; } protected String getInititalText() { return "(10 – 3) * 6"; } protected void buttonClicked() { JOptionPane.showMessageDialog(this, "The result of " + getInput() + " is " + calculate(getInput())); } private String calculate(String text) { ... } } public class Ping extends Application { protected String getApplicationTitle() { return "Ping"; } protected String getButtonText() { return "ping"; } protected String getInititalText() { return "127.0.0.1"; } protected void buttonClicked() { ... } }

slide-21
SLIDE 21

21

17-214

Terminology: Frameworks

  • Framework: Reusable skeleton code that can be customized into

an application

  • Framework calls back into client code

– The Hollywood principle: “Don’t call us. We’ll call you.”

  • Framework controls

– Program structure – Control flow

  • E.g.: Eclipse, Firefox, Spring, Swing

Framework

public MyWidget extends JContainer { public MyWidget(int param) { // setup internals, without rendering } // render component on first view and resizing protected void paintComponent(Graphics g) { // draw a red box on his component Dimension d = getSize(); g.setColor(Color.red); g.drawRect(0, 0, d.getWidth(), d.getHeight()); } }

your code

slide-22
SLIDE 22

22

17-214

General distinction: Library vs. framework

Library

public MyWidget extends JContainer { public MyWidget(int param) { // setup internals, without rendering } // render component on first view and resizing protected void paintComponent(Graphics g) { // draw a red box on his component Dimension d = getSize(); g.setColor(Color.red); g.drawRect(0, 0, d.getWidth(), d.getHeight()); } }

your code

Framework

public MyWidget extends JContainer { public MyWidget(int param) { // setup internals, without rendering } // render component on first view and resizing protected void paintComponent(Graphics g) { // draw a red box on his component Dimension d = getSize(); g.setColor(Color.red); g.drawRect(0, 0, d.getWidth(), d.getHeight()); } }

your code user interacts user interacts

slide-23
SLIDE 23

23

17-214

Libraries and frameworks in practice

  • Defines key abstractions and their interfaces
  • Defines object interactions and invariants
  • Defines flow of control
  • Provides architectural guidance
  • Provides defaults

framework library framework application

credit: Erich Gamma

slide-24
SLIDE 24

24

17-214

A simple example framework

public abstract class Application extends JFrame { protected String getApplicationTitle() { return ""; } protected String getButtonText() { return ""; } protected String getInitialText() { return ""; } protected void buttonClicked() { } private JTextField textField; public Application() { JPanel contentPane = new JPanel(new BorderLayout()); contentPane.setBorder(new BevelBorder(BevelBorder.LOWERED)); JButton button = new JButton(); button.setText(getButtonText()); contentPane.add(button, BorderLayout.EAST); textField = new JTextField(""); textField.setText(getInitialText()); textField.setPreferredSize(new Dimension(200, 20)); contentPane.add(textField, BorderLayout.WEST); button.addActionListener((e) -> { buttonClicked(); }); this.setContentPane(contentPane); this.pack(); this.setLocation(100, 100); this.setTitle(getApplicationTitle()); ... }

slide-25
SLIDE 25

25

17-214

Framework or library?

  • Eclipse
  • Java Collections
  • The Java Logging Framework
  • Java Encryption Services
  • Wordpress
  • Django
slide-26
SLIDE 26

26

17-214

A Carcassonne framework?

slide-27
SLIDE 27

27

17-214

Learning library and framework

  • Documentation
  • Tutorials, wizards,

and examples

  • Other client

applications and plugins

  • Communities,

email lists and forums

Library Effort Reward Framework

slide-28
SLIDE 28

28

17-214

More terms

  • API: Application Programming Interface, the interface of a library
  • r framework
  • Client: The code that uses an API
  • Plugin: Client code that customizes a framework
  • Extension point: A place where a framework supports extension

with a plugin

  • Protocol: The expected sequence of interactions between the

API and the client

  • Callback: A plugin method that the framework will call to access

customized functionality

  • Lifecycle method: A callback method that gets called in a

sequence according to the protocol and the state of the plugin

slide-29
SLIDE 29

29

17-214

Today: Libraries and frameworks for reuse

  • Terminology and examples
  • Whitebox and blackbox frameworks
  • Designing a framework
  • Implementation details
slide-30
SLIDE 30

30

17-214

Today: Libraries and frameworks for reuse

  • Terminology and examples
  • Whitebox and blackbox frameworks
  • Designing a framework
  • Implementation details
slide-31
SLIDE 31

31

17-214

Whitebox vs. blackbox framework

Whitebox frameworks Blackbox frameworks Subclassing and overriding methods Composition; implementing a plugin interface Subclass has main() but gives control to framework Plugin-loading mechanism loads plugins and gives control to framework Need to understand implementation

  • f superclass

Only need to understand interface Only one extension at a time Multiple plugins simultaneously Compiled together More modularity; separate deployment possible (.jar, .dll, …) Common pattern: Template method Common patterns: Strategy,

  • bserver

“Developer frameworks” “End-user frameworks”

slide-32
SLIDE 32

32

17-214

Is this a whitebox or blackbox framework?

public abstract class Application extends JFrame { protected String getApplicationTitle() { return ""; } protected String getButtonText() { return ""; } protected String getInitialText() { return ""; } protected void buttonClicked() { } private JTextField textField; public Application() { JPanel contentPane = new JPanel(new BorderLayout()); contentPane.setBorder(new BevelBorder(BevelBorder.LOWERED)); JButton button = new JButton(); button.setText(getButtonText()); contentPane.add(button, BorderLayout.EAST); textField = new JTextField(""); textField.setText(getInitialText()); textField.setPreferredSize(new Dimension(200, 20)); contentPane.add(textField, BorderLayout.WEST); button.addActionListener((e) -> { buttonClicked(); }); this.setContentPane(contentPane); this.pack(); this.setLocation(100, 100); this.setTitle(getApplicationTitle()); ... } public class Calculator extends Application { protected String getApplicationTitle() { return "My Great Calculator"; } protected String getButtonText() { return "calculate"; } protected String getInititalText() { return "(10 – 3) * 6"; } protected void buttonClicked() { JOptionPane.showMessageDialog(this, "The result of " + getInput() + " is " + calculate(getInput())); } private String calculate(String text) { ... } } public class Ping extends Application { protected String getApplicationTitle() { return "Ping"; } protected String getButtonText() { return "ping"; } protected String getInititalText() { return "127.0.0.1"; } protected void buttonClicked() { ... } }

slide-33
SLIDE 33

33

17-214

An example blackbox framework

public class Application extends JFrame { private JTextField textField; private Plugin plugin; public Application() { } protected void init(Plugin p) { p.setApplication(this); this.plugin = p; JPanel contentPane = new JPanel(new BorderLayout()); contentPane.setBorder(new BevelBorder(BevelBorder.LOWERED)); JButton button = new JButton(); button.setText(plugin != null ? plugin.getButtonText() : "ok"); contentPane.add(button, BorderLayout.EAST); textField = new JTextField(""); if (plugin != null) textField.setText(plugin.getInititalText()); textField.setPreferredSize(new Dimension(200, 20)); contentPane.add(textField, BorderLayout.WEST); if (plugin != null) button.addActionListener((e) -> { plugin.buttonClicked(); } ); this.setContentPane(contentPane); ... } public String getInput() { return textField.getText(); } } public interface Plugin { String getApplicationTitle(); String getButtonText(); String getInititalText(); void buttonClicked() ; void setApplication(Application app); }

slide-34
SLIDE 34

34

17-214

An example blackbox framework

public class Application extends JFrame { private JTextField textField; private Plugin plugin; public Application() { } protected void init(Plugin p) { p.setApplication(this); this.plugin = p; JPanel contentPane = new JPanel(new BorderLayout()); contentPane.setBorder(new BevelBorder(BevelBorder.LOWERED)); JButton button = new JButton(); button.setText(plugin != null ? plugin.getButtonText() : "ok"); contentPane.add(button, BorderLayout.EAST); textField = new JTextField(""); if (plugin != null) textField.setText(plugin.getInititalText()); textField.setPreferredSize(new Dimension(200, 20)); contentPane.add(textField, BorderLayout.WEST); if (plugin != null) button.addActionListener((e) -> { plugin.buttonClicked(); } ); this.setContentPane(contentPane); ... } public String getInput() { return textField.getText(); } } public interface Plugin { String getApplicationTitle(); String getButtonText(); String getInititalText(); void buttonClicked() ; void setApplication(Application app); } public class CalcPlugin implements Plugin { private Application app; public void setApplication(Application app) { this.app = app; } public String getButtonText() { return "calculate"; } public String getInititalText() { return "10 / 2 + 6"; } public void buttonClicked() { JOptionPane.showMessageDialog(null, "The result of " + application.getInput() + " is " + calculate(application.getInput())); } public String getApplicationTitle() { return "My Great Calculator"; } }

slide-35
SLIDE 35

35

17-214

Aside: Plugins could be reusable too

public class Application extends JFrame implements InputProvider { private JTextField textField; private Plugin plugin; public Application() { } protected void init(Plugin p) { p.setApplication(this); this.plugin = p; JPanel contentPane = new JPanel(new BorderLayout()); contentPane.setBorder(new BevelBorder(BevelBorder.LOWERED)); JButton button = new JButton(); button.setText(plugin != null ? plugin.getButtonText() : "ok"); contentPane.add(button, BorderLayout.EAST); textField = new JTextField(""); if (plugin != null) textField.setText(plugin.getInititalText()); textField.setPreferredSize(new Dimension(200, 20)); contentPane.add(textField, BorderLayout.WEST); if (plugin != null) button.addActionListener((e) -> { plugin.buttonClicked(); } ); this.setContentPane(contentPane); ... } public String getInput() { return textField.getText(); } } public interface Plugin { String getApplicationTitle(); String getButtonText(); String getInititalText(); void buttonClicked() ; void setApplication(InputProvider app); } public class CalcPlugin implements Plugin { private InputProvider app; public void setApplication(InputProvider app) { this.app = app; } public String getButtonText() { return "calculate"; } public String getInititalText() { return "10 / 2 + 6"; } public void buttonClicked() { JOptionPane.showMessageDialog(null, "The result of " + application.getInput() + " is " + calculate(application.getInput())); } public String getApplicationTitle() { return "My Great Calculator"; } } public interface InputProvider { String getInput(); }

slide-36
SLIDE 36

36

17-214

Whitebox vs. blackbox framework

Whitebox frameworks Blackbox frameworks Subclassing and overriding methods Composition; implementing a plugin interface Subclass has the main method but gives control to framework Plugin-loading mechanism loads plugins and gives control to framework Need to understand implementation

  • f superclass

Only need to understand interface Only one extension at a time Multiple plugins simultaneously Compiled together More modularity; separate deployment possible (.jar, .dll, …) Common pattern: Template method Common patterns: Strategy,

  • bserver

“Developer frameworks” “End-user frameworks”

slide-37
SLIDE 37

37

17-214

Today: Libraries and frameworks for reuse

  • Terminology and examples
  • Whitebox and blackbox frameworks
  • Designing a framework
  • Implementation details