samedi 15 juin 2019

Design Pattern for attaching new behaviors to classes statically?

The decorator pattern provides a nifty way of attaching additional responsibilities to an object dynamically. Observe the solution below with a great resemblance to the decorator pattern. In this solution, the additional responsibilities are however not added dynamically. Would this still be considered as the decorator pattern or something else?

    public class SomeClass {
        public SomeClass() {}
        public void SomeMethod() {}
    }

    public abstract class ExtensionOfSomeClass : SomeClass {
        protected ExtensionOfSomeClass() : base() {}
        public abstract void SomeOtherMethod();
    }

    public class ConcreteExtension1 : ExtensionOfSomeClass{
        public override void SomeOtherMethod() {
            Console.WriteLine("1");
        }
    }

    public class ConcreteExtension2 : ExtensionOfSomeClass{
        public override void SomeOtherMethod() {
            Console.WriteLine("2");
        }
    }

Aucun commentaire:

Enregistrer un commentaire