software engineering: design for reuse
DESCRIPTION
TRANSCRIPT
SOFTWARE REUSE ANDDESIGN FOR REUSE
Marco Brambilla
http://home.dei.polimi.it/mbrambil http://twitter.com/MarcoBrambi
http://www.slideshare.net/mbrambil
Context of the Lesson
Prerequisites Object oriented programming Software engineering basics (UML,
patterns, ...)
Agenda
Introduction to reuse Benefits and issues for reuse Levels of reuse
System Architecture Design Implementation
Some details on design
Introduction
Definition of reuse Design for reuse and reuse of design Purpose and state of the practice
in other disciplines in software engineering
Benefits of software reuse
Benefit Explanation
Increased dependability
Reused software has been tried and tested in working systems
Reduced process risk
The cost and risk of existing software is already known
Effective use of specialists
reusable software encapsulates their knowledge
Accelerated development
both development and validation time may be reduced
Problems with reuse
Problem Explanation
Increased maintenance costs
reused elements of the system may become increasingly incompatible with system changes
Not-invented-here syndrome
Companies rewrite components because they believe they can improve on them or because they feel they must own them.
Creating, maintaining, and using a component library
Generality of components doesn’t come for free. Development processes have to be adapted
Finding and understanding components
Software components have to be discovered in a library, understood and, sometimes, adapted
Understanding applicability
Reused elements always come with prerequisites the application field must comply with
The 4 layers of reuse
4. Whole system
3. (Macro-, System-, Enterprise-, Global-) Architecture
2. Design (micro architecture)
1. Implementation
BlackBox
Frameworks
Patterns
Programming
Config.
object and function reuse
Reuse of classes
and methods
Reuse of designs
and componen
ts
Reuse of application frameworks, middleware,
services
Configuration and reuse
of applications
4. Approaches supporting reuse at system level
Software product lines COTS (Commercial, off-the-shelf) product
reuse Configurable vertical applications ERP (Enterprise Resource Planning)
systems
3. Approaches supporting reuse at architecture level
Architectural patterns standard sw architectures
Application frameworks classes at system level
Legacy system wrapping interfaces to old code
Service-oriented systems shared (third-party) services
2. Reuse at design level
Object orientation object design and development
Design patterns reusable software solutions
Model-driven engineering models and transformations
Aspect-oriented software development perspectives
Component-based development cbse, component-model
1. Approaches supporting reuse at implementation level
Program libraries, APIs set of reusable artefacts
Program generators code generators
Reuse at design level
Mix of design best practices Not granted by the (design or coding)
language ... but: some paradigms may help in the
job
Object orientation object design and development
Design patterns reusable software solutions
Model-driven engineering models and transformations
Aspect-oriented software development perspectives
Component-based development cbse, component-model
[D]
OO Principles orient to reuse
Open/close principle Software entities should be open for extension but closed for
modifications. Dependency inversion
High-level modules should not depend on low-level modules. Both should depend on abstractions.
Abstractions should not depend on details. Details should depend on abstractions.
Interface segregation principle Clients should not be forced to depend on/ implement interfaces
that they don't use. Single responsibility – separation of concerns
A class should have only one reason to change Substitution (Liskov)
If a program is using a Base class, then the reference to the Base class can be replaced with a Derived class without affecting the overall functionality
[D1]
Object orientation for reuse
Encapsulation, modularization, and inheritance :
the main reuse features of OO that support reuse
[D1]
Component
Package
Encapsulation
Encapsulation: to expose only enough of a module to allow other modules to make use of it.
You can syntactically seal off implementation details, leading to more flexibility and maintainability in your system.
Every time an item is designed as private (restricted), it encompasses potential for reuse and redefinition.
Every item of the system can change independently, no impact to the other modules.
[D1]
Modularization
Components, Interfaces, packages: basic mechanisms that ONLY aim at modularization (and thus reuse)
Components allow system to be assembled from binary replaceable elements A component is physical – bits, not concepts (Iike
classes)
A component provides the realization of a set of interfaces.
A component can be replaced by any other component(s) that conforms to the interfaces.
A component is part of a system.
[D1]
Modularization exampleExample
[D1]
simulation.exe Render.java
IModels ILighting
IRender
LightModel
Environment
Packaging exampleExample
[D1]
Overriding and Overloading
Overriding, overloading, and polimorphism are the concrete mechanisms for reuse based on inheritance
Overriding, or hiding, is when you have a method with the same name and parameters as in a superclass
the rest of the superclass is reused
Overloading is when you have multiple methods of the same name, but with different parameter lists.
the object is more likely to be reused
[D1]
Polymorphism (Many Forms!)
Polymorphism is when objects of various types define a common interface of operations for users.
users can share usage, although at runtime instances of different types can be bound
Literally means many forms Can submit/use an instance of a subclass when a
super type is expected Reference and object can be different Arguments and return types can be polymorphic
[D1]
What does OO bring you?
Avoid duplicate code Define a common API (protocol or contract) for a
group of classes Change in one place Can override methods if more specific behavior is
needed Code doesn’t need changing when new sub You can extend and change behavior, even if you
don't have source code
[D1]
What’s going to get printed?
public class Animal {
public static void hide() {
System.out.format(“Hide animal."); }
public void override() {
System.out.format(“Override Animal."); }
}
public class Cat extends Animal {
public static void hide() {
System.out.format(“Hide Cat."); }
public void override() {
System.out.format(“Override Cat."); }
}
Example
public static void main(…) {
Cat myCat = new Cat();
Animal myAnimal = myCat;
//myAnimal.hide(); //Bad style!
Animal.hide(); //Better!
myAnimal.override();
}
}
[D1]
The answer
The Cat class overrides the instance method in Animal called override and hides the class method in Animal called hide
For class methods, the runtime system invokes the method defined in the compile-time type of the reference
For instance methods, the runtime system invokes the method defined in the runtime type of the reference
The hide method in Animal. The override method in Cat.
Example
[D1]
Design patterns
A design pattern is a reusable solution to a recurrent problem
Software design patterns are based (somehow) on work by the architect Christopher Alexander
A design pattern captures design expertise – not created but abstracted from existing design examples
Using design patterns is reuse of design expertise
Design patterns provide a vocabulary for talking about design
[D2]
How patterns arise
ProblemProblem
Context
SolutionSolution
Benefits
Related Patterns
Consequences
Forces
[D2]
Patterns vs. “design”
Patterns are design But: patterns transcend the “identify
classes and associations” approach to design
Instead: learn to recognize patterns in the problem space and translate to the solution
[D2]
Composite pattern
ComponentOperation()Add(Component)Remove(Component)
CompositeOperation()Add(Component)Remove(Component)
LeafOperation()
Client
children
0..*
For all c in children c.Operation();
Construct part-whole hierarchy Simplify client interface to leaves/composites Easier to add new kinds of components
Example
[D2]
Composite pattern
Figure
paint()translate()getBounds()
CompositeFigure
paint()addFigure(Figure)removeFigure(Figure)
BasicFigurepaint()
Viewchildren
0..*
For all c in children c.paint();
Example: figures in a structured graphics toolkit
LabelFigurepaint()
0..*
Controller
parent
Example
[D2]
Creational Design Patterns
Manage the way objects are created Singleton - Ensures that only one instance of a class is
created and Provides a global access point to the object. Factory(Simplified version of Factory Method) - Creates objects
without exposing the instantiation logic to the client and Refers to the newly created object through a common interface.
Factory Method - Defines an interface for creating objects, but let subclasses to decide which class to instantiate and Refers to the newly created object through a common interface.
Abstract Factory - Offers the interface for creating a family of related objects, without explicitly specifying their classes.
Builder - Defines an instance for creating an object but letting subclasses decide which class to instantiate and Allows a finer control over the construction process.
Prototype - Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Object Pool - reuses and shares objects that are expensive to create..
For your reference
Structural Design PatternsDefine structures of objects and classes that can work
together and define how the relations can be defined between entities. Adapter - Convert the interface of a class into another
interface clients expect. Adapter lets classes work together, that could not otherwise because of incompatible interfaces.
Bridge - Compose objects into tree structures to represent part-whole hierarchies.
Composite - Compose objects into tree structures to represent part-whole hierarchies. / Composite lets clients treat individual objects and compositions of objects uniformly.
Decorator - add additional responsibilities dynamically to an object.
Flyweight - use sharing to support a large number of objects that have part of their internal state in common where the other part of state can vary.
Memento - capture the internal state of an object without violating encapsulation and thus providing a mean for restoring the object into initial state when needed.
Proxy - provide a “Placeholder” for an object to control references to it.
Facade - unified interface to a complex system.
For your reference
Behavioural Design PatternsDefine the interactions and behaviours of classes
Chain of Responsibiliy - It avoids attaching the sender of a request to its receiver, giving this way other objects the possibility of handling the request too. The objects become parts of a chain and the request is sent from one object to another across the chain until one of the objects will handle it.
Command - Encapsulate a request in an object, Allows the parameterization of clients with different requests and Allows saving the requests in a queue.
Interpreter - Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language / Map a domain to a language, the language to a grammar, and the grammar to a hierarchical object-oriented design
Iterator - Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
Mediator - Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
For your reference
Behavioural Design PatternsDefine the interactions and behaviours of classes
Observer - Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
Strategy - Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
Template Method - Define the skeleton of an algorithm in an operation, deferring some steps to subclasses / Template Method lets subclasses redefine certain steps of an algorithm without letting them to change the algorithm's structure.
Visitor - Represents an operation to be performed on the elements of an object structure / Visitor lets you define a new operation without changing the classes of the elements on which it operates.
Null Object - Provide an object as a surrogate for the lack of an object of a given type. / The Null Object Pattern provides intelligent do nothing behavior, hiding the details from its collaborators.
For your reference
References
Ian Sommerville. Software Engineering, Addison Wesley
Martin Fowler et al. Refactoring: Improving the Design of Existing Code, Addison Wesley
Ivar Jacobson et al. Software Reuse: Architecture, Process and Organization for Business Success, Addison Wesley
E. Gamma, R. Helm, R. Johnson, H. Vlissides (“the gang of four”), Design Patterns, Addison-Wesley
Further readings
Diomidis Spinellis, Cracking Software Reuse, IEEE Software, 2007
David A. Wheeler, Free-Libre / Open Source Software (FLOSS) is Commercial Software, web, 2009
Frakes, W.B. and Kyo Kang. Software Reuse Research: Status and Future, IEEE TSE, 2005
...