Design Mode of system architecture skill-one-piece Mode

Source: Internet
Author: User
ArticleDirectory
    • I. Opening
    • Ii. Summary
    • Iii. Outline
    • Iv. Introduction to singleton Mode
    • V. Related application scenarios
    • Vi. Summary
    • VII. Series progress
    • 8. next announcement
I. Opening

In fact, I did not intend to explain some design patterns in the system architecture separately, because many good friends are also concerned about this content, so I want to understand what I usually use in projects

Some common design patterns can be used to give you a brief explanation. Here I will only give you a reference. If the explanation is incorrect or not detailed, please criticize and point out. Design Patterns written by many Daniel in the garden

They are all very classic. I may feel a little confused here, but I decided to write it out, hoping to help and guide beginners. Of course, I want to explain some questions here.

The question or the non-logical content written in a certain place should be pointed out and valuable comments should be put forward.

In software engineering, there are many summative words, such as software =Algorithm+ Data structure and so on. Of course, the algorithm here may refer to some programming methods in software. How can we describe the design pattern?

What about it? Why is there a design pattern? What does it bring? And so on. First, we need to know what the design pattern can bring. Maybe this is the main reason for us to learn about it. If

It cannot bring us more benefits in the process of writing software, so we will not use and learn it.

What is the design pattern?

The design pattern can be simply understood as a perfect solution to a series of problems. We often encounter design function implementation problems in the process of software development, and the design mode is to solve the software design function.

A solution to a certain type of problem encountered during implementation. In general, the functional design problems we encounter during the development of a software function may be problems that our predecessors have encountered very early.

The solution of the design pattern can allow us to avoid detours in the software implementation process, or bring good flexibility and adaptability to our software design.

What does the design pattern bring about?

Design Patterns are derived from practice, and each design pattern contains a problem description, participants involved in the problem and provides a practical solution. The benefits of the design pattern can be simplified through

Note:

Of course, I may not summarize it completely here. Please add it. I will update the content here. Of course

The design model has brought so many benefits, so it is necessary for us to learn the design model. It is also one of the basic skills required for software development and design.

Simple classification of design patterns:

Of course, this can be simply divided into these three categories, which will be explained separately in the process described below. Of course, I am here to create a model

I want to create a model that is also essential for your project? Next I will explain the Creation Mode first.

Ii. Summary

This article will mainly explain the singleton mode in the Creation Mode, because Singleton mode is the simplest and easiest to understand design mode. It is easy to use and easy to use. This article will explain the process below

Singleton mode: The design mode described later will also use this method.

1. What is Singleton mode?

2. Application scenarios of Singleton mode.

3. Use the singleton mode as an example.

4. Summarize the usage of Singleton mode.

Iii. Outline

A. The beginning.

B. Summary.

C. Outline of this article.

D. Introduction to singleton mode.

E. Analysis of related application scenarios.

F. Summary in this article.

G, Series progress.

H. next release notice.

Iv. Introduction to singleton Mode

In this chapter, we will introduce how to use the singleton mode. First, let's take a look at the definition of Singleton mode:

Singleton mode: a common design mode in software design. It is mainly used to control the necessity of a class in an application.ProgramOnly one instance exists.

Sometimes we need to ensure that only one instance of a class exists in the system, which is conducive to Coordinated Control of system behavior. For example, if we use a service like SMS in a system

We may want to use a single SMS service instance instead of Multiple object instances to complete the SMS sending service. In this case, we can use the singleton mode.

This section briefly describes the application location of the singleton mode.

Let's look at several implementation methods of the singleton mode:

The following is an example of the implementation of the two methods.

1. External Control Mode

 

Public class instance {private list <sendmessage> lists = new list <sendmessage> (); Private sendmessage sendinstance; Public sendmessage sinstance {get {return sendinstance;} public void instancemethod () {If (lists. count = 0) {sendinstance = new sendmessage (); lists. add (sendinstance) ;}else {sendinstance = lists [0] ;}}

2. Internal Control Mode

 

Public class instance1 {Private Static sendmessage sendinstance; Private Static object _ Lock = new object (); protected instance1 () {} public static sendmessage sinstance {get {lock (_ Lock) {If (sendinstance = NULL) sendinstance = new sendmessage (); Return sendinstance ;}}}}

There are several points to note here. For the second method, we need to describe the following: first, we need to control the class with only one instance globally. Please define it as a static instance, this ensures that only one instance has

Second, the constructor of this object should be declared as a member of the protection type, which can be blocked and accessed through direct instantiation. In this way, you do not need to know

Implementation Details. Generally, you can control the globally unique access point to meet the above two requirements. Of course, it may have some drawbacks when using this form in the case of multithreading. Of course, we are also simple here.

To explain the corresponding control scheme. The solution is as follows:

 

Public class coolinstance {private coolinstance () {} public static readonly coolinstance instance = new coolinstance ();}

 

Let's take a look. Of course, here is a simple explanation of the principle:

1. First, we declare the constructor as a private constructor, so that we can block external access to internal member functions Through instantiation. All member functions must be accessed through the static member instance.

To complete the access.

2. This sectionCodeDefining public, static, and read-only members is equivalent to executing the structure when the class is used for the first time. Because it is read-only, once constructed, it cannot be modified, so you don't have to worry about security issues.

I believe that the above introduction should basically know the application of the singleton mode. Let's take a look at the actual application scenarios and usage of the project.

V. Related application scenarios

1. Scenario Text message and email sending service

We will use the "cool" method described above to control and provide services for sending text messages and emails.

 

 
Public class coolinstance {private coolinstance () {} public static readonly coolinstance instance = new coolinstance (); /// <summary> /// send a text message // </Summary> Public bool sendmessage (string telnumber, string content) {return true ;} /// <summary> /// send the email /// </Summary> /// <Param name = "content"> </param> /// <Param name = "tomail"> </param> Public bool Sendmail (string content, string tomail) {return true ;}}

Let's take a look at how to complete the call in the call class. For example, we have an order class. When someone places an order, it will send a text message reminder to the seller.

 

/// <Summary> /// order business // </Summary> public class order {public int save () {// generate the order information first, this. initorderinfo (); // The Persistence method of order execution int COUNT = This. add (); // send SMS coolinstance. instance. sendmessage (string. empty, String. empty); // send the email coolinstance. instance. sendmail (string. empty, String. empty); Return count ;}/// <summary> /// initialize order information /// </Summary> private void initorderinfo () {}/// <summary> /// add order information /// </Summary> /// <returns> </returns> private int add () {return 0 ;}}

 

In this way, we have completed the control of the SMS sending service and the mail sending service. It mainly depends on your own business needs.

2. For example, if we provide a system log service or a printing or scanning service, we want to have only one access portal globally, then we can achieve this through this Singleton mode.

 

 
Public class printhelper {# region constructor private printhelper () {} public static readonly printhelper instance = new printhelper (); # endregion # region printing service // <summary> // print the service directly /// </Summary> /// <returns> </returns> Public bool print () {return true;} // <summary> // print preview /// </Summary> /// <returns> </returns> Public bool printpreview () {return true ;}# endregion}

I will not write the corresponding code for the specific call class, which is similar to the above form. The following describes more special requirements. Sometimes we may need to update the unique instance we created, how can we control

The update of the Instance objects in the ticket-making instance may sometimes have such a requirement. Let's take a look at how to achieve this.

3. scenario where the singleton object can be updated

First, let's talk about the following update Methods? For example, if the constructor of the class in singleton mode has certain parameters:

 

Public class updatehelper {private string type = string. empty; Private Static object _ Lock = new object (); Private Static updatehelper instance; private updatehelper (string valuetype) {type = valuetype ;} public static updatehelper instance {get {lock (_ Lock) {If (instance = NULL) {// It may be complicated to write if multiple conditions are required, is there a better way to handle it? Instance = new updatehelper ("test! ");} Return instance ;}}}}

Are there any better solutions to this problem?

1. First, we cannot manually instantiate it, so we have no way to dynamically pass in the constructor parameter. We can only specify this parameter within the class, but sometimes we need to dynamically update this parameter.

Obviously, there is no way to achieve this.

2. You can set the attribute content dynamically to change the output parameters. However, this method may be too free to meet the original intention of the singleton mode.

3. interface mode, because the interface must be implemented by class, it is more unreliable.

4. the attribute method is used to dynamically inject information into the constructor. But how can this method be used? After all, the singleton mode is simple.

5. dynamically configure related information through the config file configuration node through the configuration file to update the content of the Instance Object.

Through the analysis of the above five cases, this requirement can be achieved through 2, 4, 5, but compared with the corresponding cost, 5 is the most flexible and most compliant with the specifications of the singleton model.

The cost and cost can also be received.

 

 
<? XML version = "1.0" encoding = "UTF-8"?> <Configuration> <system. Web> <add key = "ssss"> value </Add> </system. Web> </configuration>

 

The code in the above single-force model only needs to be slightly changed. Please refer to the following code:

 

 
Public class updatehelper {private string type = string. empty; Private Static object _ Lock = new object (); Private Static updatehelper instance; private updatehelper (string valuetype) {type = valuetype ;} public static updatehelper instance {get {lock (_ Lock) {If (instance = NULL) {// It may be complicated to write if multiple conditions are required, is there a better way to handle it? Instance = new updatehelper (system. configuration. configurationmanager. etettings ["ssss"]. tostring ();} return instance ;}}}}

I think that everyone here has a simple understanding of the singleton mode, and the content of this article is described here. Let's review our content:

Vi. Summary

This article describes the singleton mode in the Creation Mode. Singleton mode is mainly used to control the number of instances of a class in the system and the Global Access endpoint. We mainly discuss how to implement the singleton mode,

It can be divided into external and internal methods. Of course, the methods we adopt are both internal methods. It also describes the thread-safe Singleton mode and constructors with parameters, dynamic configuration of parameter values based on the configuration file

. I hope that my colleagues who are not familiar with the design mode will be able to understand the application of the singleton mode, while those who are familiar with the singleton mode will be able to learn from each other for a better time, I will try to write this series.

In front of Daniel may be a shift, but I will continue to work hard to write a series of design patterns that everyone can understand at a glance. The mistakes in this article are inevitable. I will continue to modify them if you want to criticize them.

.

VII. Series progress Creation

1. Design Mode of system architecture skills-one-piece Mode

2. Design Mode of system architecture skills-factory Mode

3. Design Mode of system architecture skills-Abstract Factory Mode

4. Design Mode of system architecture skills-creator Mode

5. Design Mode of system architecture skills-prototype mode

Structural

1. Design Mode of system architecture skills-Combination Mode

2. Design Mode of system architecture skills-appearance Mode

3. Design Mode of system architecture skills-adapter Mode

4. Design Mode of system architecture skills-Bridge Mode

5. Design Mode of system architecture skills-decoration Mode

6. Design Mode of system architecture skills-enjoy the Yuan Model

7. Design Mode of system architecture skills-Agent Mode

Behavior Type

1. Design Mode of system architecture skills-command mode

2. Design Mode of system architecture skills-Observer Mode

3. Design Mode of system architecture skills-Strategy Mode

4. Design Mode of system architecture skills-responsibility Mode

5. Design Mode of system architecture skills-template Mode

6. Design Mode of system architecture skills-intermediary Mode

7. Design Mode of system architecture skills-interpreter Mode

8. next announcement

In the next article, we will introduce the most well-known engineering models. Of course, I will introduce more examples to illustrate the application scenarios and specific instances of each design model, to clearly describe under what circumstances the module is used

And the difference between each mode. Everyone's support is the motivation of my writing. I hope you can support me more!

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.