4.3.3 High-Level
refactorings
The low-level transformations implement basic operations (move/ delete/ ...) for changing a program entity. High level transformations use object-oriented constructs (pattern) to change the relationship between the entities(inherit/factory/... ).
4.3.3.1 Inherit
transformations
Inherit refactorings can be classified into three groups:
- An existing class will become new super-class or interface(generalization)
- An existing class will become new subclass(specialization)
- Two existing but independent classes will be involved in an inherit-relationship
4.3.3.1.1
Generalize transformations
- An existing class receives a new super-class or interface(generalization) This construct can be used to create classes which would act like and can be used as the existing one but do something different. This can be a good way to avoid duplicate code in the program, because new classes can share the implementation of some methods, which do not change. This pattern is also useful to split a huge class.
a)
Generalize to interface
Description: Transformation creates an interface for an existing class
Conditions: Class(can be interface!)exists and interface does not.
DPT: DPT checks the condition.
Parameters: String (class), String (new interface name), Vector (methods-names)
Output: String (new interface).Output can be used in composite-transformation.
Program: An interface will be created and added to class. All selected methods will
be
added to the interface. The behavior of the program does not change.
b)
Generalize to super-class
Description: Transformation creates a super-class for the existing class
Conditions: Class(can not be interface!)exists and super-class does not.
Class does not have a super-class (multiple inherit is not allowed in Java)
DPT: DPT checks the condition.
Parameters: String (class), String (new interface name), Vector (methods -names)
Output: String (new class).Output can be used in composite-transformation.
Program: A new class will be created and added to subclass. All selected methods
will be added to super. The behavior of the program does not change.
4.3.3.1.2
Specialize transformations
- An existing class receives a new subclass(specialization) This transformation is the opposite of a generalization: It can split an existing large and complex class into several smaller classes.
a)
Implements Interface
Description: Transformation creates an abstract class for given interface
Optional: Default-constructor can be created.
Conditions: Interface exists and class does not.
DPT: DPT checks the condition.
Parameters: String ( interface), String (new class name),int Option(create constructor)
Output: String (new class).Output can be used in composite-transformation.
Program: Unreferenced abstract class will be created. Behavior does not change.
b)
Create subclass
Description: Transformation creates a plain subclass for the existing class
Optional: Default-constructor can be created.
Conditions: Super class exists and subclass does not.
DPT: DPT checks the condition.
Parameters: String (class), String (new class name),int Option(create constructor)
Output: String (new class).Output can be used in composite-transformation.
Program: A new class will be added to program.
Behavior does not change.
4.3.3.1.3
Plain Inherit
- Two existing but independent classes will be involved in an inherit-relationship This construct can be used to give new sub-class access to the members defined in new super-class.
Description: Transformation creates an inherit relationship for two classes.
Conditions: 1) Subclass is not already derived (or not derived from super interface)
2) Check: subclass is an interface and super is not
(minor) 3) Check: If super/sub class is interface/abstract
DPT: 1) DPT checks the condition.
2) DPT checks the condition.
3) If not accomplished -> DPT prompts user for the next step.
Parameters: String (super-class), String (sub-class)
Output: String (subclass).Output can be used in composite-transformation.
Program: Default: If condition is accomplished behavior does not change.
If minor
condition is not accomplished: If super-class
is interface or abstract and „continue„
is chosen, then subclass or its subclasses have to implement all methods of the
super-class/interface.
4.3.3.2 Substitute
transformations
Substitute refactorings change the type(type = class) of an entity (variable or method) to its super(up)- or sub(down)- class or interface. This is a good way to make the program more flexible and to let the program accept different implementations of a class-object. Changing the entity-type to its super does not change its behavior as long as this entity is created by a concrete constructor. Substitute transformations make the program more flexible. It is also possible to construct a transformation which will substitute an initialization of a variable or parameter of a method with a factory call. But complex interaction with the user and preconditions will make this transformation useless.
Example: Transformation „Replace method parameter with factory-call“.
Parameters: Parameter-type/position. List of all occurrences which should be replaced.
Name of create method(if many) of class, and so on...
Because the Up/down - substitute are very similar, only the Up-substitute will be described below. The entity can be a variable, a method or a method-parameter(s)
4.3.3.2.1
Substitute member type
Description: Transformation changes the type(=class)of variable to its super(up)
Conditions: 1)Check: If variable type is a class and if its implementation is available
2)Check: If variable type has a super-class/interface and if it is available
3)Check: If the owner of the variable calls method of variable,
that is not available in it’s super-class(interface)
DPT: 1) DPT checks the condition.
2) DPT checks the condition. DPT prompts the user if type is interface.
3) DPT does not check the condition.
Reason: DPT-Parser can't resolve all references to this member correctly
Parameters: String (variable-name)
Output: String (variable-name) can be used in composite-transformation.
Program: Variable-type changes to its super. Behavior does not change.
4.3.3.3.1 Substitute method type
Description: Transformation changes the type(=class) of method to its super(up)
Conditions: 1)Check: If method type is a class and its implementation is available
2)Check: If method type has super-class(or interface)and it’s available
3)Check: If owner of the method calls methods of method-type which
are not available in its super-class(interface)
DPT: 1) DPT checks the condition.
(minor) 2) DPT checks the condition. DPT prompts the user if type is interface.
3) DPT does not check the condition.
Reason: DPT-Parser can't resolve all references to this member correctly
Parameters: String (method-name)
Output: String (method-name) can be used in composite-transformation.
Program: Method-type changes to its super. Behavior does not change.
4.3.3.3.2
Substitute method parameters
Parameters of a method can be handled as variables. A transformation has to remember the positions of the method-parameter(if there is more than one parameter of the same type) DPT substitutes all parameters(which can be substituted)
Description: Transformation changes the parameter(=class) of method to its super(up)
Conditions: 1)Check: if method parameter is a class, if its implementation is available
(minor) 2)Check: if method parameter has super/interface and it's is available
(minor) 3)Check: if method uses members of the parameter which are not
available in its super-class.
DPT: 1) DPT checks the condition.
2-3) DPT checks the condition. DPT prompts user if type is interface.
Parameters: String (method-name)
Output: String (method-name) can be used in composite-transformation.
Program: Method-type changes to its super. Behavior does not change.
4.3.3.4 Factory
transformations
A factory transformation takes an existing or new class object and creates a method which constructs an object of a given type. The next step of the abstraction could be the abstract factory-pattern(see example in section 2.2): If the user doesn't want to specify a concrete constructor for an entity, he can create an abstract factory-object and only the chosen implementation of the factory will know how to build the new object. Factory transformations make the program easily extendible.
4.3.3.4.1
New factory
Description: Transformation creates a new class and a create-method
Conditions: 1) Check: If class created by factory exists and is not interface or abstract
2) Check: If factory(new class) does not exist
3) Check: If class created by factory has a constructor
DPT: 1) DPT checks the condition.
2) DPT checks the condition.
3) If not accomplished, DPT will generate a default constructor
Parameters: String (new class(factory)), String (name of the create-method),
String (class to be created), String(chosen class constructor)
Output: String (new class(factory)) can be used in composite-transformation.
Program: New unreferenced class will be added. Behavior does not change.
4.3.3.4.2 Add factory method
Description: Transformation creates a new create-method in existing class(factory)
Conditions: 1) Check: If class created by factory exists and is not interface or abstract
2) Check: If factory exists and doesn't have this method.
3) Check: If class created by factory has a constructor
DPT: 1) DPT checks the condition.
2) DPT checks the condition.
3) If not accomplished, DPT will generate a default constructor
Parameters: String (class to be created), String(chosen class constructor)
String
(class-name(factory)), String (name of the create-method),
Output: String (class-name(factory)) can be used in composite-transformation.
Program: New unreferenced method will be added. Behavior does not change.