Evolutionary architecture and emergent design: combinatorial methods and SLAP

Source: Internet
Author: User
Tags stmt

Description: How to find hidden designs in a stale code base? This article discusses two patterns that are important for the code structure: combinatorial methods and a single abstraction layer. Applying these principles to code helps to find previously hidden reusable assets, helping to abstract existing code into a mature framework.

In the first two phases of this series, I discussed how to use test-driven development (TDD) to help you incrementally discover your design. If you start a new project from scratch, this method works very well. But the more common scenario is that you already have a lot of imperfect code in your hand. How can I find reusable assets and hidden designs in a stale code base?

This article discusses two very mature patterns that can help you refactor your code to look for reusable assets: combinatorial methods and a single abstraction layer (SLAP) principle. Well-designed elements already exist in your code; You can simply find hidden assets that you've created by using tools.

Combination method

Technology is changing very quickly, which has a bad side effect: Developers often ignore software knowledge. We tend to think that something a few years ago must have gone out of fashion. This is certainly wrong: many old books can still provide knowledge that is important to developers. One such classic is Kent Beck's Smalltalk best Practice Patterns. As a Java developer, you might ask, "What is the use of Smalltalk books 13 years ago?" "Smalltalk developers were the first developers to write programs in object-oriented languages, and they pioneered a lot of good ideas. One of them is the combination method.

The combined method pattern has three key rules:

Divide the program into methods, and each method performs an identifiable task.

Make all operations in a method at the same abstraction level.

This naturally produces programs that contain many small methods, each containing only a small amount of code.

In the test-driven design, part 1th, I discussed the combinatorial approach when writing unit tests before writing the actual code. Strict adherence to TDD naturally produces methods that conform to the combined method pattern. But what about the existing code? Now, let's look at how to use combinatorial methods to discover hidden designs.

Idiomatic patterns

You may be familiar with the formal design pattern movement, which originates from the designing Patterns of Gang of Four. It describes the common patterns that apply to all projects. However, each solution also contains idiomatic patterns, which are not formal enough, but are widely used. Idiomatic patterns represent common design habits in your code. The real trick of emergent design is discovering these patterns. They include various modes of working from a pure technology model, such as the way transactions are handled in the project, to the problem domain pattern (such as "Always check customer's credit before shipping").

The method of composition composition

Consider the simple method in Listing 1. It connects a database using low-level JDBC, collects part objects, and places them in a list:

Listing 1. A simple way to collect part

public void populate() throws Exception {
   Connection c = null;
   try {
     Class.forName(DRIVER_CLASS);
     c = DriverManager.getConnection(DB_URL, USER, PASSWORD);
     Statement stmt = c.createStatement();
     ResultSet rs = stmt.executeQuery(SQL_SELECT_PARTS);
     while (rs.next()) {
       Part p = new Part();
       p.setName(rs.getString("name"));
       p.setBrand(rs.getString("brand"));
       p.setRetailPrice(rs.getDouble("retail_price"));
       partList.add(p);
     }
   } finally {
     c.close();
   }
}

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.