zoukankan      html  css  js  c++  java
  • c# 设计模式篇

    一、 单例(Singleton)模式

       单例模式的特点:

    • 单例类只能有一个实例。
    • 单例类必须自己创建自己的唯一实例。
    • 单例类必须给所有其它对象提供这一实例。

        程序举例:

    // Singleton pattern -- Structural example  
    using System;
    
    // "Singleton"
    class Singleton
    {
      // Fields
      private static Singleton instance;
    
      // Constructor
      protected Singleton() {}
    
      // Methods
      public static Singleton Instance()
      {
        // Uses "Lazy initialization"
        if( instance == null )
          instance = new Singleton();
    
        return instance;
      }
    }
    
    /// <summary>
    /// Client test
    /// </summary>
    public class Client
    {
      public static void Main()
      {
        // Constructor is protected -- cannot use new
        Singleton s1 = Singleton.Instance();
        Singleton s2 = Singleton.Instance();
    
        if( s1 == s2 )
          Console.WriteLine( "The same instance" );
      }
    }

    二、 策略(Strategy)模式

                     策略模式的用意是针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使得它们可以相互替换。策略模式使得算法可以在不影响到客户端的情况下发生变化。

                   程序举例:

               

    // Strategy pattern -- Real World example  
    using System;
    using System.Collections;
    
    // "Strategy"
    abstract class SortStrategy
    {
      // Methods
      abstract public void Sort( ArrayList list );
    }
    
    // "ConcreteStrategy"
    class QuickSort : SortStrategy
    {
      // Methods
      public override void Sort(ArrayList list )
      {
        list.Sort(); // Default is Quicksort
        Console.WriteLine("QuickSorted list ");
      }
    }
    
    // "ConcreteStrategy"
    class ShellSort : SortStrategy
    {
      // Methods
      public override void Sort(ArrayList list )
      {
        //list.ShellSort();
        Console.WriteLine("ShellSorted list ");
      }
    }
    
    // "ConcreteStrategy"
    class MergeSort : SortStrategy
    {
      // Methods
      public override void Sort( ArrayList list )
      {
        //list.MergeSort();
        Console.WriteLine("MergeSorted list ");
      }
    }
    
    // "Context"
    class SortedList
    {
      // Fields
      private ArrayList list = new ArrayList();
      private SortStrategy sortstrategy;
    
      // Constructors
      public void SetSortStrategy( SortStrategy sortstrategy )
      {
        this.sortstrategy = sortstrategy;
      }
    
      // Methods
      public void Sort()
      {
        sortstrategy.Sort( list );
      }
    
      public void Add( string name )
      {
        list.Add( name );
      }
    
      public void Display()
      {
        foreach( string name in list )
          Console.WriteLine( " " + name );
      }
    }
    
    /// <summary>
    /// StrategyApp test
    /// </summary>
    public class StrategyApp
    {
      public static void Main( string[] args )
      {
        // Two contexts following different strategies
        SortedList studentRecords = new SortedList( );
        studentRecords.Add( "Samual" );
        studentRecords.Add( "Jimmy" );
        studentRecords.Add( "Sandra" );
        studentRecords.Add( "Anna" );
        studentRecords.Add( "Vivek" );
    
        studentRecords.SetSortStrategy( new QuickSort() );
        studentRecords.Sort();
        studentRecords.Display();
      }
    }

     三、 "开放-封闭"原则(OCP)

    Open-Closed Principle原则讲的是:一个软件实体应当对扩展开放,对修改关闭。

    四、 里氏代换原则(LSP)

    Liskov Substitution Principle(里氏代换原则):父类中的成员在子类中都有实现,反之则不一定.因此可以用子类实例替换父类实例,以达到相同的功能(具体实现可能不一样),反之则不可以.
    如:
    class human
    {
       void eat(){}
    }
    class Chinese:human
    {
        void speakingChinese(){}
    }

    class client
    {
        static void Main()
    {
         human man=new Chinese();
        man.eat() ;   //可以

    Chinese chinses=new human();//不可以,human中没有speakingChinese()方法

    }
    }

  • 相关阅读:
    November 07th, 2017 Week 45th Tuesday
    November 06th, 2017 Week 45th Monday
    November 05th, 2017 Week 45th Sunday
    November 04th, 2017 Week 44th Saturday
    November 03rd, 2017 Week 44th Friday
    Asp.net core 学习笔记 ( Area and Feature folder structure 文件结构 )
    图片方向 image orientation Exif
    Asp.net core 学习笔记 ( Router 路由 )
    Asp.net core 学习笔记 ( Configuration 配置 )
    qrcode render 二维码扫描读取
  • 原文地址:https://www.cnblogs.com/chengjunwei/p/3092229.html
Copyright © 2011-2022 走看看