Talking about Design Patterns

When I first learned about design patterns, I always thought it was a very deep thing, and it was the place where development architects were awesome, and these design patterns were everywhere in many source codes, such as Java source code, Android source code, etc. So I began to look for books on design patterns. After reading it, I found that the basic idea of ​​design patterns is very simple. Most of the design patterns are various designs using a polymorphism of Java, and then they will Discovering the original design pattern is such a fun thing. The most commonly used is the upward transformation. The so-called upcasting means that the object can be used either as its own type or as its base class type . And the practice of treating a reference to an object as a reference to its base class is upcasting.

Here is a brief introduction to a design pattern - the strategy pattern. All it has to do is define a strategy interface, implement two subclasses that include the implemented methods, then define a role class Context, declare a constructor with parameters, the constructor parameters are declared as interface classes, and then in the final Strategy strategy = new ConcreteStrategy1() is called in the calling class, and the upward transformation is performed here. Context context = new Context(Strategy), context.doAnything(), here is the specific method of ConcreteStrategy1, which implements the strategy pattern. .

Another pattern to mention is the adapter pattern. I see that some people also call it translation mode. In fact, this mode is to let two classes with different interfaces but similar functions do the same thing through a translation class. Because our principle is to be closed to modification and open to extension, we try not to modify the existing interface class, but to extend a new class. There are two types of adapter patterns, class adapter and object adapter. Among them, the class adapter is connected to the Adaptee class using the inheritance relationship , and the object adapter is connected to the Adaptee class using the delegation relationship . In the object adapter, in the Adapter class, the surface is to call the request method, but actually the specificRequest method of Adaptee is called. Of course, it is obvious that Java is designed to use less inheritance, so we should use more object adapters and reduce the use of class adapters.

Reference address: Adapter mode (Adapter): class adapter, object adapter

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325387351&siteId=291194637