C # understand where abstract classes and interfaces are applied through the Observer Design Pattern

Source: Internet
Author: User

When to use abstract classes? When to use interfaces? How to understand abstract classes? How do I understand interfaces?

1. Baidu explains the differences between abstract classes and interfaces

In a word, an abstract class can contain specific implementations, and an interface can only contain definitions.

When implementing an interface, you must implement methods defined by the interface. If the abstract class adds abstract to the method, this method also needs to be implemented after inheritance. From here, an interface is a special abstract class that does not contain specific implementations.

2. Differences between interfaces and abstract classes in the Observer Mode

What is the observer mode? Let me give you an example. There are four people in your family, including a baby, father, mother, and grandmother. Now we simulate a scenario where the baby is sleeping, once the baby wakes up, it will cry. If my father, mother, and grandmother will do some things after the baby cries (the specific things will read the code), when will the baby wake up? I don't know. In this case, my father, mother, and grandmother will have to go and watch the baby wake up. They can't do anything else. This is unreasonable, so we should be able to trigger the response from the father, mother, and grandmother when the baby wakes up. See the following code:

Using System; using System. collections; // Observer, defined as interface public interface Observer {void Do () ;}// Dad. after implementing the Observer interface, it becomes an Observer public class Father: observer {public void Do () {// dad told mom That The baby woke up on the Console. writeLine ("Father tell baby's mother the baby is weak up. ") ;}} // Mom, after implementing the Observer interface, it becomes an Observer public class Mother: Observer {public void Do () {// Mom wants to hold the baby Console. writeLine ("Mother embrace the baby. ") ;}}// grandma, implements the Observer interface and becomes an Observer public class GrandMother: Observer {public void Do () {// Grandma wants to get the milk Console. writeLine ("Grandmother take the milk to baby. ") ;}}// observed object, defined as abstract class public abstract class Subject {public ArrayList list = new ArrayList (); public void Attach (params Observer [] dst) {for (int I = 0; I <dst. length; I ++) {list. add (dst [I]) ;}} public void DeTach (params Observer [] dst) {for (int I = 0; I <dst. length; I ++) {list. remove (dst [I]) ;}} public abstract void WeakUpAndCry () ;}// Baby, inherit the observer abstract class and become the observer public class Baby: subject {public Baby (string name) {this. name = name;} private string Name {get; set;} public override void WeakUpAndCry () {foreach (Observer item in list) item. do () ;}} // program entry public class Client {public static void Main (string [] args) {Baby baby = new Baby ("simon"); baby. attach (new Father (), new Mother (), new GrandMother (); baby. weakUpAndCry (); Console. read ();}}

Question 1: The Subject abstract class defines an abstract method "public abstract void WeakUpAndCry ();". This method must be rewritten in the subclass. Why do we not implement this method in the abstract class, in this way, the abstract class is no longer needed. Just use the ordinary class directly?

Let me assume that my business requires that, if the baby name is simon, my father, mother, and grandmother will respond. If the baby name is lucy, my father will not do anything. This requirement cannot be implemented if the WeakUpAndCry method is implemented in the abstract class.

From the code above, we can find that: 1. The observer is an interface. 2. The observer is an abstract class.

Then we can draw a conclusion:

1. The interface is used to define what the implementation class looks like

2. abstract classes provide a set of public methods. For non-public use, they are defined but not implemented. You can write different implementations in each subclass according to your own needs.

 

 

 

 

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.