ohar3 komponentit

Upload: pasi-nieminen

Post on 08-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 Ohar3 Komponentit

    1/29

    1Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    3.1 The component vision: rationalizing software development

    3.2 The component concept

    3.3 Components as software units

    3.4 Interfaces

    3.5 Specifying interfaces

    3.6 Customizing components

    3. Software components and interfaces3. Software components and interfaces

  • 8/7/2019 Ohar3 Komponentit

    2/29

    2Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    3.1 The component vision: rationalizing software3.1 The component vision: rationalizing software

    productionproduction

    Developing software products from existing components

    products become more realiable

    easier to construct products easier to train people

    component competition reduces prices

    More or less standard practice in other engineering areas

  • 8/7/2019 Ohar3 Komponentit

    3/29

    3Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    Exercise: Write down 5 characteristicExercise: Write down 5 characteristic

    properties of a good software componentproperties of a good software component

  • 8/7/2019 Ohar3 Komponentit

    4/29

    4Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    Composing applications from componentsComposing applications from components

    Composing new applications from existing componentsVisual component integration tools

  • 8/7/2019 Ohar3 Komponentit

    5/29

    5Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    Example: BeanBoxExample: BeanBox

  • 8/7/2019 Ohar3 Komponentit

    6/29

    6Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    Component = independent unit of software providingservices through a well-defined interface

    Szyperski:A software component is a unit of composition with contractuallyspecified interfaces and explicit context dependencies only.

    A software component can be deployed independently and issubject to composition by third parties.

    3.2 The component concept3.2 The component concept

  • 8/7/2019 Ohar3 Komponentit

    7/29

    7Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    Degree of independence

    Deployment

    Size

    Standardization

    Technology-specific properties

    Issues related to componentsIssues related to components

  • 8/7/2019 Ohar3 Komponentit

    8/29

    8Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    3.3 Components as software units3.3 Components as software units

    Units of architecture

    Units of functionality who is responsible of functionalites?

    Units of reuse what remains common?

    Units of product configuration what is included in a product?

    Units of deployment what can be upgraded in isolation?

    Units of variance what can be changed, how?

    Units of third-party software what can be obtained from outside?

    Units of work division what can be separated as tasks?

  • 8/7/2019 Ohar3 Komponentit

    9/29

    9Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    3.4 Interfaces3.4 Interfaces

    Subroutine library

    Module library

    Class library

    M

    A

    B

    C

    A

    B

    C

    A

    C

    B

    A

    C

    B

    X

    Y Z

    History:

    Abstract servicesignature

    Service

    implementation

    module

    class

    interface

    subroutine

    data

    datadata

  • 8/7/2019 Ohar3 Komponentit

    10/29

    10Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    Interfaces as firstInterfaces as first--class entitiesclass entities

    ComponentA

    ComponentB

    Interface1

    Interface2

    Interface3

    Interface4

  • 8/7/2019 Ohar3 Komponentit

    11/29

    11Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    Provided and required interfaces in UML 2.0Provided and required interfaces in UML 2.0

    Car

    Engine

    Car Engine

    PowerSource

    PowerSource

    PowerSource

    required interface

    provided interface

  • 8/7/2019 Ohar3 Komponentit

    12/29

    12Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    Possible implementation in Java:Possible implementation in Java:

    interface PowerSource {

    void start();

    int temperature();

    void stop();

    }

    class Engine implements PowerSource ... {...

    public void start() {...}

    public int temperature() {...}

    public void stop() {...}

    }

    class Car ... {

    ...

    private PowerSource eng;

    ...

    public void setEng(PowerSource e) {

    eng = e;

    }

    public void run() {...

    eng.start();

    ...

    eng.stop();...

    }

    }

  • 8/7/2019 Ohar3 Komponentit

    13/29

    13Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    Ports in UML 2.0Ports in UML 2.0

    Point of interaction between an object (component) and

    its environment

    Engine

    PowerSource

    Warnings

    Engine

    PowerSource

    Warnings

    host

    Port

  • 8/7/2019 Ohar3 Komponentit

    14/29

    14Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    Client Serviceprovider

    service interface

    postcondition: must hold,when the service has been delivered,assuming that the precondition heldwhen the service was called

    precondition: must hold,when the service is called

    3.5 Specifying interfaces:3.5 Specifying interfaces: contractcontractss

    defines the abstract semantics of the service allows exchange of participants,

    as long as the interface and its pre-and postconditions hold

    defines the responsibilities of checking(no multiple checking, clear separation of

    concerns)

    design-by-contract(Eiffel)

  • 8/7/2019 Ohar3 Komponentit

    15/29

    15Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    Class/component invariantsClass/component invariants

    Person

    op1(...)

    op2(...)op3(...)

    invariantage > 0 &age < toberetired

    age: inttoberetired: int

  • 8/7/2019 Ohar3 Komponentit

    16/29

    16Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    Inheritance & contractsInheritance & contracts

    public oper(...)prepost

    public oper(...)pre'post'

    pre post

    pre' post'

    pre'

    pre post

    post'

    pre post'

    pre' post

    pre'

    pre post'

    post

    A

    B

    C

    D

    Which is correct (A,B,C,D)?

  • 8/7/2019 Ohar3 Komponentit

    17/29

    17Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    Design by contract: example (1)Design by contract: example (1)

    class Stack {private Object [] storage;private int top;

    public Stack(int size) {storage = new Object[size];top = -1;

    }

    public int size() {return top+1}public boolean full() {return top == storage.length;}public void push(Object x) {

    if (top < storage.length-1) {

    storage[++top] = x;

    }else {

    System.out.println("Overflow");}

    }

  • 8/7/2019 Ohar3 Komponentit

    18/29

    18Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    Design by contract: example (2)Design by contract: example (2)

    public Object pop() {

    if (top >= 0) {return storage[top--];

    }else {

    System.out.println("Underflow");return null;

    }}

    }

    client: ... if s.size() > 0 then {s.pop();} else {... error handling ...} ...

    Problems?

  • 8/7/2019 Ohar3 Komponentit

    19/29

    19Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    Design by contract: example (3)Design by contract: example (3)

    interface Stack {Object pop()

    //pre size() > 0;//post size = old size 1;

    }

    class ArrayStack implements Stack {public Object pop() {

    return storage[top--];}

    }

    Prove that the pre- and postconditions hold (?)Produce run-time checks that the pre- and postconditions hold (?)

    Idealisticsolution:

  • 8/7/2019 Ohar3 Komponentit

    20/29

    20Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    Pragmatic approach: exceptionsPragmatic approach: exceptions

    public Object pop() throws Underflow {// pre: stack non-empty// post: stack size reduced by one// Underflow: stack is empty upon entry

    if (top >= 0) {return storage[top--];}else {

    throw new Underflow(this);}

    }

    exception = contractor is unable to fulfill its part of the contract method either fulfills the contract or throws an exception implies that the service provider is able to detect the unableness

    exceptions become part of the interface pre- and postconditions can be given informally

  • 8/7/2019 Ohar3 Komponentit

    21/29

    21Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    3.6 Customizing components3.6 Customizing components

    Changing the initial state of a component

    Implementing required interfaces

    Subclassing

  • 8/7/2019 Ohar3 Komponentit

    22/29

    22Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    Changing the initial state of a componentChanging the initial state of a component

    Comp

    ServicesIsetProperty(...)

    :CompClient

    create

    setProperty(...)

    use

  • 8/7/2019 Ohar3 Komponentit

    23/29

    23Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    In Java:In Java:

    class Button extends Component ... {

    ...

    String label;

    public Button() {...

    }

    public Button(String arg) {

    label = arg;...

    }

    public setLabel(String arg) {

    label = arg;

    }

    ...

    }

  • 8/7/2019 Ohar3 Komponentit

    24/29

    24Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    Implementing required interfacesImplementing required interfaces

    Button AppLogic

    ActionListener

  • 8/7/2019 Ohar3 Komponentit

    25/29

    25Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    SubclassingSubclassing

    Button

    CompanyButton

  • 8/7/2019 Ohar3 Komponentit

    26/29

    26Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    In Java:In Java:

    class CompanyButton extends Button {

    public CompanyButton(String arg) {super(arg);

    label = arg.toUpperCase();

    }

    public void setLabel(String arg) {

    label = arg.toUpperCase();

    }}

  • 8/7/2019 Ohar3 Komponentit

    27/29

    27Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    Customizedcomponent

    Fragile base class problemFragile base class problem

    "Innocent" modification ofthe original component

    can make the customizedcomponent invalid

    Comp

    Custom

    Library

    component

    E l (1)E l (1)

  • 8/7/2019 Ohar3 Komponentit

    28/29

    28Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    Example (1)Example (1)

    List

    CountedList

    addContainer:

    { ...addElement(...); ... }

    addElement:

    { super.addElement(...);counter++; }

    addElement(Object)

    addContainer(Container)

    Container

    getCount()

    CountedContainer

  • 8/7/2019 Ohar3 Komponentit

    29/29

    29Ohjelmistoarkkitehtuurit Syksy 2006 TTY Ohjelmistotekniikka

    Example (2)Example (2)

    List

    CountedList

    addContainer:

    { ...}

    addElement:

    { super.addElement(...);counter++; }

    addElement(Object)

    addContainer(Container)

    Container

    getCount()

    CountedContainer

    addElementno more called!