方法级别的抽象

//修改前
public class Customer
{
    public string FirstName { get; set; }
    public string SecondName { get; set; }
    public bool IsPriorityCustomer { get; set; }
    public decimal AnnualIncome { get; set; }
}
public class ProspectiveCustomer
{
    public string FirstName { get; set; }
    public string SecondName { get; set; }
    public decimal AnnualIncome { get; set; }
}

public class ProspectiveCustomerValidator
{
    public Customer CreateValidatedCustomer(
    ProspectiveCustomer prospectiveCustomer)
    {
        if (string.IsNullOrWhiteSpace(
        prospectiveCustomer.FirstName))
        {
            throw new ArgumentException("Invalid FirstName");
        }
        if (string.IsNullOrWhiteSpace(
        prospectiveCustomer.SecondName))
        {
            throw new ArgumentException("Invalid SecondName");
        }
        var newValidCustomer = new Customer
        {
            FirstName = prospectiveCustomer.FirstName,
            SecondName = prospectiveCustomer.SecondName
        };
        if (prospectiveCustomer.AnnualIncome > 100000)
        {
            newValidCustomer.IsPriorityCustomer = true;
        }
        return newValidCustomer;
    }
}
//修改后
using System;

public class Customer
{
    public string FirstName { get; set; }
    public string SecondName { get; set; }
    public bool IsPriorityCustomer { get; set; }
    public decimal AnnualIncome { get; set; }
}
public class ProspectiveCustomer
{
    public string FirstName { get; set; }
    public string SecondName { get; set; }
    public decimal AnnualIncome { get; set; }
}
public class ProspectiveCustomerValidator
{
    // Higher abstraction level
    public Customer CreateValidatedCustomer(
    ProspectiveCustomer prospectiveCustomer)
    {
        EnsureValidDetails(prospectiveCustomer);
        var validatedCustomer =
        CreateNewCustomerFrom(prospectiveCustomer);
        SetCustomerPriority(validatedCustomer);
        return validatedCustomer;
    }
    // Medium abstraction level
    private static void EnsureValidDetails(
    ProspectiveCustomer prospectiveCustomer)
    {
        EnsureValidFirstName(prospectiveCustomer);
        EnsureValidSecondName(prospectiveCustomer);
    }

    private static Customer CreateNewCustomerFrom(
    ProspectiveCustomer prospectiveCustomer)
    {
        return new Customer
        {
            FirstName = prospectiveCustomer.FirstName,
            SecondName = prospectiveCustomer.SecondName,
            AnnualIncome = prospectiveCustomer.AnnualIncome
        };
    }
    // Low abstraction level
    private static void EnsureValidFirstName(
    ProspectiveCustomer prospectiveCustomer)
    {
        if (string.IsNullOrWhiteSpace(prospectiveCustomer.FirstName))
        {
            throw new ArgumentException("Invalid FirstName");
        }
    }
    private static void EnsureValidSecondName(
    ProspectiveCustomer prospectiveCustomer)
    {
        if (string.IsNullOrWhiteSpace(
        prospectiveCustomer.SecondName))
        {
            throw new ArgumentException("Invalid SecondName");
        }
    }
    private static void SetCustomerPriority(Customer customer)
    {

        if (customer.AnnualIncome > 100000)
        {
            customer.IsPriorityCustomer = true;
        }
    }
}
时间: 2024-08-30 18:31:46

方法级别的抽象的相关文章

简单工厂模式,工厂方法模式,抽象工厂模式

简单工厂模式.抽象工厂模式.工厂方法模式,这三种工厂模式都属于设计模式中的创建型模式,它们在形式和特点上也多少有些相似,其最终目的都是帮我们将对象的实例化部分取出来,进而优化系统架构,增强系统的扩展性,也就是说更好的体现开放封闭原则. 简单工厂模式: 概念: 简单工厂模式是类的创建模式,又叫做静态工厂方法模式,是由一个工厂类根据传入的参量决定创建出哪一种产品类的实例,涉及到工厂角色.抽象产品角色以及具体产品角色. 结构图: 分析: 一: 简单工厂类是整个模式的关键所在,包含了必要的逻辑判断,根据

PHP简单工厂模式、工厂方法模式和抽象工厂模式比较

PHP工厂模式概念:工厂模式是一种类,它具有为您创建对象的某些方法.您可以使用工厂类创建对象,而不直接使用 new.这样,如果您想要更改所创建的对象类型,只需更改该工厂即可.使用该工厂的所有代码会自动更改. 根据抽象程度不同,PHP工厂模式分为:简单工厂模式.工厂方法模式和抽象工厂模式 简单工厂模式: /******代码在原文还是git上都有osgit地址 https://git.oschina.net/mjw/pattern.git  *******/ /** *简单工厂模式与工厂方法模式比较

工厂方法模式And抽象工厂模式

一.工厂方法模式 简单工厂模式的缺陷就是类的创建太过于依赖工厂,一旦需要进行程序扩展,就必须修改工厂类的代码 这违背了背包原则. 而工厂方法模式针对这一问题进行了改进. public interface Sender { public void send(); } public interface producer { public Sender produce(); } public class MailFactory implements producer{ public Sender pr

Spring3.1 对Bean Validation规范的新支持(方法级别验证)

一.Bean Validation框架简介 写道 Bean Validation standardizes constraint definition, declaration and validation for the Java platform. 大体意思是:Bean Validation 标准化了Java平台的约束定义.描述.和验证. 详细了解请参考:http://beanvalidation.org/ Bean Validation现在一个有两个规范: 1.Bean Validatio

人人都能看懂的工厂方法模式和抽象工厂模式的区别

设计模式中的工厂方法模式和抽象工厂模式一直不知不觉地使用在程序设计中,但是就是经常只有实践没有进行理论指导,所以感觉有一点半路子出家而没有系统学习过的不踏实的感觉.现在总结了一下之前项目中使用过的抽象工厂模式,加强对创建型模式中工厂模式的理解. 先举个栗子感性的理解工厂方法模式和抽象工厂模式这两者的区别. 有一个富商,在深圳开了三个手机厂,分别代工苹果手机,三星手机,华为手机.富商经常视察这3个厂,在苹果手机厂就说"大家要做好苹果手机",在三星厂就说"大家要做好三星手机&qu

PHP简单工厂模式、工厂方法模式和抽象工厂模式

PHP工厂模式概念:工厂模式是一种类,它具有为您创建对象的某些方法.您可以使用工厂类创建对象,而不直接使用 new.这样,如果您想要更改所创建的对象类型,只需更改该工厂即可.使用该工厂的所有代码会自动更改.根据抽象程度不同,PHP工厂模式分为:简单工厂模式.工厂方法模式和抽象工厂模式 简单工厂模式: /** *简单工厂模式与工厂方法模式比较. *简单工厂又叫静态工厂方法模式,这样理解可以确定,简单工厂模式是通过一个静态方法创建对象的. */ interface people { function

简单工厂、工厂方法模式、抽象工厂模式

只为每天积累一点点... 简单工厂.工厂方法模式.抽象工厂模式的简单原理. 一.引子 话说十年前,有一个爆发户,他家有三辆汽车(Benz(奔驰).Bmw(宝马).Audi(奥迪)),还雇了司机为他开车.不过,爆发户坐车时总是这样:上Benz车后跟司机说“开奔驰车!”,坐上Bmw后他说“开宝马车!”,坐上 Audi后他说“开奥迪车!”.你一定说:这人有病!直接说开车不就行了?!而当把这个爆发户的行为放到我们程序语言中来,我们发现C语言一直是通过这种方式来坐车的!幸运的是这种有病的现象在OO语言中可

Visual Studio 实用扩展推荐   Visual Studio 拥有非常不错的可扩展性,在之前的文章中,我也给大家示范了如何进行编辑器的扩展(详见文末参考资源)。在本篇文章中,我将介绍几款非常实用的扩展,从而帮助我们提高开发效率。 C# outline   Visual Studio 默认的大纲方案只允许在方法级别及以上进行代码的折叠,无法对一个if、while的区块进行折叠

Visual Studio 实用扩展推荐 Visual Studio 拥有非常不错的可扩展性,在之前的文章中,我也给大家示范了如何进行编辑器的扩展(详见文末参考资源).在本篇文章中,我将介绍几款非常实用的扩展,从而帮助我们提高开发效率. C# outline Visual Studio 默认的大纲方案只允许在方法级别及以上进行代码的折叠,无法对一个if.while的区块进行折叠,而这款工具则正好弥补了这个问题. highlight all occurrences of selected word

工厂模式:简单工厂模式、工厂方法模式和抽象工厂模式

我们一般制造对象时,采用操作符new来进行创建.但是慢慢我们了解到实例化这个活动不应该总是公开地进行,同时初始化还经常造成"耦合"的问题. 如果我们不希望出现上述问题,那么我们就有必要认识一下"工厂模式",它将有助于我们从复杂的依赖中解脱出来. 1)为什么说"new"不好? 当看到"new",就会想到"具体". 我们不应该总是针对实现编程,但是当我们每次使用new时,正是在针对实现编程而不是接口,这很不符合