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 ). |