あなたの質問は非常に幅広く、コメントに@doogleが記載しているように、抽象クラスとインターフェイスは必ずしも特定のタイプのプロジェクトに結びついているわけではありませんが、デザインに基づいています。
あなたの電子商取引や店舗管理アプリケーションの設計を知らなくても、具体的な助けをすることは不可能ですが、以前の質問の1つを読んで、抽象クラスとインターフェイスがあなたを助けることができるように、正しい方向に向けるかもしれない何かの基本的な例がここにあります。
仮想ストアでは、数千の異なる製品があり、アプリケーションでそれらを追跡したいと考えています。製造元、価格、部門、数量などのすべての製品に共通することは明らかですが、特定の製品にのみ適用できる情報もあります(衣類はサイズはありますが、DVDはありません)。
解決策は、すべての製品に共通のProduct
インターフェイスを作成し、各クラスがインターフェイスを実装し、独自のプロパティを追加できるようにすることです。これで、どの製品クラスでも最低限これらの共通メンバーが保有されることが保証されます。そして、あなたがIProduct
を実装する抽象クラスを作成することができ
:
はここで架空の製品インターフェースです。ここでIProduct
を実装衣料品ですが、また、あなたが行くとストア内のアイテムのいくつかのクラスを作成することができます今、あなたはClothingProduct
クラスを持っていることをSize
フィールド
public abstract class ClothingProduct : IProduct
{
public string Size { get; set; }
public decimal SellingPrice { get; set; }
public decimal StoreCost { get; set; }
public int QuantityOnHand { get; set; }
public string Manufacturer { get; set; }
public string DepartmentName { get; set; }
// by marking CalculateProfits abstract, you can let every class that inherits
// from ClothingProduct decide how to calculate the profit based on the selling
// costs of that product
public abstract decimal CalculateProfit();
}
を追加します。彼らは順番にIProduct
を実装ClothingProduct
から継承するため、エンドで
public class Shirt : ClothingProduct
{
public override CalculateProfit()
{
return this.SellingPrice - this.StoreCost - this.CalculateSellingCosts();
}
private decimal CaclulateSellingCosts()
{
// some code that would let you calculate the selling costs and
// overhead costs associated with this specific product
}
}
public class Shoes : ClothingProduct
{
private const decimal commissionRate = 0.05;
public override CalculateProfit()
{
return this.SellingPrice - this.StoreCost - this.CalculateSellingCosts() - this.CalculateCommission();
}
private decimal CaclulateSellingCosts()
{
// some code that would let you calculate the selling costs and
// overhead costs associated with this specific product
}
private decimal CalculateCommission()
{
return this.SellingPrice * commissionRate;
}
}
、両方Shoes
とShirt
IProducts
をされています。ここでは靴とシャツクラスです。
ストア内の他の製品用に他のクラスまたは抽象クラスを作成することはできますが、IProduct
の実装では、IProduct
のすべてのプロパティが保証されます。このことができます
はすべて2つのクエリがちょうど不自然な例それはちょうどように簡単どおりですIProduct
// a hypothetical method that grabs everything (maybe from a database)
List<IProduct> products = GetAllProducts();
// this query would give you the total number of items in your inventory
var totalItems = products.Select(quant => quant.QuantityOnHand).Sum();
// and this would calculate the total value of the products based on the store's cost
var totalCost = products.Select(quant => quant.StoreCost).Sum();
を実装するので、あなたも、あなたのインターフェイスタイプのコレクションを作成し、そこにすべての製品を追加することができていますデータベース内でこれを行う(それがeverythignがどこにストアされているのか分かりませんが)、私はちょうど1つのインターフェースに関連するすべてのアイテムをどのようにつなぎ合わせることができるのかの例を教えてくれました
この質問はあまりにも自由です。抽象化は、どのタイプのプロジェクトにも固有のものではなく、オブジェクトモデル全体の設計に関連しています。 – doogle
この質問の問題は、あなたが要求している回答があなたに役に立たないことです。あなたはどのように知っているかもしれませんが、なぜあなたはその理由を持っているようには見えません。理由を知っていれば、どんなアプリケーションでも候補者があなたに飛び乗るでしょう。抽象化を使用するべきではないときは、停止する必要があります... –