finding design patterns from Model or P.E file

Hi,

       Are there any tools or APIs that take a model (as XMI probably) or a P.E file (so in effect, source code won't be available) and give the design patterns that were used in the model Which classes participate in which patterns and related info It could probably be one of the stages in reverse engineering, isn't


      Thank you.


Answer this question

finding design patterns from Model or P.E file

  • Dr.Bev

    Hi,

    Could you help me I realy don't know this kind of implementation.

    [Singleton]
    public class Singleton {
    .
    .
    [SingletonAccessor]
    public static Singleton Instance {
    get { return m_Instance; }
    }
    }


    Could I use this [Singleton] togenerate the code for this Design Patterns ou just to name it This is a VS Feauture

    I do an intensive use of Design Patterns in my Solution and this facilities would help a lot.

    Thank you,


  • varadaraj

    Hi,

    But that's what reverse engineering tools do isn't Get the model from source code. I don't know the accuracy of those tools because I haven't tried any. I thought what I was asking is one of the phases of reverse engineering. Albeit in that case, source code will be available but here model is available. So the tool has to dig out the patterns (might not be 100% accurate). In the example that you have given, although it isn't "Notification pattern", it matches with that upto 90% or so, then it would be classified(it is wrong) as that pattern by the tool.

  • Mark Wulf

    Hi,

    Attributes - I had them in mind as well. All this kind of discourages tools in that direction. Then what about "Refactoring" Generally most tools are equipped with refactoring mechanisms. And it is said that patterns are one of the important ways in which refactoring is done.

    So once the tool identifies "bad smells" in the code, it has to substitute that code fragment with an appropriate design pattern with the functionality intact. In this case, how does the tool make decisions regarding which design pattern is appropriate It is related to the identification of design patterns isn't


    I wanted to continue this discussion. So whatever ideas Iam getting, Iam just posting them.
    .


  • sirck

    Hi,

    Regarding the naming of the methods - I had the same doubt when I was posting my earlier reply. But I thought that programmers generally follow naming conventions. So that shouldn't be a problem.

    What about a tool dependent pattern analyzer Something similar to XMI formats. Only the tool that exported the XMI file can import it (or the ones that collaborate with it). Likewise, only the tool which was used to write the code (say Eclipse or VS with an in built pattern wizard) can be able to recognise the patterns that were used. It could use some tags or extra resource file to remember them.

    Is this possible

  • matttypething

    Hi,

    How do reverse engineering tools work As I said before, I didn't try any of those tools, only studied the theory part. Thats why I thought it is possible. Iam still a student. All the discussions and suggestions are welcome.

    Now, to the pattern matching part. I was thinking on the lines of what search engines do. Given a keyword (which we type in the search box), the search engines don't know the context in which we typed it. A given word will have different meanings in different contexts. So the search engine analyses that word and the engine that comes up closest to the results we had in mind is regarded as the best (study shows its Google). There are many criteria based on which searching is performed or the best one is determined.


    Now, coming to the pattern matching part - analysis of the characteristics of the class can be done to find out the set of classes that are involved in a particular pattern. Although in this case I don't know how the analysis should proceed. For eg, for "Observer Pattern" if set of classes follow the steps:

    1) Interface Subject with Attach(), Detach(), Notify() methods
    2) Interface Observer with Update() method.
    3) Two concrete classes which implement Subject and Observer and provide them with the state.

    If the classes follow the above steps then probably (I mentioned the probability as percentage) it is "Observer Pattern". There has to be other criteria to analyse these classes to determine the over all probability.

    Then again the tool that gives the closest correct result will be termed as the best. But you said they don't exist. Thats why I asked my first question about those tools.

    Thank you.



  • MarkBuckland

    I have yet to see a tool that can do this (and with some situations it would be impossible) this is especially true when you don't have source. Don't get me wrong, some such as singleton and a few others could be easily automated.

    btw: how do you match a pattern 90% (many patterns with a slight variation become another pattern).


  • Glen Clark

    "Regarding the naming  of the methods - I had the same doubt when I was posting my earlier reply. But I thought that programmers generally follow naming conventions. So that shouldn't be a problem."

    What do you name the accessor for a singleton instance or a factory method

    What if the names are in Russian

    What about obfusicators which deliberately make the method names things like aaaab aaaaa etc

    "What about a tool dependent pattern analyzer Something similar to XMI formats. Only the tool that exported the XMI file can import it (or the ones that collaborate with it). Likewise, only the tool which was used to write the code (say Eclipse or VS with an in built pattern wizard) can be able to recognise the patterns that were used. It could use some tags or extra resource file to remember them."

    Sure if there is metadata describing the patterns being used it would become quite easy. But this doesn't seem to help much with reverse engineering .. another method might even be to just decorate your objects with attributes that describe their roles.

    [Singleton]
    public class Singleton {
      .
      .
      [SingletonAccessor]
      public static Singleton Instance {
           get { return m_Instance; }
      }
    }


  • Giles Nutkins

    You mention names of methods ... these names are not required in order for the pattern to be met ..

    I could replace Attach with aaaaa detach with aaaab and notify with aaaac and still have an observer pattern (in fact if you are looking at obfusicated code this will likely be the case). The behavior of the object would remain the same and it would still be implementing an observer pattern.

    The other difficulty with this is that you are looking solely at the structure ... A perfect example of something deceptive to this methodology is a decorator/proxy. The key difference between the two is that the proxy is controlling access to the subject object where as the decorator is adding functionality (aside from that they can be identical).

    Another example of an item this would have trouble with is factory method ...

    public Foo Something() {
    ....
    }

    Is this a factory method or some other creational pattern In order to figure that out you would need to analyze the code in the method to look at the object construction (and this may be quite a deceptive task (calling through to other methods (perhaps its calling into an OR mapper ))

    As I said you can do some guessing based off of the structure but the reliablity level will be very low.


  • Geoff McElhanon

    I don't think such a tool would be feasable (atleast not in a reasonable way) as many of the patterns differ only in intent. As an example imagine you are writing such a tool and you come accross a structure such as the following ...

    public class Foo {
       public void Bar() { ... }
    }

    public class FooExtension : Foo {
       private Foo m_Subject;
       public overrides void Bar() {
           ...
           m_Subject.Bar();
       }
    }

    Decorator or Proxy I order to figure out which to classify it as, you would actually need to analyze the code in the extension to figure out what it was doing. As you said, there would be no source so this task would become impossible.

     

    There are loads of examples (especially when dealing with patterns from different sources (Specfication from DDD and Strategy from GoF which is an example you could figure out but there are loads that you could not).

     

    There is also the problem of "modified" patterns which is often seen ... I recently saw a good example of this that used the notification pattern (PEAA) in validation but instead of sending a surrogate object (message) it sent the originating object (the rule that was broken). This would be very difficult for such a system to detect as it is actually not the notification pattern though it operates in a similar fashion.


  • finding design patterns from Model or P.E file