[. NET] Quick notes on Objective C # (3)-use C # To express design,

Source: Internet
Author: User
Tags types of functions

[. NET] Quick notes on Objective C # (3)-use C # To express design,
Quick notes for Objective C # (III)-use C # To express the design directory

  • 21. Restricted type visibility
  • 22. define and implement interface substitution
  • 23. Differences between interface methods and Virtual Methods
  • 24. Delegate callback
  • 25. Implement notification in Event Mode
  • 26. Avoid returning references to internal class objects
  • 27. Support serialization for Types
  • 28. Provides group-level Internet service APIs
  • 29. Support for generic covariant and Inverter

 

21. Restricted type visibility

1. To ensure that the type can complete the work, assign the minimum visibility to the type as much as possible.

2. We often subconsciously create public types. The lower the visibility, the less changes you need to upgrade or change later, because the less code you need to access your function module.

3. Creating an internal class is a method that is often ignored to restrict the scope of the type. We often get used to creating a public class without thinking about it. You should think carefully about the scope of this type, that is, whether it will be used by all customers or only within the Assembly.

4. Fewer public types also make unit tests easier, because the quantity is reduced.

5. Classes and interfaces exposed to the outside world in public form will become the contract of your components. The more redundant the interface is, the more limited future modifications will be. The fewer exposed public types, the greater the room for future updates and extensions.

 

22. define and implement interface substitution

1. abstract base classes provide a shared ancestor for the class inheritance system, and Interfaces describe a group of atomic functions. An interface is a contract. abstract base classes provide a shared abstraction for a group of related types. A base class describes what an object is and an interface describes how an object represents its behavior.

2. We should distinguish and extract reusable behaviors and define them in interfaces. Because unrelated types can all implement an interface, this indicates that the code reuse rate will be greatly increased.

3. if you add a method to the base class, all the derived classes will automatically include the method. That is to say, over time, you can still effectively expand multiple types of functions. By adding and implementing a function to the base class, all derived classes will immediately have this function. Adding a member to an interface destroys all classes that implement the interface. Because these classes do not contain new methods, every implementation must be updated and re-compiled.

4. Selecting between abstract base classes and interfaces actually represents different attitudes towards future changes. Interfaces are fixed: we encapsulate a set of functions in one interface as other types of contracts. The base class can be extended in the future, and these extensions will become part of each derived class.

5. You can also use the extension method for expansion.

6. Sometimes, using interfaces can help us avoid the cost caused by struct unpacking.

 

23. Differences between interface methods and Virtual Methods

 

24. Delegate callback

1. When classes need to communicate with each other and we expect a loose coupling mechanism than the interfaces, delegation is the best choice. The delegate allows us to configure the target at runtime and notify multiple customer objects. The delegate object contains a method reference, which can be a static method or an instance method.

2. the multicast delegate combines all the target functions added to the delegate into a single call. Note that:

(1) If an exception occurs in a delegated call, security cannot be guaranteed;

(2) the return value of the entire call will be the return value of the last function call.

3. During the multicast delegate call process, each target is called in sequence. The delegate object itself does not capture any exceptions. Therefore, any exceptions thrown by the target end the call of the delegate chain.

 

25. Implement notification in Event Mode

  

26. Avoid returning references to internal class objects

1.4 policies can prevent internal data structures of types from being intentionally or unintentionally modified: Value Type, constant type, interface and wrapper ):

(1) When the Customer Code accesses a value-type member through the property, a copy of the value type is actually returned.

(2) constant type. For example, System. String is also safe.

(3) expose the class function to the outside through the interface, so as to avoid unintentional changes to internal data.

(4) Only expose the package.

2. You can use interfaces, wrapper objects, or value types to provide external private data to restrict external access to the data.

 

27. Support serialization for Types

  

28. Provides group-level Internet service APIs

1. Each time an application obtains a small segment of data through the network, the application needs to wait for the network transmission process. The finer the API granularity, the more time it takes to wait for the data to return.

2. We hope to reduce the communication frequency and the amount of data transmitted during each communication at the same time. However, these two goals often cannot have both sides, so you must make a trade-off. Try not to take two extremes. You can select a smaller number of communications and try to transmit more data at a time.

 

29. Support for generic covariant and Inverter

1. Type variants: covariant and inverter. Defines that in some cases, a type can be used instead of another type. You should try your best to make generic interfaces and generic delegation support covariant and inverter.

2. covariant and inverter are two different types of replacement. If a returned type can be replaced by a derived type, this type supports covariant. If a parameter type can be replaced by its base class, this type supports invert.

3. If possible, add the in and out parameters for the generic interface and delegate.

4. Because IList <T> does not add in or out modifier T, exact type matching must be used.

 

This series

Quick notes in Objective C # (1)-C # language habits

Quick notes for Objective C # (2)-. NET resource hosting

Quick notes in Objective C # (III)-use C # To express design

 

 

[Blogger] Anti-Bot

Http://www.cnblogs.com/liqingwen/p/6774210.html.

Related Article

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.