The first chapter of the "grinding design mode"

Source: Internet
Author: User
Document directory
  •  
  •  
  •  
Chapter 1 design model basics 1st
What is the design pattern?




1.1.1 what is a mode?


Literally, a model is the meaning of a model or template. A formula is the meaning of a method or method. In combination, the so-called mode can be used as a model or template. Simply put, it can be used as a sample method or method, similar to the example that everyone is familiar.



1.1.2 concept of Design Pattern

According to the above understanding, the design pattern refers to the design template, that is, the design method or method.

1. The design mode isSolution

According to the above definition of the design pattern, in the final analysis, the design pattern is a number of solutions.

The so-called solution is the solution, that is, the method or method to solve the problem. The Solution book is a document formed after the solution is documented.


So can we put it another way: the solution is the design model? Obviously, this is not the case. Why? Because there are still some acronyms before the solution, only solutions that meet these conditions are called design patterns.

2. The design mode isSpecific problems
Solution

Why is it necessary to limit the design pattern as a "specific problem" solution?


Limiting "specific problems" indicates that the design model is not a panacea and can be solved without any problems. Generally, a design model only solves one or some specific problems and cannot cure all diseases.

Therefore, do not be superstitious about the design model, nor use the design model in full. The design model cannot solve so many problems. It is only a solution to "specific problems.

3. The design mode isRepeat
Solutions to specific problems

So why are these specific problems repeated?


Only when these specific problems "repeat", it is meaningful to summarize solutions for these problems. Because only solutions that summarize these problems can reuse these solutions when these problems occur again, rather than looking for solutions from the beginning.

4. The design mode is used to solveIn a specific environment
, Repeated solutions to specific problems

Why is it limited to "in a specific environment?

Any problem occurs in scenarios and cannot be discussed without the environment. Because in different environments, even the same problem occurs, the solution may not be the same.

5. The design mode isVerified
What are the restrictions for solutions to repeated and specific problems in a specific environment?

Each person can summarize some solutions that are used to solve repeated and specific problems in a specific environment, but not all of them summarize the solutions as design patterns, these solutions should be verified by sufficient applications and recognized by everyone. Only a verified solution is considered a design model.


If there is not a verified solution, if it is also regarded as a design model and is heavily reused by everyone, what if there is a problem with this solution? In this case, errors occur in all applications and must be modified. This reuse is not necessary.

6. Why should we emphasize "in software development"

Note:

 


The reason is very simple, because the content to be discussed next is the design pattern in software development, so here we limit "in software development ".



1.1.3 understanding of design patterns


From the introduction of the Design Pattern concept, we can see that there is nothing magical about the design pattern. Next, Let's explain the design pattern to help readers better understand it.

N design pattern is a solution to some problems.

To understand and master the design model, the focus is to understand and master these methods, and then to further deepen the ideological aspects embodied in these methods, absorb and digest the Thinking Modes embodied in the design patterns, and integrate them into your own thinking.

N the design model is not just imagined, but accumulation and summarization of experience.

Theoretically, the design model is not necessarily the best solution, and there may be better solutions than the design model. That is to say, the design model is relatively good and not optimal, only better.


 

 

N the design model is not static, but in constant development.

This book only discusses the classic design patterns recorded in the gof book, but it does not mean that only these design patterns are supported. Because the development of design patterns has never stopped since the introduction of design patterns into software.

N the design model is not exclusive to the software industry. all walks of life have their own design models.

For example, the pharmaceutical industry has its own design model. Assume that a person feels

Come on, go to the pharmacy to buy the cold medicine, this cold medicine is a good embodiment of the design model.

U
Verified: there will be a lot of verification and experiments before the drug is listed to ensure the safety of the drug.

U in a specific environment: these medicines are for humans, not other animals.

U re-occurrence: It is precisely because the cold will repeat, the development of drugs is meaningful.

U
Specific problems: cold medicines are only used to solve cold problems and cannot solve other problems, such as foot pain.

U solution: the drug itself is a concrete embodiment of the solution.

After the comparison above, you will find that the pharmaceutical industry's embodiment of the design model is not inferior to the software industry at all. Besides, the design model is not originated from the software industry, but from the construction industry.



1.1.4 Design Pattern history

Design Patterns originated in the construction industry. Alexander, an architect, found and summarized some architectural design patterns.

In 1990s, it was precisely 1995, due to Erich Gamma, Richard
Helm, Ralph Johnson, John
The publication of the design model-reusable basis for object-oriented software co-authored by vlissides officially kicked off the design model of the software industry. The four authors of this book are called the four-person group, some people refer to it as the "Gang of Four", that is, the common gof (gang)
Of four ).



1.2 What are the design patterns?


1.2.1 composition of design patterns

When describing a single design pattern, the design pattern usually consists of the following parts.

N mode name: it is the name of each design mode. It should be remembered and communicated well.

N environment and problems: Describe the specific problems in what scenarios.

N solution: describes how to solve this problem.

N
Effect: describes the effect of a pattern application, as well as possible problems, or issues that need to be weighed during use.

In the Java field, there is another division of the composition of the design pattern, which is divided by the technical field where the design pattern is located and roughly divided into the following categories.

N
Java design pattern: it is usually the design pattern mentioned in gof's design pattern-the basis for reusable object-oriented software.

N
Javaee design mode: The design mode mentioned in Sun's core J2EE design mode.

N
Other fields, including but not limited to: EJB Design Mode, real-time system design mode, multi-thread design mode, and architecture mode.



1.2.2 classification of design patterns


To narrow down the scope, we only discuss the Java design patterns, that is, the 23 design patterns mentioned in the gof book. For these 23 design patterns, gof classifies them into three types.

N
Creation Mode: abstracts the Object Instantiation process to help create an object instance.

N structure mode: describes how to combine classes and objects to obtain a larger structure.

N behavior mode: Describes the assignment of duties between algorithms and objects.

Of course, there are also categories based on other methods, which will not be discussed here.



1.3 learning Design Patterns


1.3.1 why should we learn the design model?

Why should we learn the design model? There are too many reasons. Here are some simple points.

1
The design model has become a "Standard word" for software developers"


When communicating with each other, many software developers only use the name of the design pattern without in-depth description of the specific content. Just as we use idioms in Chinese, when you use an idiom during communication, you will not tell the story behind this idiom.


For example, if developer A encounters a problem and discusses with developer B, developer B may make the following moves: use the "XXX mode" (XXX is the name of a design mode. If developer A doesn't understand the design pattern at this time, they won't be able to communicate.

Therefore, a qualified software developer must master the "standard vocabulary" of the design model ".

2
Learning the design model is a shortcut to improving personal technical capabilities


The design model is the accumulation of experience of many predecessors, most of which are relatively good solutions. Many problems are typical and representative.

By studying the design model, you can learn the experience of many predecessors, absorb and understand their design ideas, and master their solutions to problems, which is equivalent to standing on the shoulders of these giants, our personal technical capabilities can be quickly improved. Although there are some difficulties in learning the design model, it is definitely a shortcut to quickly improve personal technical capabilities.

3
No repeated Design


Design Patterns are solutions to specific problems. When we face these problems again, we don't have to solve these problems from the ground up, just reuse these solutions.


In most cases, this may be a better solution than solving these problems from the beginning. First, you may not be able to find a better solution than the design mode. In addition, you can save a lot of time by using the design mode, you can save time on other problems that need to be solved.



1.3.2 layers of learning Design Patterns

There are roughly three levels of learning design patterns.

1
Basic entry level

It is required to be able to correctly understand and master the basic knowledge of each design mode, and to identify the scenarios, problems, and solutions used to solve the problems, and can apply the corresponding design pattern in the actual program design and development.

 

2
, Master level

In addition to basic entry-level requirements, it also requires deformation of the design mode based on actual application scenarios.

In fact, in actual development, we often encounter some different situations from the application scenarios of the standard mode. To use the design mode properly, we need to make proper changes to them, instead of applying it stiff. Of course, the premise of deformation is to be able to accurately and deeply understand and grasp the nature of the design model,Only by grasping the essence can we ensure correct deformation and use, rather than misuse.

3
Deep understanding and mastery


In addition to having the basic master level requirements, the following are more important:

 
 

Extension

 

 

 

 

 

In complex applications, when a problem is solved, it is likely that a single application may not have a design pattern, but a comprehensive application of many design patterns. For example, in combination with a specific situation, you may need to simplify mode A, then combine part of mode B, and then combine the transformed Mode C ..., To solve the problem.
What's more complicated is that in addition to these design patterns, you may also need to consider the overall architecture of the system, the implementation of actual functions, and the combination of existing functions. This requires that the design pattern should be applied at the level of thought and method rather than the design pattern itself.

 

Prompt

 

To put it simply, basic entry-level applications are suitable for use, which is equivalent to being able to draw images based on the gourd tree and be mechanical. The basic master level is deformation-able, which is more flexible than basic entry-level ones and can be used with proper deformation; deep understanding and mastery can truly absorb the essence of the design model. It is like practicing martial arts to the highest level to understand and master the design model from the perspective of ideas and methods,
"No moves, no moves. To reach this realm, there is not enough development and design experience, and there is not enough in-depth thinking, which is unlikely to be achieved.



1.3.3
How to Learn Design Patterns


Based on the author's experience, the following suggestions are provided for learning the design model.

(1) first, you must adjust your mindset. Do not expect it to be done overnight. Do not be impetuous.

It takes a process to learn and master the design patterns. Different Stages may have different understandings and feelings.

Do not count on books with true design patterns to be simple and interesting.Books are mostly popular books.
It just gives you a brief introduction to the design mode. This is why many friends always feel

Prompt

 

"The fish and the bear's paw cannot have both sides". Therefore, this book tries its best to balance the depth, difficulty, and ease of understanding of the content, we hope that you can truly understand and master the design model with little effort. "Hspace =" 12 "width =" 479 "Height =" 122 "align =" Left ">
"Understand" the design pattern, but do not apply the design pattern in the actual project. It is not enough for you to "understand.

 

(2) Step 1 of learning the design model: accurately understand the functions, basic structures, and standard implementations of each design model, and understand the applicable scenarios and Effects

(3) Step 2 of learning design patterns: in actual development, we try to use these design patterns, and repeatedly think about and summarize whether they are used properly and whether some changes need to be made.

(4) Step 3 of learning the design model: Let's look back at the theory of the design model. with practical model application experience, we will have different insights, consider the application experience while looking at it. For example, what are the essential functions of the design model? How is it implemented? Where can this implementation be applied? How can we combine this design pattern with specific applications? What is the starting point of this design model design? And so on. There are many considerations for the design model from different degrees.

(Step 5: The second and third steps of the repeated learning design model. That is to say, it is used in actual development, and then combined with theoretical thinking, and then applied and thought again.......
Multiple times until the level of basic understanding of the design pattern is reached.

In short, you should pay attention to combining the theory and practice of the design model,Theoretical guidance and practice, in turn, deepen understanding of the Theory
, So that the cycle goes up in a spiral.

As a matter of fact, when we reach the level of basic understanding of the design model, what kind of height can we achieve? It varies from person to person. We need to look at the level of personal thinking and understanding. At this stage, there is only one suggestion, that is, repeated and in-depth thinking, and there is no other way. At the ideological level, we have to rely on "enlightenment.

Organization of 1.4 books
1.4.1 outline of the design model described in this book


Starting from chapter 1, this book describes in detail the 23 design patterns described in gof, the basis for reusable object-oriented software, and the simple factory described in Chapter 3rd, strictly speaking, it is not a standard design model, but a warm-up exercise.

1. Chapter 1 simple factory (not in gof's book)

Provides the function of creating an object instance without having to worry about its specific implementation. The instance to be created can be an interface, an abstract class, or a specific class.

2. Chapter 1 Appearance Model (the gof works are divided into structural models)

Provides a consistent interface for a group of interfaces in the subsystem. The facade mode defines a high-level interface, which makes the subsystem easier to use.

3. Chapter 1 adapter mode (the gof works are divided into structural models)

Converts an interface of a class to another interface that the customer wants. The adapter mode allows the classes that cannot work together due to interface incompatibility to work together.

4. Chapter 1 Singleton model (Division of gof works into creation models)

Ensure that a class has only one instance and provides a global access point to it.

5. Chapter 1 factory method model (the gof works are divided into creation models)

Defines an interface for creating objects, so that the subclass decides which class to instantiate, factory
Method delays the instantiation of a class to its subclass.

6. Chapter 2
Abstract Factory model (the gof works are divided into creation models)

Provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes.

7. Chapter 1 generator model (the gof works are divided into creation models)

Separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.

8. Chapter 1 prototype model (the gof works are divided into creation models)

Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object.

9. Chapter 1 intermediary model (the gof works are divided into behavior models)

An intermediary object is used to encapsulate a series of object interactions. The intermediary makes it unnecessary for objects to explicitly reference each other, so that the coupling is loose and the interaction between objects can be changed independently.

10. Chapter 1 agency model (the gof works are divided into structural models)

Provides a proxy for other objects to control access to this object.

11. Chapter 1 Observer Model (Division of gof works into behavioral models)

Defines a one-to-many dependency between objects. When the status of an object changes, all objects dependent on it are notified and automatically updated.

Chapter 12 Command mode)

Encapsulate a request as an object so that you can parameterize the customer with different requests, queue requests or record request logs, and support auditable operations.

13. Chapter 1 iterator model (divided into behavior patterns in gof's Works)

Provides a method to access each element in an aggregate object sequentially without exposing the internal representation of the object.

14. Chapter 1 composite mode (the gof works are divided into structural models)

Combine objects into a tree structure to represent the "part-whole" hierarchy. The combination mode ensures consistency between the use of a single object and a composite object.

15. Chapter 1 template method model (the gof works are divided into behavior models)

Defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. The template method allows the subclass to redefine certain steps of an algorithm without changing the structure of an algorithm.

16. Chapter 1 Policy Model (divided into behavioral patterns in gof's work)

Define a series of algorithms, encapsulate them one by one, and make them replaceable. This mode allows algorithms to change independently of customers who use it.

17. Chapter 1 state model (divided into behavioral patterns in gof's Works)

Allows an object to change its behavior when its internal state changes. The object seems to have modified its class.

18. Chapter 1 Memorandum model (the gof works are divided into behavioral models)

Capture the internal state of an object without compromising encapsulation, and save the state outside the object. In this way, the object can be restored to the previously saved state.

19. Chapter 1 interest model (the gof works are divided into structural models)

Use the sharing technology to effectively support a large number of fine-grained objects.

20. Chapter 1 Interpreter mode (the gof works are divided into behavior models)

Given a language, it defines a representation of its syntax and an interpreter that uses this representation to interpret sentences in the language.

21. Chapter 1 decorative model (the gof works are divided into structural models)

Dynamically add some additional responsibilities to an object. The decoration mode is more flexible than the subclass generation function.

22. Chapter 1 responsibility chain model (Division of gof works into behavior models)

So that multiple objects have the opportunity to process the request, so as to avoid coupling between the request sender and the receiver. Connect these objects into a chain and pass the request along the chain until an object processes it.

23. Chapter 1 bridging model (the gof works are divided into structural models)

Separate the abstract part from its implementation part so that they can all change independently.

24. Chapter 1 visitor mode (the gof works are divided into behavior types)

Indicates an operation that acts on each element in an object structure. It allows you to define new operations that act on these elements without changing the classes of each element.

1.4.2 description structure of each mode

1.
Scenario Problems

1) A Practical Application

Describe a problem in a specific scenario through a practical application, that is, the problem to be solved by the mode.

2) Non-modal solutions

The first example does not use the mode. How can this problem be solved.

3) What is the problem?

Analyze the problems in the solution that does not need to use the mode, and find a better solution.

2.
Solution

1) solve the problem in a certain mode

First, define the mode, and then describe the method to solve the problem above.

2) schema structure and description

Use UML to draw a schema and describe the participants.

3) sample code of the Mode

The sample code for the basic implementation of each mode is provided as accurate as possible, which is a standard reference implementation of the mode.

4) use mode to rewrite the example

The usage mode is used to overwrite the example implemented by the previous "NO mode". This is a practical application example of the mode, which is also easier for you to compare and learn, and to see how to use the mode to solve the problem, and the benefits of the use mode.

3.
Mode description

1) recognize a model

It mainly describes the various knowledge involved in the mode. It usually includes the function of the mode, its understanding of each part, its implementation, and the sequence of running the mode.

2) Discussion and examples of key and difficult functions, or integration with practical applications

It provides in-depth explanations and examples based on some key difficulties of the mode or the combination of the mode and the actual application.

3) advantages and disadvantages

The advantages and disadvantages of the mode are discussed so that you can try to use the advantages of the mode in practical applications, and avoid the disadvantages of the mode.

4) Thinking Mode

First, summarize the nature of the model, then think about the model from the design perspective, and then give the appropriate use mode.

5) related models

Describes the relationship with other models and the similarities and differences with other models.

 

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.