My design patterns (II)

Source: Internet
Author: User

Http://35java.com/zhibo/forum.php? MoD = viewthread & tid = 251 & extra = Page % 3d3

 

3, Solve the problem of excessive dependency implementation.
 
In the test. Java test class of this example, the object is obtained through fruit Apple = new Apple ();, resulting inProgramToo many dependent implementation classes are over-coupled with the Implementation classes. after learning the design model, we can consider using the factory model to achieve this.CodeMake the following improvements: add the factory class fruitgardener. java. The factory method of this class is as follows:
Public
StaticFruit Factory (string fruittype)... {
If(Fruittype. inclusignorecase ("Apple"))... {
Return
New Apple ();
}
Else
If(Fruittype. inclusignorecase ("Strawberry"))... {
Return
New Strawberry ();
}
Else
... {
Return
Null ;
}
}

After the fruit factory class is added, the test class also needs to be modified accordingly. The modified main method of test. Java is as follows:

Fruit Apple = fruitgardener.Factory("Apple ");

Fruit strawberry = fruitgardener.Factory("Strawberry ");

 

After the corresponding modification, the test class greatly reduces the dependency on the fruit implementation class, from direct new implementation class to obtaining the desired instance through the input string. The factory mode is widely used, for example, spring, which is currently booming, uses the factory model in many places. It is itself a large Bean factory, but it has made greater improvements in code, each implementation class can be set through the configuration file.

III,Design Mode----Step from excellence to excellence

From the above three examples, we can see that our code is optimized by using the design pattern. There are many examples of this in our daily coding process. When we first started learning coding, we could still say that we were writing this code. But as our experience grew, we needed to go further, the existing design model provides us with a good solution to solve most of the problems. Of course, in practice, we can even explore new design models to solve certain problems.

The learning design model is not achieved overnight. Many people sigh that the design model is very good, but it is rarely used in their coding design career. I think the main reason is that the learning of the design model is not enough, I haven't changed it into something in my own head. So when the problem changes to face, I cannot understand the problem because I cannot correctly analyze the problem and understand the problem, of course, it is impossible to solve the problem well.

Programmers who have not yet learned design patterns or know little about them, learning design patterns, it will make you a good programmer (CODER) become a superior software designer ).

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.