Software engineering: design for reuse

10,204 views

Published on

Published in: Technology

Software engineering: design for reuse

  1. 1. http://www.slideshare.net/mbrambil http://home.dei.polimi.it/mbrambil http://twitter.com/MarcoBrambi SOFTWARE REUSE AND DESIGN FOR REUSE Marco Brambilla
  2. 2. Context of the Lesson  Prerequisites  Objectoriented programming  Software engineering basics (UML, patterns, ...)
  3. 3. Agenda  Introduction to reuse  Benefits and issues for reuse  Levels of reuse  System  Architecture  Design  Implementation  Some details on design
  4. 4. Introduction  Definition of reuse  Design for reuse and reuse of design  Purpose and state of the practice  in other disciplines  in software engineering
  5. 5. Benefits of software reuse Benefit Explanation Increased Reused software has been tried and tested dependability in working systems Reduced process The cost and risk of existing software is risk already known Effective use of reusable software encapsulates their specialists knowledge Accelerated both development and validation time may development be reduced
  6. 6. Problems with reuse Problem Explanation Increased reused elements of the system may become maintenance costs increasingly incompatible with system changes Not-invented-here Companies rewrite components because they syndrome believe they can improve on them or because they feel they must own them. Creating, Generality of components doesn’t come for free. maintaining, and Development processes have to be adapted using a component library Finding and Software components have to be discovered in a understanding library, understood and, sometimes, adapted components Understanding Reused elements always come with prerequisites the applicability application field must comply with
  7. 7. The 4 layers of reuse 4. Whole system Configuration and BlackBox reuse of Config. applications 3. (Macro-, System-, Enterprise-, Global-) Reuse of application Architecture Frameworks frameworks, middleware, services 2. Design (micro architecture) Reuse of designs and Patterns object and function reuse components 1. Implementation Reuse of classes and methods Programming
  8. 8. 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
  9. 9. 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
  10. 10. 2. Reuse at design level  Objectorientation 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
  11. 11. 1. Approaches supporting reuse at implementation level  Program libraries, APIs set of reusable artefacts  Program generators code generators
  12. 12. [D] 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  Objectorientation 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
  13. 13. [D1] 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
  14. 14. [D1] Object orientation for reuse Encapsulation, modularization, and inheritance : the main reuse features of OO that support reuse Component Package
  15. 15. [D1] 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.
  16. 16. [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.
  17. 17. Example [D1] Modularization example simulation.exe Render.java IRender LightModel IModels ILighting Environment
  18. 18. Example [D1] Packaging example
  19. 19. [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
  20. 20. [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
  21. 21. [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
  22. 22. Example [D1] What’s going to get printed? public class Animal { public static void hide() { System.out.format(“Hide animal."); } public static void main(…) { public void override() { Cat myCat = new Cat(); System.out.format(“Override Animal."); } Animal myAnimal = myCat; } //myAnimal.hide(); //Bad style! Animal.hide(); //Better! myAnimal.override(); } public class Cat extends Animal { } public static void hide() { System.out.format(“Hide Cat."); } public void override() { System.out.format(“Override Cat."); } }
  23. 23. Example [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.
  24. 24. [D2] 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
  25. 25. [D2] How patterns arise Problem Forces Solution Benefits Consequences Related Patterns
  26. 26. [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
  27. 27. Example [D2] Composite pattern  Construct part-whole hierarchy  Simplify client interface to leaves/composites  Easier to add new kinds of components 0..* Client Component Operation() Add(Component) Remove(Component) children Leaf Composite Operation() Operation() Add(Component) Remove(Component) For all c in children c.Operation();
  28. 28. Example [D2] Composite pattern  Example: figures in a structured graphics toolkit Controller 0..* 0..* View Figure children paint() translate() getBounds() LabelFigure BasicFigure CompositeFigure parent paint() paint() paint() addFigure(Figure) removeFigure(Figure) For all c in children c.paint();
  29. 29. For your reference 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..
  30. 30. For your reference Structural Design Patterns Define 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.
  31. 31. For your reference Behavioural Design Patterns Define 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.
  32. 32. For your reference Behavioural Design Patterns Define 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.
  33. 33. 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
  34. 34. 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  ...

×