精华内容
参与话题
问答
  • C# - Tester-Doer Pattern

    万次阅读 多人点赞 2019-02-27 11:22:13
    分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!...Indaily coding, consider the Tester-Doer pattern for members that may throw exceptions in common scenarios to avoid perform...

    分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net 

    In daily coding, consider the Tester-Doer pattern for members that may throw exceptions in common scenarios to avoid performance problems related to exceptions. The Tester-Doer pattern divides a call that might throw exceptions into two parts: a Tester and a Doer. The Tester performs a test for the state that can cause the Doer to throw an exception. The test is inserted just before the code that throws the exception, thereby guarding against the exception.

    The following code example shows a Tester-Doer pattern.

    if (uiElement.Exists(true, false)) // Tester
    {
        uiElement.Click(); // Doer
    }

    In the above example, if we don't use a Tester here, when the uiElement does not exist, the Doer will throw an exception and the application running will down. So the Tester-Doer pattern is very important in daily coding.

    展开全文
  • Design Pattern - Strategy(C#)

    万次阅读 多人点赞 2019-02-27 10:38:14
    分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!... Definition Define a family of algorithms, encapsulate each one, and make them interchangeable....

    分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net 

    Definition

    Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

    When To Use

    The strategy design pattern is used to encapsulate changes and allows you to easily swap in and out alternative implementations, even at runtime. This makes it a great tool for benchmarking algorithms, responding to volatile requirements and quickly trying out new ideas.

    Participants

    The classes and/or objects participating in this pattern are:  

    • Strategy (SortStrategy)
      • Declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy
    • ConcreteStrategy (QuickSort, ShellSort, MergeSort)
      • Implements the algorithm using the Strategy interface
    • Context (SortedList)
      • Is configured with a ConcreteStrategy object
      • Maintains a reference to a Strategy object
      • May define an interface that lets Strategy access its data

    Sample Code in C#

    This structural code demonstrates the Strategy pattern which encapsulates functionality in the form of an object. This allows clients to dynamically change algorithmic strategies.

    /*
     * Created by Chimomo
     * 
     * Strategy pattern - Structural example
     */
    
    using System;
    
    namespace Strategy.Sample
    {
        /// <summary>
        /// Startup class for Structural Strategy Design Pattern.
        /// </summary>
        public class MainApp
        {
            /// <summary>
            /// Entry point into console application.
            /// </summary>
            public static void Main()
            {
                // Three contexts following different strategies.
                var context = new Context(new ConcreteStrategyA());
                context.ContextInterface();
    
                context = new Context(new ConcreteStrategyB());
                context.ContextInterface();
    
                context = new Context(new ConcreteStrategyC());
                context.ContextInterface();
            }
        }
    
        /// <summary>
        /// The 'Strategy' abstract class.
        /// </summary>
        public abstract class Strategy
        {
            /// <summary>
            /// The algorithm interface.
            /// </summary>
            public abstract void AlgorithmInterface();
        }
    
        /// <summary>
        /// A 'ConcreteStrategy' class.
        /// </summary>
        public class ConcreteStrategyA : Strategy
        {
            /// <summary>
            /// The algorithm interface.
            /// </summary>
            public override void AlgorithmInterface()
            {
                Console.WriteLine("Called ConcreteStrategyA.AlgorithmInterface()");
            }
        }
    
        /// <summary>
        /// A 'ConcreteStrategy' class.
        /// </summary>
        public class ConcreteStrategyB : Strategy
        {
            /// <summary>
            /// The algorithm interface.
            /// </summary>
            public override void AlgorithmInterface()
            {
                Console.WriteLine("Called ConcreteStrategyB.AlgorithmInterface()");
            }
        }
    
        /// <summary>
        /// A 'ConcreteStrategy' class.
        /// </summary>
        public class ConcreteStrategyC : Strategy
        {
            /// <summary>
            /// The algorithm interface.
            /// </summary>
            public override void AlgorithmInterface()
            {
                Console.WriteLine("Called ConcreteStrategyC.AlgorithmInterface()");
            }
        }
    
        /// <summary>
        /// The 'Context' class.
        /// </summary>
        public class Context
        {
            /// <summary>
            /// The strategy.
            /// </summary>
            private readonly Strategy _strategy;
    
            /// <summary>
            /// Initializes a new instance of the <see cref="Context"/> class.
            /// </summary>
            /// <param name="strategy">
            /// The strategy.
            /// </param>
            public Context(Strategy strategy)
            {
                _strategy = strategy;
            }
    
            /// <summary>
            /// The context interface.
            /// </summary>
            public void ContextInterface()
            {
                _strategy.AlgorithmInterface();
            }
        }
    }
    
    // Output:
    /*
    Called ConcreteStrategyA.AlgorithmInterface()
    Called ConcreteStrategyB.AlgorithmInterface()
    Called ConcreteStrategyC.AlgorithmInterface()
    
    */

    This real-world code demonstrates the Strategy pattern which encapsulates sorting algorithms in the form of sorting objects. This allows clients to dynamically change sorting strategies including Quicksort, Shellsort, and Mergesort.

    /*
     * Created by Chimomo
     *
     * Strategy pattern - Real World example
     */
    
    using System;
    using System.Collections.Generic;
    
    namespace Strategy.RealWorld
    {
        /// <summary>
        /// Startup class for Real-World Strategy Design Pattern.
        /// </summary>
        public class MainApp
        {
            /// <summary>
            /// Entry point into console application.
            /// </summary>
            public static void Main()
            {
                // Context following different strategies.
                SortedList studentRecords = new SortedList();
                studentRecords.Add("Samual");
                studentRecords.Add("Jimmy");
                studentRecords.Add("Sandra");
                studentRecords.Add("Vivek");
                studentRecords.Add("Anna");
    
                studentRecords.SetSortStrategy(new QuickSort());
                studentRecords.Sort();
    
                studentRecords.SetSortStrategy(new ShellSort());
                studentRecords.Sort();
    
                studentRecords.SetSortStrategy(new MergeSort());
                studentRecords.Sort();
            }
        }
    
        /// <summary>
        /// The 'Strategy' abstract class.
        /// </summary>
        public abstract class SortStrategy
        {
            /// <summary>
            /// Sort.
            /// </summary>
            /// <param name="list">
            /// The list.
            /// </param>
            public abstract void Sort(List<string> list);
        }
    
        /// <summary>
        /// A 'ConcreteStrategy' class.
        /// </summary>
        public class QuickSort : SortStrategy
        {
            /// <summary>
            /// Sort.
            /// </summary>
            /// <param name="list">
            /// The list.
            /// </param>
            public override void Sort(List<string> list)
            {
                // Default is QuickSort.
                list.Sort();
                Console.WriteLine("QuickSorted list:");
            }
        }
    
        /// <summary>
        /// A 'ConcreteStrategy' class.
        /// </summary>
        public class ShellSort : SortStrategy
        {
            /// <summary>
            /// Sort.
            /// </summary>
            /// <param name="list">
            /// The list.
            /// </param>
            public override void Sort(List<string> list)
            {
                // list.ShellSort(); not-implemented.
                Console.WriteLine("ShellSorted list:");
            }
        }
    
        /// <summary>
        /// A 'ConcreteStrategy' class.
        /// </summary>
        public class MergeSort : SortStrategy
        {
            /// <summary>
            /// Sort.
            /// </summary>
            /// <param name="list">
            /// The list.
            /// </param>
            public override void Sort(List<string> list)
            {
                // list.MergeSort(); not-implemented.
                Console.WriteLine("MergeSorted list:");
            }
        }
    
        /// <summary>
        /// The 'Context' class.
        /// </summary>
        public class SortedList
        {
            /// <summary>
            /// The list.
            /// </summary>
            private readonly List<string> _list = new List<string>();
    
            /// <summary>
            /// The sort strategy.
            /// </summary>
            private SortStrategy sortStrategy;
    
            /// <summary>
            /// Set sort strategy.
            /// </summary>
            /// <param name="sortStrategy">
            /// The sort strategy.
            /// </param>
            public void SetSortStrategy(SortStrategy sortStrategy)
            {
                this.sortStrategy = sortStrategy;
            }
    
            /// <summary>
            /// Add.
            /// </summary>
            /// <param name="name">
            /// The name.
            /// </param>
            public void Add(string name)
            {
                _list.Add(name);
            }
    
            /// <summary>
            /// Sort.
            /// </summary>
            public void Sort()
            {
                sortStrategy.Sort(_list);
    
                // Iterate over list and display results.
                foreach (string name in _list)
                {
                    Console.WriteLine("  " + name);
                }
    
                Console.WriteLine();
            }
        }
    }
    
    // Output:
    /*
    QuickSorted list:
      Anna
      Jimmy
      Samual
      Sandra
      Vivek
    
    ShellSorted list:
      Anna
      Jimmy
      Samual
      Sandra
      Vivek
    
    MergeSorted list:
      Anna
      Jimmy
      Samual
      Sandra
      Vivek
    
    
    */

     

    展开全文
  • Design Pattern - Prototype(C#)

    万次阅读 多人点赞 2019-02-02 17:29:01
    分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!... Definition Specify the kind of objects to create using a prototypical instance, and create new objects by ...

    分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net 

    Definition

    Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype.

    Participants

        The classes and/or objects participating in this pattern are:

    • Prototype (ColorPrototype)
      • Declares an interface for cloning itself
    • ConcretePrototype (Color)
      • Implements an operation for cloning itself
    • Client (ColorManager)
      • Creates a new object by asking a prototype to clone itself

    Sample Code in C#


    This structural code demonstrates the Prototype pattern in which new objects are created by copying pre-existing objects (prototypes) of the same class.

    /*
     * Structural Prototype Design Pattern.
     */
    
    namespace Prototype.Sample
    {
        using System;
    
        /// <summary>
        /// Startup class for Structural Prototype Design Pattern.
        /// </summary>
        internal static class Program
        {
            #region Methods
    
            /// <summary>
            /// Entry point into console application.
            /// </summary>
            private static void Main()
            {
                // Create two instances and clone each.
                var p1 = new ConcretePrototype1("I");
                var c1 = (ConcretePrototype1) p1.Clone();
                Console.WriteLine("Cloned: {0}", c1.Id);
                var p2 = new ConcretePrototype2("II");
                var c2 = (ConcretePrototype2) p2.Clone();
                Console.WriteLine("Cloned: {0}", c2.Id);
            }
    
            #endregion
        }
    
        /// <summary>
        /// The 'Prototype' abstract class.
        /// </summary>
        internal abstract class Prototype
        {
            #region Constructors and Destructors
    
            /// <summary>
            /// Initializes a new instance of the <see cref="Prototype"/> class.
            /// </summary>
            /// <param name="id">
            /// The id.
            /// </param>
            protected Prototype(string id)
            {
                Id = id;
            }
    
            #endregion
    
            #region Public Properties
    
            /// <summary>
            /// Gets the id.
            /// </summary>
            public string Id { get; }
    
            #endregion
    
            #region Public Methods and Operators
    
            /// <summary>
            /// Clone.
            /// </summary>
            /// <returns>
            /// The <see cref="Prototype"/>.
            /// </returns>
            public abstract Prototype Clone();
    
            #endregion
        }
    
        /// <summary>
        /// A 'ConcretePrototype' class.
        /// </summary>
        internal class ConcretePrototype1 : Prototype
        {
            #region Constructors and Destructors
    
            /// <summary>
            /// Initializes a new instance of the <see cref="ConcretePrototype1"/> class.
            /// </summary>
            /// <param name="id">
            /// The id.
            /// </param>
            public ConcretePrototype1(string id)
                : base(id)
            {
            }
    
            #endregion
    
            #region Public Methods and Operators
    
            /// <summary>
            /// Clone.
            /// Returns a shallow copy.
            /// </summary>
            /// <returns>
            /// The <see cref="Prototype"/>.
            /// </returns>
            public override Prototype Clone()
            {
                return (Prototype) MemberwiseClone();
            }
    
            #endregion
        }
    
        /// <summary>
        /// A 'ConcretePrototype' class.
        /// </summary>
        internal class ConcretePrototype2 : Prototype
        {
            #region Constructors and Destructors
    
            /// <summary>
            /// Initializes a new instance of the <see cref="ConcretePrototype2"/> class.
            /// </summary>
            /// <param name="id">
            /// The id.
            /// </param>
            public ConcretePrototype2(string id)
                : base(id)
            {
            }
    
            #endregion
    
            #region Public Methods and Operators
    
            /// <summary>
            /// Clone.
            /// Returns a shallow copy.
            /// </summary>
            /// <returns>
            /// The <see cref="Prototype"/>.
            /// </returns>
            public override Prototype Clone()
            {
                return (Prototype) MemberwiseClone();
            }
    
            #endregion
        }
    }
    
    // Output:
    /*
    Cloned: I
    Cloned: II
    
    */

    This real-world code demonstrates the Prototype pattern in which new Color objects are created by copying pre-existing, user-defined Colors of the same type.

    /*
     * Real-World Prototype Design Pattern.
     */
    
    namespace Prototype.RealWorld
    {
        using System;
        using System.Collections.Generic;
    
        /// <summary>
        /// Startup class for Real-World Prototype Design Pattern.
        /// </summary>
        internal static class Program
        {
            #region Methods
    
            /// <summary>
            /// Entry point into console application.
            /// </summary>
            private static void Main()
            {
                var colorManager = new ColorManager
                {
                    ["red"] = new Color(255, 0, 0),
                    ["green"] = new Color(0, 255, 0),
                    ["blue"] = new Color(0, 0, 255),
                    ["angry"] = new Color(255, 54, 0),
                    ["peace"] = new Color(128, 211, 128),
                    ["flame"] = new Color(211, 34, 20)
                };
    
                // User clones selected colors.
                var color1 = colorManager["red"].Clone() as Color;
                var color2 = colorManager["peace"].Clone() as Color;
                var color3 = colorManager["flame"].Clone() as Color;
            }
    
            #endregion
        }
    
        /// <summary>
        /// The 'Prototype' abstract class.
        /// </summary>
        internal abstract class Prototype
        {
            #region Public Methods and Operators
    
            /// <summary>
            /// Clone.
            /// </summary>
            /// <returns>
            /// The <see cref="Prototype"/>.
            /// </returns>
            public abstract Prototype Clone();
    
            #endregion
        }
    
        /// <summary>
        /// The 'ConcretePrototype' class.
        /// </summary>
        internal class Color : Prototype
        {
            #region Fields
    
            /// <summary>
            /// The blue.
            /// </summary>
            private readonly int _blue;
    
            /// <summary>
            /// The green.
            /// </summary>
            private readonly int _green;
    
            /// <summary>
            /// The red.
            /// </summary>
            private readonly int _red;
    
            #endregion
    
            #region Constructors and Destructors
    
            /// <summary>
            /// Initializes a new instance of the <see cref="Color"/> class.
            /// </summary>
            /// <param name="red">
            /// The red.
            /// </param>
            /// <param name="green">
            /// The green.
            /// </param>
            /// <param name="blue">
            /// The blue.
            /// </param>
            public Color(int red, int green, int blue)
            {
                _red = red;
                _green = green;
                _blue = blue;
            }
    
            #endregion
    
            #region Public Methods and Operators
    
            /// <summary>
            /// Clone.
            /// </summary>
            /// <returns>
            /// The <see cref="Prototype"/>.
            /// </returns>
            public override Prototype Clone()
            {
                Console.WriteLine("Cloning color RGB: {0,3},{1,3},{2,3}", _red, _green, _blue);
                return MemberwiseClone() as Prototype;
            }
    
            #endregion
        }
    
        /// <summary>
        /// Prototype manager.
        /// </summary>
        internal class ColorManager
        {
            #region Fields
    
            /// <summary>
            /// The colors.
            /// </summary>
            private readonly Dictionary<string, Prototype> _colors = new Dictionary<string, Prototype>();
    
            #endregion
    
            #region Public Indexers
    
            /// <summary>
            /// This.
            /// </summary>
            /// <param name="key">
            /// The key.
            /// </param>
            /// <returns>
            /// The <see cref="Prototype"/>.
            /// </returns>
            public Prototype this[string key]
            {
                get { return _colors[key]; }
    
                set { _colors.Add(key, value); }
            }
    
            #endregion
        }
    }
    
    // Output:
    /*
    Cloning color RGB: 255,  0,  0
    Cloning color RGB: 128,211,128
    Cloning color RGB: 211, 34, 20
    
    */

     

    展开全文
  • Design Pattern - Memento(C#)

    万次阅读 多人点赞 2019-02-12 14:44:19
    分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!... Definition Without violating encapsulation, capture and externalize an object's internal state so that the ...

    分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net 

    Definition

    Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later.

    Participants

        The classes and/or objects participating in this pattern are:

    • Memento (Memento)
      • Stores internal state of the Originator object. The memento may store as much or as little of the originator's internal state as necessary at its originator's discretion.
      • Protect against access by objects of other than the originator. Mementos have effectively two interfaces. Caretaker sees a narrow interface to the Memento -- it can only pass the memento to the other objects. Originator, in contrast, sees a wide interface, one that lets it access all the data necessary to restore itself to its previous state. Ideally, only the originator that produces the memento would be permitted to access the memento's internal state.
    • Originator (SalesProspect)
      • Creates a memento containing a snapshot of its current internal state.
      • Uses the memento to restore its internal state
    • Caretaker (Caretaker)
      • Is responsible for the memento's safekeeping
      • Never operates on or examines the contents of a memento.

    Sample Code in C#


    This structural code demonstrates the Memento pattern which temporary saves and restores another object's internal state.

    /*
     * Structural Memento Design Pattern.
     */
    
    namespace Memento.Sample
    {
        using System;
    
        /// <summary>
        /// Startup class for Structural Memento Design Pattern.
        /// </summary>
        internal static class Program
        {
            #region Methods
    
            /// <summary>
            /// Entry point into console application.
            /// </summary>
            private static void Main()
            {
                var o = new Originator {State = "On"};
    
                // Store internal state.
                var c = new Caretaker {Memento = o.CreateMemento()};
    
                // Continue changing originator.
                o.State = "Off";
    
                // Restore saved state.
                o.SetMemento(c.Memento);
            }
    
            #endregion
        }
    
        /// <summary>
        /// The 'Originator' class.
        /// </summary>
        internal class Originator
        {
            #region Fields
    
            /// <summary>
            /// The state.
            /// </summary>
            private string _state;
    
            #endregion
    
            #region Public Properties
    
            /// <summary>
            /// Gets or sets the state.
            /// </summary>
            public string State
            {
                get { return _state; }
    
                set
                {
                    _state = value;
                    Console.WriteLine("State = " + _state);
                }
            }
    
            #endregion
    
            #region Public Methods and Operators
    
            /// <summary>
            /// Create memento.
            /// </summary>
            /// <returns>
            /// The <see cref="Memento"/>.
            /// </returns>
            public Memento CreateMemento()
            {
                return new Memento(_state);
            }
    
            /// <summary>
            /// Set memento.
            /// </summary>
            /// <param name="memento">
            /// The memento.
            /// </param>
            public void SetMemento(Memento memento)
            {
                Console.WriteLine("Restoring state...");
                State = memento.State;
            }
    
            #endregion
        }
    
        /// <summary>
        /// The 'Memento' class.
        /// </summary>
        internal class Memento
        {
            #region Constructors and Destructors
    
            /// <summary>
            /// Initializes a new instance of the <see cref="Memento"/> class.
            /// </summary>
            /// <param name="state">
            /// The state.
            /// </param>
            public Memento(string state)
            {
                State = state;
            }
    
            #endregion
    
            #region Public Properties
    
            /// <summary>
            /// Gets the state.
            /// </summary>
            public string State { get; }
    
            #endregion
        }
    
        /// <summary>
        /// The 'Caretaker' class.
        /// </summary>
        internal class Caretaker
        {
            #region Public Properties
    
            /// <summary>
            /// Gets or sets the memento.
            /// </summary>
            public Memento Memento { get; set; }
    
            #endregion
        }
    }
    
    // Output:
    /*
    State = On
    State = Off
    Restoring state...
    State = On
    
    */

    This real-world code demonstrates the Memento pattern which temporarily saves and then restores the Sales Prospect's internal state.

    /*
     * Real-World Memento Design Pattern.
     */
    
    namespace Memento.RealWorld
    {
        using System;
    
        /// <summary>
        /// Startup class for Real-World Memento Design Pattern.
        /// </summary>
        internal static class Program
        {
            #region Methods
    
            /// <summary>
            /// Entry point into console application.
            /// </summary>
            private static void Main()
            {
                var s = new SalesProspect {Name = "Noel van Halen", Phone = "(412) 256-0990", Budget = 25000.0};
    
                // Store internal state.
                var m = new ProspectMemory {Memento = s.SaveMemento()};
    
                // Continue changing originator.
                s.Name = "Leo Welch";
                s.Phone = "(310) 209-7111";
                s.Budget = 1000000.0;
    
                // Restore saved state.
                s.RestoreMemento(m.Memento);
            }
    
            #endregion
        }
    
        /// <summary>
        /// The 'Originator' class.
        /// </summary>
        internal class SalesProspect
        {
            #region Fields
    
            /// <summary>
            /// The budget.
            /// </summary>
            private double _budget;
    
            /// <summary>
            /// The name.
            /// </summary>
            private string _name;
    
            /// <summary>
            /// The phone.
            /// </summary>
            private string _phone;
    
            #endregion
    
            #region Public Properties
    
            /// <summary>
            /// Gets or sets the budget.
            /// </summary>
            public double Budget
            {
                get { return _budget; }
    
                set
                {
                    _budget = value;
                    Console.WriteLine("Budget: " + _budget);
                }
            }
    
            /// <summary>
            /// Gets or sets the name.
            /// </summary>
            public string Name
            {
                get { return _name; }
    
                set
                {
                    _name = value;
                    Console.WriteLine("Name:  " + _name);
                }
            }
    
            /// <summary>
            /// Gets or sets the phone.
            /// </summary>
            public string Phone
            {
                get { return _phone; }
    
                set
                {
                    this._phone = value;
                    Console.WriteLine("Phone: " + _phone);
                }
            }
    
            #endregion
    
            #region Public Methods and Operators
    
            /// <summary>
            /// Restore memento.
            /// </summary>
            /// <param name="memento">
            /// The memento.
            /// </param>
            public void RestoreMemento(Memento memento)
            {
                Console.WriteLine("\nRestoring state --\n");
                Name = memento.Name;
                Phone = memento.Phone;
                Budget = memento.Budget;
            }
    
            /// <summary>
            /// Save memento.
            /// </summary>
            /// <returns>
            /// The <see cref="Memento"/>.
            /// </returns>
            public Memento SaveMemento()
            {
                Console.WriteLine("\nSaving state --\n");
                return new Memento(_name, _phone, _budget);
            }
    
            #endregion
        }
    
        /// <summary>
        /// The 'Memento' class.
        /// </summary>
        internal class Memento
        {
            #region Constructors and Destructors
    
            /// <summary>
            /// Initializes a new instance of the <see cref="Memento"/> class.
            /// </summary>
            /// <param name="name">
            /// The name.
            /// </param>
            /// <param name="phone">
            /// The phone.
            /// </param>
            /// <param name="budget">
            /// The budget.
            /// </param>
            public Memento(string name, string phone, double budget)
            {
                Name = name;
                Phone = phone;
                Budget = budget;
            }
    
            #endregion
    
            #region Public Properties
    
            /// <summary>
            /// Gets or sets the budget.
            /// </summary>
            public double Budget { get; }
    
            /// <summary>
            /// Gets or sets the name.
            /// </summary>
            public string Name { get; }
    
            /// <summary>
            /// Gets or sets the phone.
            /// </summary>
            public string Phone { get; }
    
            #endregion
        }
    
        /// <summary>
        /// The 'Caretaker' class.
        /// </summary>
        internal class ProspectMemory
        {
            #region Public Properties
    
            /// <summary>
            /// Gets or sets the memento.
            /// </summary>
            public Memento Memento { get; set; }
    
            #endregion
        }
    }
    
    // Output:
    /*
    Name:  Noel van Halen
    Phone: (412) 256-0990
    Budget: 25000
    
    Saving state --
    
    Name:  Leo Welch
    Phone: (310) 209-7111
    Budget: 1000000
    
    Restoring state --
    
    Name:  Noel van Halen
    Phone: (412) 256-0990
    Budget: 25000
    
    */

     

    展开全文
  • Design Pattern - Singleton(C#)

    万次阅读 多人点赞 2019-02-02 16:23:28
    分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!... Definition Ensure a class has only one instance and provides a global point of access to it. ...
  • Design Pattern - Template Method(C#)

    万次阅读 多人点赞 2019-02-26 20:29:04
    分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!... Definition Define the skeleton of an algorithm in an operation, deferring some stepsto subclasses....
  • HTML pattern

    2018-04-19 21:17:03
    pattern pattern属性规定用于验证输入内容的正则表达式,要求用户必须按正则表达式的规则输入信息,防止输入错误。text、search、url、telephone、email、password等类型的文本输入框都支持pattern属性。如,这个...
  • gdb pattern_create | pattern_offset

    千次阅读 2019-09-16 14:16:00
    gdb-peda$ pattern_create 200 gdb-peda$ pattern_offset 0x47414131 首先寻找多少字节能溢出切刚好能够覆盖return addr。我们使用gdb-peda提供的pattern_create和pattern_offset。pattern_create是生成一个字符串...
  • Bayer Pattern

    千次阅读 2018-03-31 20:20:30
    本文为转载,原博客地址...在之前的接触中,一直默认一张图片的每个像素点有RGB三个分量,那“RGGB Bayer Pattern”究竟是什么意思。在网上查找资料以后发现这其实也是非常简单的概念。以下内容大部分为翻译,结合“...
  • =pattern)(?!pattern)(?:pattern)的理解 学习JAVA正则表达式时,(?=pattern)(?!pattern)(?:pattern)这类东西看着就挺难理解,而且官方释义更是越看越不懂。查阅资料后,总结如下。 环视 (?=pattern)与(?!...
  • Word Pattern

    2015-10-08 18:56:05
    Given a pattern and a string str, find if str follows the same pattern.Examples: pattern = “abba”, str = “dog cat cat dog” should return true. pattern = “abba”, str = “dog cat cat fish” ...
  • Bayer pattern

    千次阅读 2015-09-09 11:06:28
    对于SENSOR来说,Bayer RGB和RGB Raw两者的图象结构都是BG/GR的(Bayer pattern说的是COLOR FILTER的结构,分为两种:STD Bayer pattern 与Pair pattern,其中STD Bayer pattern的结构是BG/GR的,而Pair Pattern...
  • Data Pattern

    千次阅读 2015-02-13 14:33:53
    What are pattern Pattern Discovery Computation Uncover what PatternWhat are pattern? Patterns: A set of items, subsequences, or substructures that occur frequently together (or strongly correlated) in
  • pattern+?、pattern*? 这两个比较常用,表示懒惰匹配,即匹配符合条件的尽量短的字符串。默认情况下 + 和 * 是贪婪匹配,即匹配尽可能长的字符串,在它们后面加上 ? 表示想要进行懒惰匹配。 (?!pattern) 表示一个...
  • Valid Pattern Lock

    2017-04-08 09:25:33
    Pattern lock security is generally used in Android handsets instead of a password. The pattern lock can be set by joining points on a 3 × 3 matrix in a chosen order. The points of the matrix are ...
  • @Pattern的用法

    万次阅读 2019-02-21 16:53:26
    @Pattern(regexp = "\\w+$") 把这个注解加在entity的参数上,可以选择分类也可以默认; 关于注解中需要传的参数:一般默认就填入正则表达式即可,但是java中字符串需要转义,这个需要注意一下。 @...
  • 学习正则表达式时,这几个(pattern),(?:pattern),(?=pattern),(?!pattern)不明白* (pattern)匹配pattern并捕获该匹配的子表达式.可以使用 $0...$9属性从结果"匹配"集合中检索捕获的匹配.若要匹配括号字符()...
  • Cache Aside Pattern

    千次阅读 2018-07-11 19:45:00
    在《究竟先操作缓存,还是数据库?》,有同学在评论提出,相关方案违背了“Cache Aside Pattern”的原则,故今天聊一聊Cache Aside Pattern。...
  • Active Object Pattern

    万次阅读 2017-12-04 20:27:46
    Active Object Pattern
  • Pattern Evaluation

    千次阅读 2015-02-17 21:07:20
    Pattern Evaluation@(Pattern Discovery in Data Mining) 本文介绍了数据挖掘中模式挖掘,评估所得模式与规则科学性的方法。
  • (?:pattern) (?=pattern) (?!pattern)

    千次阅读 2016-08-30 22:12:52
    (pattern) 匹配 pattern 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在JScript 中则使用 $0…$9 属性。要匹配圆括号字符,请使用 ‘\(’ 或 ‘\)’。 (?:...
  • Android PatternMatcher

    2017-08-10 11:32:46
    Android PatternMatcher PatternMatcher的构造函数public PatternMatcher(String pattern, int type);pattern是模式字符串 type是匹配类型,共有如下三种 1.PATTERN_LITERAL 按照字面值匹配,忽略特殊字符,区分大...
  • (?:pattern)与(?=pattern)的区别

    千次阅读 2019-04-25 15:31:20
    : pattern)是非捕获型括号 匹配pattern,但不捕获匹配结果。 (pattern )是捕获型括号。 匹配pattern,匹配pattern并捕获结果,自动获取组号 (? < name > pattern ) 匹配pattern, 匹配pattern并捕获结果,设置...
  • SimpleDateFormat的pattern

    千次阅读 2017-10-19 11:43:01
    SimpleDateFormat类pattern详解
  • 为了减少骗分的情况,接下来还要输出子串的前缀...接下来1行,包括length(pattern)个整数,表示前缀数组next[i]的值,数据间以一个空格分隔,行尾无多余空格。 输入样例: ABABABC ABA 输出样例: 1 3 0 0 1
  • pattern.matcher(String s) returns a Matcher that can find patterns in the String s.  pattern.matches(String str) tests, if the entire String (str) matches the pattern. In brief (just to ...

空空如也

1 2 3 4 5 ... 20
收藏数 151,683
精华内容 60,673
关键字:

pattern