Introduction to the design model (II)

Source: Internet
Author: User

It's so fast. I don't know if I want to spend the weekend again. Let's add "learning design patterns (below. In "learning design patterns (medium)", we talked about keeping abstract thinking. Next, let's talk about the fourth point. Let's share it and record your learning history. 4. To learn the design mode, do not take it too seriously. The design mode is a programming idea. It is not a specific code routine. For example, because of the family biography, we have been exposed to some traditional Chinese martial arts. When I talk to people who do not understand traditional martial arts, they always think that traditional martial arts in China are a set of routines and flowers, just for good looking. In their opinion, the fight between the two is not based on your fixed routine, so those martial arts routines cannot be used for actual combat. What is it like? I would like to say that they do not know Chinese traditional martial arts very well. In fact, the traditional martial arts is not only a routine, but also an aid to the physical coordination and other capabilities of the traditional martial arts. It is mainly in thinking. For example, if you do not change your mind, you can mobilize your full body of power to defend against attacks. Learning traditional martial arts is to train those martial arts ideas on the body (of course, I only practice occasionally, but I still have no skills ). The design pattern is also a programming idea or idea in some scenarios. It is not a fixed routine. There are various development scenarios, which requires you to design the code structure properly based on certain ideas. Instead of having to be exactly the same as the code structure of the design pattern described in the book. The design pattern is the things at the thought layer and abstract, so don't worry. It allows you to use it flexibly and reasonably. Don't take it too seriously (I don't know if anyone has the same interface as me, so I think it is necessary to write an interface, and how to inherit the combined call is the correct design pattern) it seems that everything is abstract. Let's take another example to compare it with the responsible chain pattern in the previous article: Scenario: imitating the network model. After the packet first passes through tcp and adds the protocol header, after the http header is added, it is finally transmitted to the other party through the network. Step 1: almost all design patterns need to abstract interfaces or abstract classes, so we abstract an interface 1 public interface IPackageCreator {2 3 public String createPackage () that generates data packets (); 4} Step 2: simply write a class that implements the IPackageCreator interface to generate a message body: Copy code 1 public class PackageBodyCreator implements IPackageCreator {2 3 @ Override4 public String createPackage () {5 6 return "Hello everyone"; 7} 8 9} copy the code Step 3: we need to add a tcp header to the message body and an http header. Consider that the added protocol header may change at any time, for example, what other protocol headers need to be packaged. We use the decoration mode for decoration. Even if the demand changes, we can freely combine the functions we want. All the decorators need to have a specific decoration object, so we extract this object into an abstract decoration class. Since the modifier class is more powerful than the decoration class, it should not make the client feel the difference before and after the decoration, so this abstract class implements the IPackageCreator interface: copy code 1 public abstract class AbstractDecorator implements IPackageCreator {2 3 protected IPackageCreator creator; 4 5 public AbstractDecorator (IPackageCreator c) {6 this. creator = c; 7} 8 9} copy the code Step 4: now we can write a specific modifier class, this is Two decorative classes used to add the tpc protocol header and http protocol header: Copy code 1 public class PackageTcpCreator extends AbstractDecorator {2 3 public PackageTcpCreato R (IPackageCreator c) {4 super (c); 5} 6 7 @ Override 8 public String createPackage () {9 10 return "tcp Header:" + this. creator. createPackage (); 11} 12 13} copy code 1 public class PackageHttpCreator extends AbstractDecorator {2 3 public PackageHttpCreator (IPackageCreator c) {4 super (c ); 5} 6 7 @ Override 8 public String createPackage () {9 10 return "http header:" + this. creator. createPackage (); 11} 12 13} copy Step 5 of the Code: Next let's take a look at the compilation of the client code: Copy code 1 public class Main {2 3 public static void main (String [] args) {4 receiver creator = new PackageBodyCreator (); 5 IPackageCreator tcpDecorater = new receiver (creator); 6 IPackageCreator httpDecorater = new PackageHttpCreator (tcpDecorater); 7 System. out. println (httpDecorater. createPackage (); 8} 9 10} the entire code has been copied. Is this mode quite familiar? It's just like inputstream is a BufferedInputStream layer, and then another DateInputStream layer. They are all decorative models. But compare it with the responsibility chain model in "learning design patterns (medium. There is a chain, right? There are interfaces, abstract classes, and specific implementations. Will it be a bit confusing. It may be a bit. How can all design patterns have interfaces and inherit abstract classes? Many of them will also be the reference attributes of one object with another interface. This is because the purpose of the design pattern is to facilitate expansion. In order to expand, you need to rely on abstraction rather than specific objects. Therefore, many design patterns have interfaces, inheritance, and other relationships. If we deliberately remember what interfaces to create and how to inherit them when learning the design model. Then we will be confused by most of their similarity. For the responsibility chain mode and decoration mode mentioned above, we only need to understand that the decoration mode is to enhance the functions of existing objects, and the responsibility chain mode is to process tasks one by one chain. You need to be flexible and variable according to the application scenario. 5. Learn the design mode. The most effective design mode for multi-read open source code is abstract. The best way to learn is an example. Although there are many examples in the Design Model Book, most of the examples are "simple" (there are many issues that need to be considered in actual application scenarios ). So learning to read those excellent open source code is the fastest way to quickly master and flexibly apply the design pattern. It may be a painful process to read the source code at the beginning. But when you start, it's easy to find some patterns. Everything starts with hard. Today, two weeks later, you must be better than you are now!

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.