What is the need for Adapter Pattern in C#?

If you want to convert interface of your class into a different interface based on Client expectations, then you can do it using adapter pattern. These two interfaces will then work with one another to achieve the desired behavior.

[フレーム]

(追記) (追記ここまで)



By this way, you can make two classes work with one another even if they are incompatible. Here is a code template demonstrating usage of adapter pattern to make the interfaces of sampleClass1 and sampleClass2 work together:

class sampleClass1 {
public virtual void requestMethod() {
Console.WriteLine(“Executing requestMethod of sampleClass1”);
}
}
class adapterClass:sampleClass1 {
private sampleClass2 sample2Obj = new sampleClass2();
public override void requestMethod() {
sample2Obj.requestMethod1();
}
}
class sampleClass2 {
public void requestMethod1() {
Console.WriteLine(“Executing requestMethod of sampleClass2”);
}
}

(追記) (追記ここまで)

AltStyle によって変換されたページ (->オリジナル) /

| Can you call a constructor from another constructor of the Class in .NET? | Difference between Response.Output.Write() method and Response.Write() method in .NET | How do you establish multiple inheritance in C#? | How do you introduce a ReadOnly property in C#? | How do you perform constructor overloading in C#? | Is catch(Exception) recommended to be used in .NET? | What are the different access modifiers available in C#? | What are the different ways of overloading in C#? | What are the members of stringbuilder class in C#? | What is Multicast Delegate? Explain it with example in C# | What is the difference between abstract class and interface in .NET? | What is the difference between Clone and CopyTo methods in .NET | What is the difference between const and readonly in .NET | What is the difference between directcast and ctype in .NET? | What is the difference between out and ref parameters in .NET | What is the difference between public assembly and private assembly in .NET | What is the difference between strong typing and weak typing in .NET? | What is the difference between Trace and Debug in .NET | What is the need for Abstract Factory Pattern in C#? | What is the need for Adapter Pattern in C# |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2025 - All Rights Reserved.