Design Mode series-appearance Mode

Source: Internet
Author: User
Document directory
  • I. Review of the previous Article
  • Ii. Summary
  • Iii. Outline
  • Iv. Features and Use Cases of the Appearance Model
  • V. Implementation Scheme of appearance Mode
  • Vi. Appearance mode usage Summary
  • VII. Series progress
  • 8. next announcement
  • IX. Demo download
I. Review of the previous Article

In the previous article, we mainly talked about the last mode in the Creation Mode-prototype mode. We mainly talked about Several implementation schemes of the prototype mode, the application scenarios and features of the prototype mode, and the prototype mode.

Which scenarios are suitable for use? Let's review the three common scenarios described in the previous article.

1. When we dynamically create a dynamic object in the running state, we may use the prototype mode to dynamically create a copy of the specified type, this is undoubtedly a good choice, no

Then, if we use the several creation modes described earlier, the efficiency and cost will be very high.

2. Sometimes we need to compare the status of an object before and after processing, and compare whether the status of the object after processing has changed, or other requirements. This time

Cloning a copy of an object through the prototype mode is far more effective and easier to implement by introducing other Factory or abstract Factory.

3. If we find that there is a class of such objects, such objects are usually relatively simple, and there are regular differences between such objects, and the number of such objects is generally limited, at this time,

If we do this in prototype mode, it is easier to copy and create other types of objects through an object than to introduce other Factory or abstract Factory.

Provide a Clone () method.

4. Sometimes in our project, we expand based on the functions of others, and we cannot modify the existing programs. If this application is based on other types

If we add a new type in the system, we need to modify the code in the unified creation mode, whether it is to modify the configuration file or the specific functional code.

To modify the object, if we use the prototype mode, we only need to provide a clone method within the newly added type object to complete the creation of the new object.

Based on the above situation, we can see that there is a premise of the prototype mode, that is, the Clone () method must be called Based on the object to complete object replication. If this pair is not created

You may not be able to use this method directly.

We also talked about the situation of deep replication and shallow replication when cloning () objects, as well as the completion of deep replication of objects by serializing objects.

Ii. Summary

This article mainly describes the appearance mode, a common mode in the structural mode. What about this mode,The biggest feature is to package fine-grained objects into coarse-grained objects., The application uses

Access this appearance object to call fine-grained objects. The appearance mode is generally a common method in the design of the application service layer in distributed applications and system architectures. It is generally combined with the appearance mode + DTO

To complete the design of the service layer and provide efficient services for distributed application services, we can understand the appearance mode in this way. Through the appearance packaging, the application can only see the appearance object, instead of

This will undoubtedly reduce the complexity of the application and improve the maintainability of the program. This article will introduce the following aspects:

1. Use Cases and features of appearance Mode

2. Implement the appearance mode.

3. Summarize the appearance mode.

Here we first provide a schematic diagram of the appearance mode:

This is the case before the appearance mode is used. The following shows the case after the appearance mode is used:

The appearance object is used to organize fine-grained service calls. The appearance object is provided to external applications.

And the specific fine-grained calling process is encapsulated by the appearance object. Of course, this process is the encapsulated change part, and the changed part is isolated from the application, no doubt

The ease of use and maintainability of ordering are greatly improved.

Iii. Outline

A. Review in the previous article.

B. Summary.

C. Outline of this article.

D. Features and Use Cases of the appearance mode.

E. Implementation Scheme of appearance mode.

F. Summary of appearance mode usage.

G, Series progress.

H. next release notice.

Iv. Features and Use Cases of the Appearance Model

The main idea of appearance mode is to package complex fine-grained object services into simple and easy-to-use coarse-grained function services. The most understandable and known appearance mode is the APIs used.

Interface encapsulation: when we introduce third-party API interfaces into our projects, we need to package these interfaces and package fine-grained specific calling processes into the form of appearance classes, connect

Use the appearance class for unified calling. We usually call some common public methods a simple appearance mode. We package complex fine-grained functions into a general and simple one.

Coarse-grained functions. Let's take a look at which scenarios are suitable for us to use the appearance model?

1. When we use a third-party class library or API, we encapsulate the local API to complete the coarse-grained appearance object of the third-party API. This appearance object can be quite

Easy to call services. Here is an example. For example, if I have an API for sending text messages, which is provided by a third party, how can I package it?

The following code and description of API encapsulation are provided.

Public class MessageHelper
{
Private static readonly MessageHelper instance = new MessageHelper ();

# Region API Interface

[DllImport ("EUCPComm. dll", EntryPoint = "SendSMS")] // instant sending
Private static extern int SendSMS (string sn, string mn, string ct, string priority );

[DllImport ("EUCPComm. dll", EntryPoint = "SendSMSEx")] // instant sending (Extension)
Private static extern int SendSMSEx (string sn, string mn, string ct, string addi, string priority );

[DllImport ("EUCPComm. dll", EntryPoint = "SendScheSMS")] // timed sending
Private static extern int SendScheSMS (string sn, string mn, string ct, string ti, string priority );

# Endregion

# Region method after packaging the preceding API

Public int SendSMSEx1 (string sn, string mn, string ct, string addi, string priority)
{
Return SendSMSEx (sn, mn, ct, addi, priority );
}

Public int SendSMS1 (string sn, string mn, string ct, string priority)
{
Return SendSMS (sn, mn, ct, priority );
}

Public int SendScheSMS1 (string sn, string mn, string ct, string ti, string priority)
{
Return SendScheSMS (sn, mn, ct, ti, priority );
}

# Endregion
}

Related Test code:

Static void Main (string [] args)
{
// Related test code
// Call the service in the appearance object
MessageHelper. instance. SendSMS1 ("","","","");
}

2. During the architecture design process, a single function access may need to call many objects at the same time. If we call a service, the application can be called once.

It would be nice to call all objects at the same time, and the appearance mode is undoubtedly the best principle, especially in distributed applications, by remotely calling the service, reduces the interaction between applications and services in appearance Mode

Times, while reducing the complexity of the application. The appearance mode + DTO provides the performance of remote service calls. These are good design methods. Let's take a look at the simple example.

You can understand the secrets. Let's look at the figure. Here we use a remote synchronization service as an example.

Before the appearance mode is used:

A simple synchronization service, because the application must perform three remote connections to complete synchronization operations in this service.

To synchronize the three objects, if we use the appearance object, we only need to access it once to complete the synchronization of the three objects. This undoubtedly improves the system performance and efficiency.

If you use DTO for transmission, you can provide access time and efficiency for remote service calls.

V. Implementation Scheme of appearance Mode

5.1 classic Implementation of appearance Mode

Let's take a look at the Classic implementation of the appearance mode. Here we have taken the appearance of the binary stream serialization service as an example to describe the classic implementation.

Define a binary serialized appearance class:

Public class SerializationFacede
{
Public string BinarySerializationObjToString (object target)
{
Using (MemoryStream stream = new MemoryStream ())
{
New BinaryFormatter (). Serialize (stream, target );

Byte [] targetArray = stream. ToArray ();

Return Convert. ToBase64String (targetArray );
}
}

Public object DerializationStringToObj (string target)
{
Byte [] targetArray = Convert. FromBase64String (target );
Using (MemoryStream stream = new MemoryStream (targetArray ))
{
Return new BinaryFormatter (). Deserialize (stream );
}
}

Public T Derialization <T> (string target)
{
Return (T) DerializationStringToObj (target );
}
}

Here we provide the relevant test code:

Static void Main (string [] args)
{
// Appearance
SerializationFacede facede = new SerializationFacede ();
// Test object class
Product product = new Product ();

// Serialize the object
String productString = facede. BinarySerializationObjToString (product );
// Deserialization object
Product = facede. Derialization <Product> (productString );
}

The code above shows that the appearance class can also be provided in the form of static methods, providing a unified access portal, here we can use the singleton mode described above to solve this problem.

Problem. You can also create an external object in a created factory instead of using new.

We can improve the appearance mode and define interfaces to decouple the call of the client program. By providing an interface, the customer can call it through the interface.

Dependency:

Let's take a look at the Interface Definition Format:

/// <Summary>
/// Serialization service appearance Interface
/// </Summary>
Public interface ISerializationFace
{
String SerializableToString (object target );

Object DerializableToObject (string target );

T Derializable <T> (string target );
}

We implement the service appearance interface defined by us in the form of SOAP and binary respectively:

Binary serialization service:

Public class BinarySerializationFace: ISerializationFace
{
# Region ISerializationFace Member

Public string SerializableToString (object target)
{
Throw new NotImplementedException ();
}

Public object DerializableToObject (string target)
{
Throw new NotImplementedException ();
}

Public T Derializable <T> (string target)
{
Throw new NotImplementedException ();
}

# Endregion
}

SOAP serialization service:

Public class SoapSerializationFace: ISerializationFace
{
# Region ISerializationFace Member

Public string SerializableToString (object target)
{
Throw new NotImplementedException ();
}

Public object DerializableToObject (string target)
{
Throw new NotImplementedException ();
}

Public T Derializable <T> (string target)
{
Throw new NotImplementedException ();
}

# Endregion
}

The test code is as follows:

Static void Main (string [] args)
{
// Appearance
ISerializationFace facede = new SoapSerializationFace ();
// Test object class
Product product = new Product ();

// Serialize the object
String productString = facede. SerializableToString (product );
// Deserialization object
Product = facede. Derializable <Product> (productString );
}

This improves the flexibility of the appearance mode.

5.2. Other considerations of the appearance mode 5.2.1-expansion of the traditional appearance Mode

In the appearance mode shown above, we still directly use the new () Specific serialization object form in the specific test code. Here we can make improvements through the factory mode or through the configuration

In the form of configuration files, decoupling may be a good choice if there are not many appearance classes, rather than creating a separate factory.

We can improve the above scheme in the following form. For example, the format of our configuration file is defined as follows:

<? Xml version = "1.0" encoding = "UTF-8"?>
<Serialization>
<SerializationSection name = "serialization" type = "SerializationType"/>
<SerializationSection/>
</Serialization>

Let's take a look at the serialization factory with sample code

Public class SerializationFactory
{
Public static ISerializationFace Create ()
{
// Type read in the configuration file
String type = XMLHelper. GetSectionValue ("");
Return (ISerializationFace) Activator. CreateInstance (Type. GetType (type ));
}
}

Let's take a look at the specific test code:

Static void Main (string [] args)
{
// Appearance
ISerializationFace facede = SerializationFactory. Create ();
// Test object class
Product product = new Product ();

// Serialize the object
String productString = facede. SerializableToString (product );
// Deserialization object
Product = facede. Derializable <Product> (productString );
}

5.2.2-expansion of the traditional appearance Mode

Next let's take a look at the DTO + appearance mode instance implementation code in another form of appearance Mode

Let's look at the DTO format:

/// <Summary>
/// DTO (data transmission object), also known as a data carrier
/// </Summary>
Public class DTO
{

Private Product _ product;
Private List <Order> _ orderList;
// All data information or other referenced Object Information
Public Product
{
Get
{
Return this. _ product;
}
Set
{
This. _ product = value;
}
}
Public List <Order> Product
{
Get
{
Return this. _ orderList;
}
Set
{
This. _ orderList = value;
}
}

}

The specific DTO Object Appearance class code is as follows:

/// <Summary>
/// Remote Access Service
/// </Summary>
Public class AccessService
{
Bool GetService (DTO dto)
{
Return true;
}

DTO GetData ()
{
Return new DTO ();
}
}

You can access the service in this simple way.

The test code of the program is as follows:

Static void Main (string [] args)
{
// Appearance
AccessService service = new AccessService ();

DTO dto = service. GetData ();
// TODO...
}

Through the above Code, we have completed the simplest remote appearance access service, and remote appearance services can also be completed in the form of WCF, because WCF has integrated the Remoting and

WebService or socket. I will not go into details here.

Vi. Appearance mode usage Summary

The appearance mode is a simple and practical mode in the structural mode. The appearance mode encapsulates details to provide large-granularity calls. The direct advantage is that it encapsulates details and provides application write programs.

Maintainability and ease of use. Appearance mode is generally applied to the service layer of the system architecture. When we are using multiple different types of client applications, for example, a system can access

Question: You can also provide remote services in the form of a client application through the appearance mode, so that the application can be called remotely, so that services can be provided in the form of appearance, no matter what

The client accesses the same appearance service, so even if our application service changes in the future, we do not need to modify the call of any client application, just need to modify the corresponding appearance application that is

Yes.

VII. Series progress

Creation type

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

The next article will describe the appearance mode. This mode is also one of the distinctive design modes in the structure mode. This mode encapsulates some fine-grained items in the existing system through the appearance object,

When you access these methods in an application, a unified access portal is provided in the form of a appearance class, and the specific details are not required by the application, this will reduce the complexity of program calls.

Due to my limited level, insufficiency, or errors, please criticize and correct me. Please continue to support me. Thank you.

IX. Demo download

Demo

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.