This page uses Python to implement an Adapter that displays a directory tree.
TreeDisplay is an abstract class that has two abstract methods GetChildren() and CreateGraphicNode(). The sample uses raise in both abstract methods to notify a programmer that the both are abstract that must be implemented in a derived class.
DirectoryTreeDisplay1 is an Adapter that adapts Python OS package (Adaptee). GetChildren() uses Python OS package to get directory tree. CreateGraphicNode() transforms directory tree to readable text.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFv54lJv150tzDV2p0hAWoUZL9y3nJZjveRgHu1lsCp42ASHMSMOhhbtFndocGKdAcUNX-2ThRrfCRc_IAueZ585NEoKL1VGq6dXrPqpP4AjF8CWDpbUcg5TLI95-Gj2pVpoiUhob9xN0/s400/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7+2015-05-30+%25E4%25B8%258B%25E5%258D%258811.25.07.png)
Test1() is a test function that tests DirectoryTreeDisplay1 ()
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHflHdw6iYR1xphkfdYf6_JsZoP1rvVqlSBaqReBchG72GkubMmxZrf_IcIyzBW10g6lXYxsb3ZV5xIxMBYvsTJkh9t5hf8pyK0Q7EDoJMPW5PuT72fynGpePPTlgjAqAs4jZScoRRPIQ/s400/%25E8%259E%25A2%25E5%25B9%2595%25E5%25BF%25AB%25E7%2585%25A7+2015-05-30+%25E4%25B8%258B%25E5%258D%258811.25.23.png)
Below is a results after running the Python program in MacBook.
$ python ./DesignPattern/Adapter/TreeDisplay.py
Test2() tests the new Adapter.
The better style of the directory tree displays as below after running the Python program.
The below class diagram summarizes the implementation.
-Count
No comments:
Post a Comment