Advanced-Programming

Software components

Slides:

Why components?

Components allows to re-use software, with the following benefits.

Desiderata for sw components

  1. Modular (packaged code)
    • compatible (simple interface)
    • reusable (its main aspect)
    • extendible (through inheritance and overriding)
  2. Reliable
    • correctness (respect specification)
    • robustness (a.k.a. fault-tolerance: able to work in abnormal situations)
  3. Efficient
  4. Portable to different platforms
  5. Timely: released when or before needed

Basic concepts

Modules vs Components

Modules are available only for that specific programming language that you are using, and doesn’t allow to cooperate with code written in different programming languages.
Modules are usually compiled separately, and the implementation is not known to the module user.

Components has several concept already present in modules, but instead of being part of a program, as for the module, are part of a system.
Components can be anything and contain anything.

Component specification

A Component specification describes, through a set of component interfaces, the behaviour of a set of component objects.
The component specification is realized as a component implementation, which is independently deployable as installed component. An instance of installed components is called component object.
A component composition is the process of assembling components to form an application or a larger component.

Java Beans

A software component model for Java.

A class to be considered a Java Bean must support:

Furthermore, must:

Reflection in Java

The ability of a program to manipulate itself as data.

This abilities requires reification, a mechanism to encode the execution state as data.

Reflection purpose

Drawbacks of reflection

Java reflection

For every type the JVM keeps an object of class java.lang.Class that is the entry point for reflection.
This object can be obtained using Object.getClass() or Class.forName(String className).

It contains:

It is usually used to:

Example:

Some operations are forbidden by privacy rules

Microsoft Software Components

Distributed Component Technologies

The goal is the integration and interoperability of services for applications on various platform.

.NET Framework

Consists of:

.NET Assembly vs Modules

An Assembly is a .NET library. Has a .dll (local, not executable) or .exe (distributed, executable) extension and can be loaded dynamically.
Consists in up to 4 parts: manifest, metadata, CIL code and resources.

A Module has the same format of an Assembly, but doesn’t include a manifest and is not dynamically loadable.

Delegates

Delegate is a type that represent a reference to a method: the method can be invoked by a delegate instance. Using delegates is possible to set up Event Handlers exploiting Observer or Publish/subscribe design pattern for events as for Java.

Inversion of Control

With Inversion of Control, the program flows is not dictated by the caller but by the framework.
The framework provides a default behavior that can be changed by extensibility: subclassing and selective overriding, implementing interfaces, registering for events.

Example: in GUI-based interaction, the GUI loop decide when to call the methods. Hollywood Principle: “Don’t call us, we’ll call you”.

Loosely coupled systems

Loosely coupled systems provides many advantages, and in particular improve the extensibility, testability and reusability of the software.

Service Locator and Dependency Injection

Service Locator helps in avoiding strong coupling exploiting object factoring and generic interfaces.

Dependency Injection allows avoiding hard-coded dependencies (strong coupling) injecting them at runtime.
It is very convenient for mock testing and for fast replacing of dependency.

IoC Containers create objects, ensure that all dependencies are satisfied and then provide a lifecycle support.

Template methods