Adapter
Adapter
Section titled “Adapter”One-line pattern summary
Section titled “One-line pattern summary”A pattern that converts an incompatible existing interface into the interface expected by the current system.
Typical Unity use cases
Section titled “Typical Unity use cases”- When aligning a legacy or external SDK with the project’s standard API.
- When reusing an existing implementation without modifying it.
Parts (roles)
Section titled “Parts (roles)”- Target
- Adaptee
- Adapter
Unity example (C#)
Section titled “Unity example (C#)”The code below is a simplified Unity example based on the scenario described above.
public interface IAdsService{ void ShowRewardedAd(string placementId);}
public sealed class LegacyAdsSdk{ public void ShowRewardVideo(string zoneId) { }}
public sealed class LegacyAdsServiceAdapter : IAdsService{ private readonly LegacyAdsSdk legacyAdsSdk = new();
public void ShowRewardedAd(string placementId) { legacyAdsSdk.ShowRewardVideo(placementId); }}Advantages
Section titled “Advantages”- It clarifies module boundaries and reduces coupling.
- Features can be extended or integrated without modifying existing code.
Things to watch out for
Section titled “Things to watch out for”- If wrapper layers become too deep, debugging gets harder.
- Interfaces should stay small so responsibility boundaries do not blur.
Interaction diagram
Section titled “Interaction diagram”This shows the flow where an existing interface is converted into the target interface for reuse.