Multiple Implementation in C#

Multiple Implementation in C#
By Craig Breakspear
Can you inherit from multiple classes in C#? Simply put, this cannot be done. However there are ways around it. From a design perspective you must ask yourself, Will a Class fully represent an object? Meaning that, if we have a base class with abstract methods designed for a particular application and we know that the inheriting object will only need the methods defined in that class. We now have a valid design pattern here.

The Vehicle Car Object

Lets say we have an abstract class called "Vehicle" as well as another class called "ConstructionVehicle". The vehicle class has methods such as Accelerate() , Stop(), and the "ConstructionVehicle" class has methods such as ExecuteDump() and TurnOnBackUpSound(). If we were only going to build a Car object and know we would only use those methods from the "Automobile" class this would be fine.

The DumpTruck Object

Now we want to create another object called "DumpTruck". We could inherit from the Automobile class but that class does not have the methods that we need called ExecuteDump() and TurnOnBackUpSound(). If we were using a language such as C++ we could easily inherit from both classes using multiple inheritance. However, seeing C# is our language of choice, multiple inheritance is not an option, you may only inherit from one Base Class.

From Abstract Classes to Interfaces

From a design perspective we must choose a different design. C# supports what is called "Multiple Implementation", which is to says a class can implement more than one interface. Our design now changes the "Vehicle" class and the "ConstructionVehicle" class into interfaces. Below we have defined the two interfaces with their very simplistic methods:

ie:

interface IConstructionVehicle
{
void ExecuteDump();
void TurnOnBackUpSound();
}
interface IVehicle
{
void Accelerate();
void Stop();
void TurnOnBackUpSound();
}

If we built a class that inherited from these two interfaces we would be able to do so spanning multiple inherited interfaces. Design problem solved! Or is it?
Explicit Interface Implementation

If you look at both interfaces defined above you'll notice that they share in common a method of the same name "TurnOnBackUpSound()". Problem? No, in fact C# supports what is known as "Explicit Interface Implementation", which allows the programmer to specify which member of which interface they want to use. Putting the Interface name in front of the member name allows this to happen as shown below.

public class DumpTruck: IEngine, IBody
{

void IEngine.Test()
{
  Console.WriteLine("This is the Engine TEst");
}
void IBody.Test()
{
  Console.WriteLine("This is the Body TEst");
}
}

Implementation Hiding
Another benefit to this technique is something called "Implementation Hiding". Implementation Hiding allows the methods from the implemented interface to be hidden from the derived class unless the developer explicitly calls the interface. This technique obviously reduces the clutter for a developer.

时间: 2024-09-19 09:33:58

Multiple Implementation in C#的相关文章

Operating Principle and Implementation of Flink: Memory Management

Nowadays, open-source big data frameworks (such as Hadoop, Spark and Storm) all employ JVM, and Flink is one of them. JVM-based data analysis engines all need to store a large amount of data in the memory, so they have to address the following JVM is

iOS 4 iPhone Database Implementation using SQLite [转]

What is SQLite? SQLite is an embedded, relational database management system (RDBMS). Most relational databases (Oracle and MySQL being prime examples) are standalone server processes that run independently, and in cooperation with, applications that

Memory Layout for Multiple and Virtual Inheritance (By Edsko de Vries, January 2006)

原文地址:http://www.phpcompiler.org/articles/virtualinheritance.html In this article we explain the object layout implemented by gcc for multiple and virtual inheritance. Although in an ideal world C++ programmers should not need to know these details of

SQL Server 2005 中的 Multiple Active Result Set (MARS)

简介 所有 SQL Server 数据访问应用程序编程接口 (API) 都提供了一个抽象来表示会话和会话中的请求.SQL Server 2000 以及更早的版本限制编程模型,它要求任何时候一个给定的会话中最多只能有一个待定的请求.有几个替代办法被用来解决这种限制,在这些替代办法中,最常见的可能就是服务器端光标.SQL Server 2005 实现了 Multiple Active Result Set (MARS),它解除了这个约束.本文介绍了 MARS 的设计.结构和语义变更,以及为了从这些改

例子:Multiple Selection

select <?phpfunction php_self2url(){   global $PHP_SELF;   $tmp = eregi_replace("%2f","/",urlencode($PHP_SELF));   return $tmp;}?><form action="<?php echo php_self2url();?>" method="post"><p>

java学习笔记--Hiding implementation

笔记 hiding the implementation这句话讲得很好:In any relationship, it's important to have boundaries that are respected by all parties involved. 不管你们是什么关系,有一个彼此尊重的界限是很重要的. #----CLASSPATH(要大写,最好加上当前目录.):java 解释器会在以下两个地方查找.class文件: 1. jdk所在的标准目录 2. 路径 = 以CLASSPA

3D编程:Multiple Lights

Multiple Lights 到目前为止,所有的effect中都只使用了一种光源(不算ambient light).但没有理由说明不能在同一种effect中结合使用directinal,point和spotlights,或者多个相同类型的光源.同时使用多个光源主要瓶颈在于性能问题以及一个shader模型中可用的指令数量.列表7.4和7.5列出了支持多种光源的effect的代码.这段代码没什么新的构造,因此首先讨论更新后的Common.fxh文件,该文件中增加了一些新的结构体和通用函数.然后再深

C++中多重继承(multiple inheritance) 的名称歧义(name ambiguity)

在多重继承中, 如果多个基类包含相同名字的成员函数, 则在派生类使用时, 容易发生歧义, 会导致出错; 解决方法是: 在派生类中重写基类方法, 覆盖原方法, 再指定基类范围(scope), 确定使用那个基类的方法, 可以避免歧义; 代码如下: /* * cppprimer.cpp * * Created on: 2014.1.10 * Author: Spike */ /*eclipse cdt, gcc 4.8.1*/ #include <iostream> #include <str

jQuery实现select multiple左右添加和删除功能

项目要实现这样的一个功能(如下图所示):选择左边下拉列表框中的选项,点击添加按钮,把选择的选项 移动到右边的下拉列表框中,同样的选择右边的选项,点击删除按钮,即把选择的选项移动到左边的下拉列 表框中.相信用js很多朋友都写过,下面是我用jQuery来实现这样的功能的.具体代码如下: 下拉列表 <table width="95%" cellpadding="0" align="center" class="listshow"