# 不必要的抽象

##为什么不可以有不必要的抽象？

##不必要的抽象的潜在原因

## 示例分析一

```public class FormattableFlags
{
/// <summary>
/// 禁止显式实例化这个类
/// </summary>
private FormattableFlags()
{
}
/// <summary>
/// 将输出左对齐
/// </summary>
public  const int LEFT_JUSTIFY = 1;
/// <summary>
/// 将输出转换为大写
/// </summary>
public const int UPPERCASE = 2;
/// <summary>
/// 要求输出使用替换类型
/// </summary>
public const int ALTERNATE = 3;
}```

```public enum FormattableFlagsEnum
{
/// <summary>
/// 将输出左对齐
/// </summary>
LEFT_JUSTIFY = 1,
/// <summary>
/// 将输出转换为大写
/// </summary>
UPPERCASE = 2,
/// <summary>
/// 要求输出使用替换类型
/// </summary>
ALTERNATE = 3
}```

## 示例分析二

```/// <summary>
/// 图书类
/// </summary>
public class Book
{
/// <summary>
/// 价格
/// </summary>
public decimal Price { get; private set; }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="price"></param>
public Book(decimal price)
{
this.Price = price;
}
/// <summary>
/// 修改图书价格
/// </summary>
/// <param name="price">图书价格</param>
public void ModifyPrice(decimal price)
{
this.Price = price;
}
}
/// <summary>
/// 畅销图书类
/// </summary>
public  class BestSellerBook
{
private Book book = null;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="price"></param>
public BestSellerBook(decimal price)
{
book = new Book(price);
}
/// <summary>
/// 修改图书价格
/// </summary>
/// <param name="price">图书价格</param>
public void ModifyPrice(decimal price)
{
book.ModifyPrice(price);
}
}```

```/// <summary>
/// 图书类
/// </summary>
public class Book
{
/// <summary>
/// 价格
/// </summary>
public decimal Price { get; private set; }
/// <summary>
/// 是否畅销书
/// </summary>
public bool IsBestSeller { get; private set; }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="price">价格</param>
/// <param name="isBestSeller">是否畅销书</param>
public Book(decimal price, bool isBestSeller)
{
this.Price = price;
this.IsBestSeller = isBestSeller;
}
/// <summary>
/// 修改图书价格
/// </summary>
/// <param name="price">图书价格</param>
public void ModifyPrice(decimal price)
{
this.Price = price;
}
/// <summary>
///  修改是否畅销书
/// </summary>
/// <param name="isBestSeller">是否畅销书</param>
public void ModifyIsBestSeller(bool isBestSeller)
{
this.IsBestSeller = isBestSeller;
}
}```

###设计模式中的委托抽象

## 总结：

1. 包含多余的抽象会增加设计的复杂性，影响整个设计的可理解性。
2. 职责独特而明确的抽象有可能得到重用，而当抽象不承担任何职责或承担的职责微不足道时，就不太可能在其它地方重用。 ​

0 条评论

911

751

1915

981

3698

1603

2083

1012

2245

2113