A Methodology for the Automated Introduction of Design Patterns
Total Page:16
File Type:pdf, Size:1020Kb
A Methodology for the Automated Introduction of Design Patterns Mel Ó Cinnéide Paddy Nixon Department of Computer Science Department of Computer Science University College Dublin Trinity College Dublin Ireland. Ireland. [email protected] [email protected] Abstract simply creating a new method or moving an existing method. One interesting category of higher-level In reengineering legacy code it is frequently useful to transformation that a designer may wish to apply introduce a design pattern in order to add clarity to the comprises those transformations that introduce design system and thus facilitate further program evolution. We patterns [10]. Design patterns loosen the binding between show that this type of transformation can be automated in program components thus enabling certain types of a pragmatic manner and present a methodology for the program evolution to occur with minimal change to the development of design pattern transformations. We program itself. For example, the creation of a Product address the issues of the definition of a starting point for object within a Creator class could be replaced by an the transformation, the decomposition of a pattern into application of the Factory Method pattern (See appendix minipatterns and the development of corresponding A for a brief description of design patterns and the minitransformations that can introduce these Factory Method pattern in particular). This enables the minipatterns to a program. We argue that behaviour Creator class to be extended to use another type of preservation is a key issue and develop a rigorous Product object without significant reworking of the argument of this for each minitransformation we existing code. Our aim is to develop a tool that automates discover. The architecture of an existing software the introduction of design patterns to an existing object- prototype is also discussed and the results of applying oriented program. this methodology to develop a transformation for the The scenario we assume is as follows: An existing Factory Method pattern are presented. (legacy) program is being extended with a new requirement. After studying the code and the desired Keywords: Software re-engineering and restructuring; requirement, it is concluded that the existing structure of legacy software; software evolution and modernisation; the program makes the desired extension difficult to behaviour preservation; design patterns. achieve, and that the application of some design pattern would introduce the desired flexibility to the program. It is at this point in the process that we aim to provide tool 1. Introduction support to the designer. A key aspect of this approach is that it is the designer that decides what design pattern to Legacy systems frequently require restructuring in apply and where it is to be applied. We are not attempting order to make them more amenable to future changes in to automate quality in any way; our aim is purely to requirements. This restructuring is performed either by remove the burden of tedious and error-prone code hand or through the use of automated tools (e.g., [1]). In reorganisation from the designer. In this paper we present the latter case, the designer usually specifies certain a methodology and tool support for the development of operations to be carried out, e.g., to extract a method from these design pattern transformations. existing code or to move a method from one class to In section 2 we outline the methodology we propose another, and the tool handles the mundane details of for the development of design pattern transformations. performing the transformation itself. Sections 3, 4 and 5 discuss key aspects of this We are interested in developing automated support for methodology in more detail, using the Factory Method program transformations that use a more sophisticated pattern as an example. In section 6 we present the final target structure than that of the examples just mentioned. transformation that introduces this design pattern. The A designer usually has a architectural view of how they architecture of our current prototype is described in wish the program to evolve that is at a higher level than Authorized licensed use limited to: University of Canberra. Downloaded on June 22,2020 at 03:24:09 UTC from IEEE Xplore. Restrictions apply. section 7 and some practical results are presented. In for a given design pattern. At present we automate the section 8 we compare our approach with other work in application of the design pattern transformation; we aim this area and finally in section 9 we present some ultimately to automate the building of the transformation as conclusions. well. This means that given a definition of the structure of a design pattern and a starting point for the transformation, 2. Methodology the system can semi-automatically generate the appropriate transformation. At present we use transformations with 2.1. Motivation semi-formal pre- and postconditions (à la Opdyke [16]) and use them to reason rigorously about program behaviour. There are several criteria we wish our methodology to This opens the possibility of taking a fully-formal approach fulfil: in the future thus enabling the derivation of the • The design pattern transformations developed must transformation algorithm itself. preserve program behaviour. • The transformations are to be applicable to real 2.2. Outline of methodology programs. • Reuse of portions of existing transformations must be feasible. Select Design • The possibility of automating the development of the Pattern transformation itself should be provided for. We expand on these criteria in the following paragraphs. Behaviour preservation: Software maintenance activities frequently involve updating existing software in order to address some new requirement. This problem is Decide on usually simplified by breaking it into two stages: Precursor for this pattern 1. Refactoring: This involves changing the design of the program so as to make it more amenable to the proposed change, while not changing the behaviour of the program. This corresponds roughly to the consolidation phase described in [9]. Decompose into Minipatterns 2. Actual Updating: Here the program is changed to fulfil the new requirement. If the refactoring step has been successful, this step will be considerably simplified. We are aiming to construct a tool that supports the refactoring step. For this to be successful in practice, the Do designer must have a strong degree of confidence that the Minitransformations no Define minitrans- transformations being applied do indeed preserve program exist? formations behaviour. In our approach we therefore place a heavy emphasis on demonstrating that the design pattern yes transformations are behaviour preserving. Define design pattern Applicability to real programs: The tool developed transformation as should be applicable to real programs and be able to cope composition of with the complexities of source code representation of minitransformations design structures. This conflicts to a certain extent with the previous point, in that formally proving properties of Figure 1. The design pattern methodology. programs written in industrial-strength languages is unrealistic. We resolve this by working with an industrial The methodology is depicted as an activity chart in language, Java, and taking a semi-formal approach to figure 1. Initially a design pattern is chosen that will serve demonstrating behaviour preservation. as a target for the design pattern transformation under Reuse where possible: Design patterns have a lot in development. We then consider what the starting point for common so it is to be expected that design pattern this transformation will be, that is, what sort of design transformations will have a lot in common as well. In our structures it may be applied to. This starting point is methodology we seek to decompose the transformations into termed a precursor, which is described in more detail in reusable chunks and to make these chunks available to later section 3. It has now been determined where the developments of design pattern transformations. transformation begins, (the precursor) and where it ends Allow for future automation: There is a non-trivial (the design pattern itself). This transformation is then amount of work involved in building a new transformation decomposed into a sequence of minipatterns. A Authorized licensed use limited to: University of Canberra. Downloaded on June 22,2020 at 03:24:09 UTC from IEEE Xplore. Restrictions apply. minipattern is a design motif that occurs frequently; in At the other extreme there is the antipattern ([12]) this way it is similar to a design pattern but is a lower- approach, which was investigated in our earlier work level construct. Section 4 examines minipatterns in more ([14], [15]). In this approach the assumption is made that detail. the designer has failed to appreciate the need for the For every minipattern discovered a corresponding pattern in the first instance, and has used some inadequate minitransformation must also be developed. A design structure to deal with the situation. The philosophy minitransformation comprises a set of preconditions, a behind this approach is that the code may have been sequence of transformation steps, a set of postconditions developed by a designer who was not aware of patterns. and an argument demonstrating behaviour preservation. For example, in the case of the Factory Method pattern, Minitransformations are our unit of reuse, so for any the client of the Creator class may have to configure it minipattern identified we first check if a with a flag to tell it what type of Product class to create. minitransformation for it has already been built as part of We discovered several problems with this approach: the development of a previous design pattern • For any pattern there are several variants and for each transformation. If so, that minitransformation can be variant there can be several antipatterns. The volume reused now, otherwise a new minitransformation must be of antipatterns rises sharply and each one has to be developed.