Review design pattern (6)-stage Summary 1

Source: Internet
Author: User

 1. Preface

InArticleBefore getting started, I wrote some nonsense and wrote five articles about the design pattern.

<1> review the design model (1) -- enjoy the metadata model (flyweight)

<2> review the design model (2)-Bridge)

<3> review the design model (3)-chain of responsibility (Responsibility)

<4> review design model (4) -- factory Model

<5> review the design model (5)-What I understand as "Abstract Factory"

The following article is a periodic summary and reflection of the above five articles.

First of all, I would like to thank winter-CN for pointing out one error after another in the design mode application. Without him, I will continue to make many mistakes.

In addition, I would like to thank many park friends in the blog Park for their encouragement and the courage to write it again.

2. Step into question 1-enjoy the Yuan Model

The sharing mode uses the sharing technology to effectively support a large number of fine-grained objects, solving the memory overhead problem caused by a large number of objects.

When to use the metadata mode:

Here I will add two important concepts: inner state and external State. The internal state is shared by all objects in the shared object, and the external State is passed by external callers.

Here we use the word document example to illustrate these two concepts, turn around and return to gof again:

In an article, there are only 26 letters in total, but this article may contain tens of thousands of words. At this time, we can solve the problem using the metadata mode.

In this case, what is the internal status? What is an external state?

The inner state is shared by all callers, that is, the 26 letters.

What about the external status? That is, the font of each character cannot be different. It may be different.

For exampleCode:

 Class  Character { Char C; Font F; Private  List < Font > Fontlist; Public  Font F { Set { Int I = 0; For (I = 0; I <fontlist. Count; I ++ ){ If (Fontlist [I]. Equals ( Value )){ This . F = fontlist [I]; Break ;} Fontlist. Add ( Value ); F = Value ;}}} Class  Font {String Fontname; String Color; Int Size; // And so on; }

3. Metadata-original article reflection

In my original article, review the design pattern (1) -- In flyweight, where did I go wrong?

Return to the example of my QQ chat:

In the chat object, I have three fields: String boyname, string girlname, string content.

In this article, I use boyname + _ + girlname as an internal state, and content as an external state.

On the surface, there is no error, but what is the actual error?

In the usage condition of the object meta mode, if you delete the external state of an object, you can replace many group objects with a relatively small number of shared objects. In this case, you can consider the metadata mode.

The following is an example of a gof26 letter: 26 letters, that is, 26 groups. Each letter may have a different font, that is, each group has several objects. If we delete the font external conditions, the 26 groups can be represented by 26 objects.

Well, let's look at my example: boyname + _ + girlname as the inner state: I think there is no error. Although in the gof mode example, 26 groups have been completed, however, in the QQ chat example, I dynamically add groups. I think this is not necessary.

The problem with this project is that the external content status. We know that each chat content is different from each other, that is, content. I use this content as the external content status, there is no sharing and reuse significance.

Continue to talk with the above group concept. Suppose there are currently 100 couples chatting with men and women, then according to boyname + _ + girlname, all objects can be divided into 100 groups, because the content of each group of objects is different, that is, this content cannot be reused every time it is sent back to the server. Therefore, there are 100 objects in the 100 groups, and the 100 objects remove the external status, or 100 objects.

Therefore, this example is not suitable for the metadata sharing mode.

4. Enjoy the metadata model-struggling (what does enjoy the metadata model ?)

Here, we declare that this is a dead-and-struggling area, so here is just my opinion. It is purely nonsense. Please trust it in your discretion. Incorrect understanding. Please kindly advise.

What is the metadata mode? The metadata mode supports a large number of large-granularity objects through the sharing technology, reducing storage overhead.

We still need to consider the application of string in the original text. This is a standard metadata mode, and I don't think this is a question. So what is string shared? String object. The Intrinsic State is the value of this string. What about the external state? It is actually the address he points.

We first use the enjoy meta mode to implement string.

 Class  String1 {String Content; String Address; Private  List < String > Stringlist; Public String Address { Set { Int I = 0; For (I = 0; I <stringlist. Count; I ++ ){ If (Stringlist [I]. Equals ( Value )){This . Address = stringlist [I]; Break ;} Stringlist. Add ( Value ); Address = Value ;}}}

Indeed, each content corresponds to an address and shares the address. But let's take a look at whether several of the same content correspond to the same address? That is to say, when the content is fixed, its address is fixed.

In this case, it is better to write: dictionary <string, string> DIC = new dictionary <string, string> (); DIC. Add (content, address ).

Why not?

Therefore, I think that the metadata-sharing model is a macro concept. Nothing is important. In short, there is only one purpose to save memory. So far!

5. Step into question 2-Bridging Mode

In the bridge mode, gof explains this: separates abstract parts and implementation parts so that they can all change independently.

I personally think this is a great embodiment of a combination that is superior to the inherited design principle.

6. Bridging Mode-original article reflection

Is my example of bridging mode? It is clear, no!

A conceptual explanation is mentioned here. Gof said that abstraction and implementation are separated. What is the implementation here?

This implementation is not the implementation of an interface that we usually call "Implementation. It can be explained as follows:

For the same work () methodProgramFor workers, work is programming, for workers, operating machines, and for teachers, teaching.

The Code is as follows:

  interface   iwork  { void  Work () ;}< span style =" color: blue "> class   programer :  iwork  { # region  iwork member  Public void  Work () { console . writeline ( "write code" );} # endregion }< span style =" color: Blue "> class   teacher :  iwork  { # region  iwork member  Public void  Work () { console . writeline ( "" );} # endregion }

Then inject the specific implementation into our application class. For example, the injection method above is extremely setwork (iwork work ){};

In my original article, where is the incorrect Photoshop example?

I am wrong in understanding implementation errors, so my example is just a simple combination better than inheritance.

That is to say, the bridge mode is only an application with better combination than inheritance, but not equal to the application with better combination than inheritance. The purpose of the bridge mode is to solve the class explosion problem caused by implementation.

7. Bridge Mode-Jedi counterattack winter-CN (does the bridge mode have only one bridge ?)

I declare again that this is the Jedi counterattack zone. Therefore, this is just my personal opinion. It is a nonsense. Please trust it in your discretion. Incorrect understanding. Please kindly advise.

 

Can this happen?

For example, we want to implement QQ games in Windows and Linux, Windows and Linux. notepad implementations in Windows and Linux are different, * implementations in Windows and Linux are different, so they should be extracted to form a bridge.

Then our original code is:

ClassProgram{PrivateImplementorI;Public voidSetimplementor (ImplementorI ){This. I = I ;}Public voidRun () {I. Run ();}}Abstract classImplementor{Public abstract voidRun ();}

But here we are doing this:

 Class  Program { Private  Implementor1 I1;Private  Implementor2 I2; Private  Implementor3 I3; Public void Setimplementor ( Implementor1 I1, Implementor2 I2, Implementor3 I3 ){ This . I1 = I1; This . I2 = I2; This . I3 = I3 ;} Public void Run1 () {i1.run ();}Public void Run2 () {i2.run ();} Public void Run3 () {i3.run ();}} Abstract class  Implementor1 { Public abstract void Run ();} Abstract class  Implementor2 { Public abstract void Run ();} Abstract class  Implementor3 { Public abstract void Run ();}

 

It makes sense that everyone can build a bridge based on their own needs in front of the house. Why do they need to specify that there is only one bridge in front of each house?

In addition, you can review the design pattern (2)-Bridge pattern (BRIDGE) as a review design pattern (2)-combination is better than inheritance to understand, it will also be a good article!

8. Supplementary description-responsibility chain model

For the responsibility connection mode, I just want to add one thing: the difference between the responsibility chain and the linked list.

In this article, I mentioned the differences between the responsibility chain and the linked list, And I will add them here.

Recently, I am writing an article about workflows. We know that workflows are classified into sequential workflows and state machine workflows. In fact, a workflow is a complex model with mature frameworks and graphical representation of the responsibility chain.

Of course, the responsibility chain should also support state machines.

Here I will reference a diagram of my state machine workflow:

 

That is to say, in the state machine, A. Next = B; B. Next = A; may occur, but this will never happen in the linked list.

The reason is that each node of a responsibility chain can lead to multiple next pointers. For more information, see the extension of the responsibility chain in my original article-tree chain structure.

9. Let's talk about the factory.

Here, I just want to explain my original article.

The first is the stateless mode I mentioned in the test code and even in the full text. Is that no mode really no mode? In fact, we should better understand him as a pseudo factory. What exactly does the factory do?

This is an abstraction. The create in this factory is actually an abstraction of the object to be created.

In this case, the process of creating a product is extracted and abstracted separately. It can be said that it is a simplified version of the factory, or a simplified version of the simple factory.

What are the limitations of this stateless model? When we have a product that uses both producta and productb, this situation is very common.

At this time, my simplified version of "pseudo" factory will not be able to meet the demand.

In short, what kind of design depends on what kind of change!

10. Step into question 3 -- Abstract Factory

The problem solved by the Abstract Factory is a product family problem.

What is a product family? I have explained some problems before. Product families cannot be matched at will. Abstract factories only provide effective combinations, and random combinations are not the scope of problems that abstract factories can solve.

11. Abstract Factory-Reflection on the original article

In the original article, I gave an example of a car, which is not an example within the scope of an abstract factory.

Back to our previous questions about free and effective combinations.

There is no mutual constraint between the paint color, engine, and wheel model. That is to say, whatever the color of paint, you can configure any type of engine. That is to say, if there are 3 paints and 4 engines, there will be 3*4 = 12 combinations. This is called random combination and cannot be solved by abstract factories.

For example, the relationship between the operating system and application software is reasonable. For example, there are two types of systems, windows, Linux, and three types of application software: QQ, office, * ** (for example, this operation can only run on Linux). Therefore, there are only Windows + QQ, Linux + QQ, Windows + office, linux + *** four combinations, which are called effective combinations rather than arbitrary combinations. This is the scope of abstract factory solutions.

12. Abstract Factory-struggling

Aside from the unreasonable CPU and video card examples in my original article, you can refer to other examples and chapters. I personally feel that my point of view is unreasonable.

13. Summary

There are a lot of problems, many models, maybe we are not commonly used now, take the current C #, Java and other languages, they can do too much for us.

For example, the iterator mode and observer mode have been much simplified in C # and are even discarded.

However, our learning model is a design concept and a kind of thinking. First, we can discard those advanced features and simply think about object-oriented.

Then we can think about how we can improve the design pattern with today's language features (such as C #). In our practical application, how to Use the mode + mode to make better use of this mode.

Finally, let's talk about it:

 

Dudu, the leader in terrylee's ranking ........................

 

 

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.