The art of Object Design

Source: Internet
Author: User

Last weekend, masbo held the Asia Pacific Software R & D team management annual meeting in Shanghai. as a lecturer, I attended the architecture breakout session. My speech is titled "The Art of Object design". The word "art" is a bit big and mysterious, but I really hope that design can be used as an art, combined with engineering, not only focus on effectiveness, but also ensure the quality of software, code elegance. In this speech, I hope to deeply explore the essence of the so-called design. This is what I feel. In the design field, our predecessors have summarized too many ideas, principles, and patterns for us. These contents are full of resources that many programmers cannot exhaust. The more you learn, the less you feel. In this case, ignorance is not conducive to the improvement of skills. Therefore, I try to grasp some of the core values of the design, which is the seven "weapons" I have summarized: reuse, expansion, separation, change, simplicity, consistency, and indirect.

Reuse

The biggest enemy of software development is repetition. It will lead to repeated development, ineffective reuse, and spread of solutions. Methods To avoid duplication include maintaining fine-grained objects, high cohesion, and reasonable object encapsulation. We can improve software reusability at the method level, category level, and module level. For example, we extract methods or classes, define helper classes, and divide modules according to dependencies. The following class diagram uses the template method mode in JUnit Framework to reuse some Logics:Extension

The excellent software structure can support expansion well without modifying the source code. Scalability indicates two meanings. One is internal expansion. It does not add new functions to external interfaces, but simply describe the responsibilities of objects or control them through proxy objects. The second is external extension. We can use inheritance and combination to expand object functions on the basis of reuse. Of course, the most important method is to use abstraction. For example, the Runnable interface provided by Java can effectively support business expansion in multi-threaded programming:

Class MyThreadStart implements Runnable {
Public void run (){
// Do something
}
}

Thread controller = new Thread (new ThreadStart ());
Controller. start ();

Separation

When building an architecture, the most important design principle is separation of focus. Classic architecture models, such as layered mode and MVC mode, are the embodiment of separation of focus. The key element of separation is the change and change of separation, and the core value is SRP (single responsibility principle ). At the same time, we also need to consider the collaboration between objects after separating them. Demonstrate my views on separation:Change

In software development, changes are inevitable. When analyzing requirements, we must look for changes. Based on my experience, these features often change:
1. Business Rules (solution: Rule Mode)
2. algorithm Policy (solution: Policy Mode)
3. Command request (solution: Command mode)
4. Hardware Support (solution: Entry Mode)
5. protocol standards (solution: Metadata)
6. Data Format (solution: Data encapsulation)
7. Business Process (solution: workflow customization)
8. system configuration (solution: Metadata and database)
9. interface performance (solution: hierarchical mode and MVC mode)
10. External Services (solution: Service appearance)

Simplicity

To keep the software simple, you must keep in mind two principles: KISS (to keep the software simple and easy to use) and YAGNI (to only implement the functions you actually need, rather than simply adding functions ). How can we simplify complex implementations? Encapsulation can hide complex implementations, while abstraction can unify models to eliminate different functions. As an architect, it is wrong to always pursue a perfect solution. Many anti-patterns really come from this, such as analysis paralysis, unexpected complexity, and cargo worship (using patterns without understanding ).

Consistent

The so-called "consistency" includes the consistency of interfaces, forms, and calls with the solution. If the interface is the same, the implementation can be replaced. If the form is the same, you can see that it is the same. If the call is the same, the client can access it transparently, it is the cornerstone of teamwork. For example, we can use the merging mode to achieve consistent calls:

Indirect

David Wheeler said: "Most Problems in computer science can be solved by adding an indirect layer ." Cheng Zai Si Yan. In software development, it can be reflected indirectly through delegation, abstraction, and collaboration. Indirectly, you can reduce dependencies, hide details, and simplify client calls. Many modes reflect indirect ideas, such as the facade mode, the mediation mode, the adapter mode, the policy mode, and the service locator mode.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.