0

假设我有很多业务逻辑来确定基于多个因素的应用程序行为。此外,我有一些非常好的地方,我知道我可以用策略模式替换行为。另外,考虑到我正在尝试利用各种模式来解决问题

  • 策略模式
  • 规范模式
  • 工厂模式

是否可以使用规范模式来确定工厂中的策略并保留开放封闭原则?

我有一个工厂,我发现自己像以前的代码一样创建 switch 语句来选择正确的策略。这似乎适得其反。

我想将所有这些逻辑决策都推到规范中,但随后出现了关于订购规范或首先选择最明确的规范的问题。

知道如何解决这个问题吗?

4

1 回答 1

0
using System;
using System.Collections.Generic;
using System.Linq;

using static BeyondOOP.Gender;

// having to make decisions based on logic
// control flow without all the nested if then
namespace BeyondOOP
{
    class Program
    {
        static void Main(string[] args)
        {
            var people = new List<Person>
            {
                new Person("Bill", new DateTime(1940, 10, 10), Male),
                new Person("Jill", new DateTime(1950, 10, 10), Female),
                new Person("Mary", new DateTime(2015, 10, 10), Female),
                new Person("Gary", new DateTime(1970, 10, 10), Male),
                new Person("Greg", new DateTime(1980, 10, 10), Male),
                new Person("Susan", new DateTime(2013, 10, 10), Female),
                new Person("Gabe", new DateTime(1999, 10, 10), Neutral),
                new Person("Barbie", new DateTime(2000, 10, 10), Female),
            };
            var greeter = new PersonGreeter();
            people.ForEach(greeter.Greet);
        }
    }

    // a little 'generics' flair
    // a 'predicate' is just a way
    // to predetermine true false
    interface IPredicate<T>
    {
        bool Matches(T value);
    }

    // this is just a simple DTO
    // Data transfer object, fancy for value container
    // it's just a way of passing values around
    class Person
    {
        public string Name { get; }
        public DateTime DateOfBirth { get; }
        public Gender Gender { get; }

        public Person(string name, DateTime dateOfBirth, Gender gender)
        {
            Name = name;
            DateOfBirth = dateOfBirth;
            Gender = gender;
        }
    }

    enum Gender { Male, Female, Neutral }

    // some prefabed predicates for 'Person'
    class OldManPredicate : IPredicate<Person>
    {
        public bool Matches(Person value) =>
            (DateTime.Now.Year - value.DateOfBirth.Year) >= 60 && 
                value.Gender == Gender.Male;
    }

    class MinorPredicate : IPredicate<Person>
    {
        public bool Matches(Person value) =>
            DateTime.Now.Year - value.DateOfBirth.Year < 18;
    }

    class ToddlerPredicate : IPredicate<Person>
    {
        public bool Matches(Person value) =>
            DateTime.Now.Year - value.DateOfBirth.Year > 2 &&
            DateTime.Now.Year - value.DateOfBirth.Year < 4;
    }

    class BabyPredicate : IPredicate<Person>
    {
        public bool Matches(Person value) =>
            DateTime.Now.Year - value.DateOfBirth.Year <= 2;
    }

    class TruePersonPredicate : IPredicate<Person>
    {
        public bool Matches(Person value) => true;
    }

    class GreetAction
    {
        public Func<Person,bool> Predicate { get; }
        public Func<Person,string> Messager { get; }

        public GreetAction(Func<Person, bool> predicate, 
            Func<Person, string> messager  )
        {
            Predicate = predicate;
            Messager = messager;
        }
    }
    class PersonGreeter
    {
        // someone may say that if you're using Func
        // why do you go through the trouble of the extra
        // classes. Predicate classes can be added freely
        // Only this factory class needs to be changed
        // and if I was to create a factory for the predicates 
        // and messagers then no changes would be neccessary
        private readonly List<GreetAction> _greetActions =
            new List<GreetAction>
            {
                // these have to be ordered
                // so they don't conflict or override
                // intended behavior
                // MinorPredicate is < 18 but babies and toddlers are also
                // less than 18, they need to preceed minors to work
                new GreetAction( new OldManPredicate().Matches, 
                    p => $"{p.Name}, you're getting up there!"),
                new GreetAction( new BabyPredicate().Matches,
                    p => $"{p.Name}, time to change the diaper!"),
                new GreetAction( new ToddlerPredicate().Matches,
                    p => $"{p.Name}, look, you're walking!"),
                new GreetAction( new MinorPredicate().Matches, 
                    p => $"{p.Name}, no, you may not have beer!"),
                // you need a default action
                // this is the same as a 'Null Pattern'
                new GreetAction( new TruePersonPredicate().Matches,
                    p => $"{p.Name}, hello there!"),
            }; 

        public void Greet(Person person) => 
            Console.WriteLine(_greetActions
                .First(p => p.Predicate(person))
                .Messager(person));
    }
}
于 2016-03-10T22:20:04.127 回答