principles of software construction objects design and
play

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


  1. Principles of Software Construction: Objects, Design, and Concurrency Design for large-scale reuse: Libraries and frameworks Michael Hilton Bogdan Vasilescu 17-214 1

  2. Administrivia • Homework 4b due tonight 17-214 2

  3. Key concepts from Tuesday 17-214 3

  4. Today: Libraries and frameworks for reuse 17-214 4

  5. 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 17-214 5

  6. Reuse and variation: Family of development tools 17-214 6

  7. Reuse and variation: Web browser extensions 17-214 7

  8. Reuse and variation: Flavors of Linux 17-214 8

  9. Reuse and variation: Product lines 17-214 9

  10. The promise Development Cost without reuse Development with reuse # Products 17-214 10

  11. Today: Libraries and frameworks for reuse • Terminology and examples • Whitebox and blackbox frameworks • Designing a framework • Implementation details 17-214 11

  12. Today: Libraries and frameworks for reuse • Terminology and examples • Whitebox and blackbox frameworks • Designing a framework • Implementation details 17-214 12

  13. 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 public MyWidget extends JContainer { public MyWidget( int param) { // setup internals, without rendering } Library // 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 • E.g.: Math, Collections, Graphs, I/O, Swing 17-214 13

  14. 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 public MyWidget extends JContainer { public MyWidget( int param) { // setup internals, without rendering } Framework // 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 • E.g.: Eclipse, Firefox, Spring, Swing 17-214 14

  15. 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"); ... } } 17-214 15

  16. 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? 17-214 16

  17. 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"); ... } } 17-214 17

  18. 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()); ... } 17-214 18

  19. 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 class Calculator extends Application { public Application() { protected String getApplicationTitle() { return "My Great Calculator"; } JPanel contentPane = new JPanel( new BorderLayout()); protected String getButtonText() { return "calculate"; } contentPane.setBorder( new BevelBorder(BevelBorder.LOWERED)); protected String getInititalText() { return "(10 – 3) * 6"; } JButton button = new JButton(); protected void buttonClicked() { button.setText(getButtonText()); JOptionPane. showMessageDialog ( this , "The result of " + getInput() + contentPane.add(button, BorderLayout.EAST); " is " + calculate(getInput())); textField = new JTextField(""); } textField.setText(getInitialText()); private String calculate(String text) { ... } 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()); ... } 17-214 19

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

  21. 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 public MyWidget extends JContainer { public MyWidget( int param) { // setup internals, without rendering } Framework // 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 • E.g.: Eclipse, Firefox, Spring, Swing 17-214 21

Recommend


More recommend