C # design patterns-differences between several obfuscated Design Patterns

Source: Internet
Author: User

Differences and usage of strategypattern and simple factory

Strategy pattern is mainly used to abstract specificAlgorithmOr the design mode of behavior. The appearance is similar to the simple factory mode, but the simple factory mode is mainly used to generate products, return new instances, and then program accordingly, the policy mode tends to specify a unique algorithm policy. After the instance policy class is initialized, the algorithm is called directly. In a simple factory, we only need to pass the corresponding conditions to get the desired object, and then implement algorithm operations through this object, in a simple factory, we implement a switch to process which object is instantiated. When using the Policy mode, you must first create a class object to be used on the client, then pass the most parameter of the object, and call different algorithms through the object, because the policy mode does not select a specific instance for us, it only calls a unified interface from the abstract class. In the simple factory mode, an object is instantiated by selecting a class based on conditions. In the policy mode, the work of the selected object is handed over to the user of the mode, which does not select the object itself.

 

Rule mode: defines the algorithm family and encapsulates them separately so that they can replace each other. This mode allows algorithm changes to be independent from customers who use the algorithm. It is a behavior mode and focuses on behavior.

Simple Factory: completely independent object creation, so that object creation has nothing to do with the specific user. In fact, the abstract factory mode belongs to the creation mode, A simple factory is only a programming idea, focusing on creating a specific instance.

There is no essential difference between them. They only use different scenarios and applicable areas.

 

The policy mode applies:

1. save files in different formats;

2. Use different algorithms to compress files;

3. capture images using different algorithms;

4. output images of the same data in different formats, such as curves or block bar.

 

Differences and usage of adapter pattern and proxy pattern

Adapter mode: converts an interface of a class to another interface that the customer wants, so that the original interface is incompatible and cannot work together. The proxy mode is the same as the interface of the agent, but the client cannot access the agent, so the agent is used for indirect access. In this case, the client can completely write newCodeImplement your own method, but with the adapter, the customer can use the existing implementation to convert the interface of a class to another interface that the customer wants. this solution solves the problem of interface incompatibility and achieves reuse.

Proxy mode: provides a proxy for other objects to control access to this object, focusing on uniform interface names, A proxy is set up to help process transactions because you do not have the permission to access the object content. For example, an application of WebService, such as an intermediary company, is a proxy element that the client can help itself handle tasks that cannot be processed by the client. Most of them are used in the later stages of code maintenance, or when a third-party library is used. Example code:

View code

 //  Unified Access to wages      Public  Interface  Salery {  String  Getsalery ();}  //  The agent of the technical department, because the technical department does not have access to the company's salary, you need to set up an agent of the human resources department to get the salary      Class  Teckdepartmentproxy: salery {  Private  Hrdepartment AHR;  Public   String  Getsalery (){  If (AHR =Null  ) {AHR = New  Hrdepartment ();}  Return  AHR. getsalery ();}}  //  Human Resources Department, get employee salary at any time      Class  Hrdepartment {  Public   String  Getsalery (){  Return  "  1 K  "  ;}}  Class  Program {  Static   Void Main ( String  [] ARGs) {salery ateck = New  Teckdepartmentproxy (); console. writeline (ateck. getsalery ());} 

The two design patterns are designed to reuse code and follow the open and closed principles. By comparing the UML diagram, we can see the most obvious difference between the two appearances, that is, the unification and adaptation of interfaces.

Proxy Mode

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.