Click here to Skip to main content
15,888,088 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi, what are the possible ways in C# to solve this kinda task? Example:
C#
// CODE BELOW CANNOT BE MODIFIED, TREAT IT AS A 3TH PARTY LIBRARY
    public interface IDoors
    {
        void PullHandle();
    }
    
    class NormalDoors : IDoors
    {
        public void PullHandle()
        {
            Console.WriteLine("Normal doors open");
        }
    }
    
    public static class Doorman
    {
        public static void Open(IDoors doors)
        {
            doors.PullHandle();
        }
    }
    
    public sealed class FancyDoors
    {
        public void PressButton()
        {
            Console.WriteLine("Fancy doors are open");
        }
    }
    
//BELOW YOU CAN ADD CODE TO SOLVE THE PROBLEM

    
    public static class Doors
    {
        public static void Main()
        {
            var myNormalDoors = new NormalDoors();
            var myFancyDoors = new FancyDoors();
    
            // This works, since normal doors implement IDoors
            Doorman.Open(myNormalDoors);
            
            //Doesn't work, because Fancy Doors are sealed and do not                            implement IDoors. How to bypass that problem?
            Doorman.Open(myFancyDoors );
        }
    }
}


So the problem is basically how to extend a sealed class (let's say it's a 3th party library) to open Fancy Doors through a Doorman?

What I have tried:

Possible ideas:
- extension method? Probably not.
- Composition?
- Adapter pattern?
Posted
Updated 28-May-23 1:15am
Comments
Graeme_Grant 28-May-23 6:45am    
Ah, home work ... Here is a resource that will help you: Refactoring and Design Patterns[^]
[no name] 28-May-23 6:46am    
Doesn't help, I have read all refactoring guru, there is no case when a class is sealed, all it has are "easy examples", which doesn't apply to this case.
Graeme_Grant 28-May-23 7:09am    
It does, you're looking at the problem too closely... It is not about it being a sealed class.

You mention Adapter, but that is like a Lego set... That website uses imagery, so you should be able to work it out. Use this page: The Catalog of Design Patterns[^]
[no name] 28-May-23 7:14am    
Which pattern are you suggesting? Otherwise it's just sending links with nothing. You might as well send google.com. Either you have something useful to say here or not...
[no name] 28-May-23 7:15am    
And yes it is about a sealed class, without it, the solution is straightforward.

1 solution

Thinking about it, one of the patterns mentioned above could be used.

The Adapter pattern allows you to adapt the interface of an existing class to match the interface that the client expects. In this case, you can create an adapter class that implements the IDoors interface and internally uses the FancyDoors class to perform the required actions.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900