Tuesday, July 13, 2010

Art of Object Design on MPDay

Last sunday, I gave a speaking as a speaker in the msup open day conference in ShangHai. My topic was Art of Object Design, mainly focused on low level design in the software development lifecycle. I want to dig the essence of software design deeply. As we know, there are amount of principles and patterns for software achitecting and design. It is impossible to grasp all of these. You know more, understand less. Through by analyzing the essence and core idea of design patterns and architecture patterns, I summarized seven principles including Reusability, Extensibility, Separation, Change, Simplicity, Consistency, Indirection.



The most evil enemy in the software development is repeat. It results in developing repeaded so that we cann't reuse some components and functions effeciency. It would generate the bad smell of solution sprawl. How to avoid repeat? We must keep the objects to be fine-grained and high cohesion. It needs to use encapsulation reasonable.  We can improve the reusability of software based on three-level: method, class and module. For instance, we can extract method or super class, define some helper class, devide modules by dependency.  The following diagram demonstrates how to apply template method pattern to reuse some codes in JUnit Framework:



The excellence structure of software should be extensible so that we can add functions without modifying the source code. There are two meaning of extensibility. First we don't add new features to expose it, that is an extensiblity internal.  We can decorate the target object or control it by providing a proxy object. Second is an extensiblity external. The inheritance and composition are the common approach. Of course, we don't forget the abstraction. For example, the Runnable interface in Java supports the extensiblity of business when you want to write the multi-thread program.

class MyThreadStart implements Runnable {
     public void run()  {
        //do something

Thread controller = new Thread(new ThreadStart());


Concern separation is the most important principle for architecting architecture. The classic patterns which embody the concern separtion are layered architecture pattern and MVC pattern. The key element of separtion is that seperating changeful resposiblity from changeless object. That is core value of SRP also. Of course, we must focus on the collaboration between objects too. The following diagrams list my opinon about separation:  imageimage image


One of the inevitable thing is change in the software development. So we must find the change point when we analyze the requirement. In my experience, I think these points always are changed as below:

1. Business Rule (Solution: Specification Pattern)

2. Algorithm and Strategy (Solution: Strategy Pattern)

3. Command and Request (Solution: Command Pattern)

4. Hardware Environment (Solution: Abstraction Layer or Gateway Pattern)

5. Protocol and Standard (Solution: Metadata)

6. Data Schema (Solutioin: Encapsulate Data or Information Hide)

7. Business Flow (Solution: Customize Workflow)

8. System Configuration (Solution: Metadata or Database)

9. User Interface and Presentation (Solution: Layered Pattern, MVC Pattern)

10. External Services (Solution: Abstraction Layer or Service Facade)


There are two important principles we have always to keep in mind. These are KISS(Keep it simple and stupid) and YAGNI(You aren't gonna need it). How to simplify the complex implemention? We can use encapsulation to hide the complex implementation. The abstraction is the other way. It might unify model, and elimilate the difference. As architect, most of them want to pursue the perfect solution. It's wrong! Many anti-patterns related with this idea, such as Analysis Paralysis, Accidental Complexity.


So called "Consistency" includes interface, format, invoking way and solution. If we have consistant interface, so the implementation can be substituted. If we have consistant format, we can infer the overall design from the part of design. Consistant invoking way would help client understand the intention of service provider. Consistant solution is the basic of cooperation of team. For example, we can achieve the consistant invoking way by using composit pattern:

image Indirection

David Wheeler said:"All problems in computer science can be solved by another level of indirection". Indirection in software programming is achieved by delegation, abstraction and collaboration. Indirection can reduce dependence, hide the detail and simplify the client call. Many pattens are reflect the indirection thought, such as Facade Pattern, Mediator Pattern, Adapter Pattern, Strategy Pattern, Service Locator Pattern.

No comments: