在阅读有关设计模式时,有人偶然发现了这个短语。
但是我不明白,有人可以帮我解释一下吗?
最佳答案
根据接口(interface)方式进行编码,客户端代码始终包含一个由工厂提供的Interface对象。工厂返回的任何实例的类型都是任何工厂候选类都必须实现的Interface类型。这样,客户端程序就不必担心实现,并且接口(interface)签名确定了可以完成所有操作的方式。这可用于在运行时更改程序的行为。从维护的 Angular 来看,它还可以帮助您编写更好的程序。
这是为您准备的基本示例。
public enum Language
{
English, German, Spanish
}
public class SpeakerFactory
{
public static ISpeaker CreateSpeaker(Language language)
{
switch (language)
{
case Language.English:
return new EnglishSpeaker();
case Language.German:
return new GermanSpeaker();
case Language.Spanish:
return new SpanishSpeaker();
default:
throw new ApplicationException("No speaker can speak such language");
}
}
}
[STAThread]
static void Main()
{
//This is your client code.
ISpeaker speaker = SpeakerFactory.CreateSpeaker(Language.English);
speaker.Speak();
Console.ReadLine();
}
public interface ISpeaker
{
void Speak();
}
public class EnglishSpeaker : ISpeaker
{
public EnglishSpeaker() { }
#region ISpeaker Members
public void Speak()
{
Console.WriteLine("I speak English.");
}
#endregion
}
public class GermanSpeaker : ISpeaker
{
public GermanSpeaker() { }
#region ISpeaker Members
public void Speak()
{
Console.WriteLine("I speak German.");
}
#endregion
}
public class SpanishSpeaker : ISpeaker
{
public SpanishSpeaker() { }
#region ISpeaker Members
public void Speak()
{
Console.WriteLine("I speak Spanish.");
}
#endregion
}
编辑
我已经更新了上面的示例,并添加了一个抽象的
Speaker
基类。在此更新中,我向“SayHello”的所有发言人添加了一项功能。所有发言人都说“Hello World”。这是具有相似功能的共同特征。引用类图,您会发现Speaker
抽象类实现了ISpeaker
接口(interface),并将Speak()
标记为抽象,这意味着每个Speaker实现都负责实现Speak()
方法,因为它从Speaker
到Speaker
有所不同。但是所有发言者都一致说“你好”。因此,在抽象的Speaker类中,我们定义了一个表示“Hello World”的方法,每个Speaker
实现都将派生SayHello()
方法。考虑一种情况,即
SpanishSpeaker
无法说声招呼,因此在这种情况下,您可以覆盖西类牙语使用者的SayHello()
方法并提出适当的异常(exception)。而且,我们可以通过在每个实现中简单地添加一个基本抽象类Speaker和一些小的修改来实现此行为,从而使原始程序保持不变。这是任何应用程序都需要的功能,它使您的应用程序易于维护。
public enum Language
{
English, German, Spanish
}
public class SpeakerFactory
{
public static ISpeaker CreateSpeaker(Language language)
{
switch (language)
{
case Language.English:
return new EnglishSpeaker();
case Language.German:
return new GermanSpeaker();
case Language.Spanish:
return new SpanishSpeaker();
default:
throw new ApplicationException("No speaker can speak such language");
}
}
}
class Program
{
[STAThread]
static void Main()
{
//This is your client code.
ISpeaker speaker = SpeakerFactory.CreateSpeaker(Language.English);
speaker.Speak();
Console.ReadLine();
}
}
public interface ISpeaker
{
void Speak();
}
public abstract class Speaker : ISpeaker
{
#region ISpeaker Members
public abstract void Speak();
public virtual void SayHello()
{
Console.WriteLine("Hello world.");
}
#endregion
}
public class EnglishSpeaker : Speaker
{
public EnglishSpeaker() { }
#region ISpeaker Members
public override void Speak()
{
this.SayHello();
Console.WriteLine("I speak English.");
}
#endregion
}
public class GermanSpeaker : Speaker
{
public GermanSpeaker() { }
#region ISpeaker Members
public override void Speak()
{
Console.WriteLine("I speak German.");
this.SayHello();
}
#endregion
}
public class SpanishSpeaker : Speaker
{
public SpanishSpeaker() { }
#region ISpeaker Members
public override void Speak()
{
Console.WriteLine("I speak Spanish.");
}
public override void SayHello()
{
throw new ApplicationException("I cannot say Hello World.");
}
#endregion
}