public void DecoratorExample() { Console.WriteLine ( "Decorator Pattern\n" ); IComponent component = new Component (); Display ( "1. Basic component: ", component ); Display ( "2. A-decorated : ", new DecoratorA ( component ) ); Display ( "3. B-decorated : ", new DecoratorB ( component ) ); Display ( "4. B-A-decorated : ", new DecoratorB ( new DecoratorA ( component ) ) ); // Explicit DecoratorB DecoratorB b = new DecoratorB ( new Component () ); Display ( "5. A-B-decorated : ", new DecoratorA ( b ) ); // Invoking its added state and added behavior Console.WriteLine ( "\t\t\t" + b.addedState + b.AddedBehavior() ); /* Output Decorator Pattern 1. Basic component: I am walking 2. A-decorated : I am walking and listening to Classic FM - 3. B-decorated : I am walking to school 4. B-A-decorated : I am walking and listening to Classic FM to school 5. A-B-decorated : I am walking to school and listening to Classic FM past the Coffee Shop and I bought a cappuccino */ }
public void BShouldPerformAdditionalPartBWork() { IComponent comp = new Component("Common Part"); DecoratorBase B = new DecoratorB("Part B", comp); B.Operation().Should().StartWith("Hmm Operated by Part B"); B.Operation().Should().EndWith("Operated by Common Part."); }
private static IComponent GetComponent() { IComponent component = new Component(); component = new DecoratorA(component); component = new DecoratorB(component); return(component); }
public static void Main(string[] args) { IComponent decoratorA = new DecoratorA(new Component()); IComponent decoratorB = new DecoratorB(decoratorA); decoratorA.Operation(); Console.WriteLine(); decoratorB.Operation(); }
public void Decorator() { IComponent component = new Component(); DecoratorA decoratora = new DecoratorA(component); DecoratorB decoratorb = new DecoratorB(component); IComponent decoratorab = new DecoratorB(decoratora); Assert.AreEqual(Decorate("Basic Component: ", component), "Basic Component: start value "); Assert.AreEqual(Decorate("Decorator A: ", decoratora), "Decorator A: start value and added value "); Assert.AreEqual(Decorate("Decorator B: ", decoratorb), "Decorator B: start value and other added value "); Assert.AreEqual(Decorate("Decorator A + B: ", decoratorab), "Decorator A + B: start value and added value and other added value "); }
public void DecoratorTest() { { var m = new ConcreteComponent(); var decoratorA = new DecoratorA(m); // decoratorA.Work(); var decoratorB = new DecoratorB(decoratorA); decoratorB.Work(); } { // 我买了个苹果手机 Phone phone = new ApplePhone(); // 现在想贴膜了 var applePhoneWithSticker = new Sticker(phone); // 扩展贴膜行为 applePhoneWithSticker.Print(); // 现在我想有挂件了 var applePhoneWithAccessories = new Accessories(phone); // 扩展手机挂件行为 applePhoneWithAccessories.Print(); Console.WriteLine("----------------------"); // 现在我同时有贴膜和手机挂件了 var sticker = new Sticker(phone); var applePhoneWithAccessoriesAndSticker = new Accessories(sticker); applePhoneWithAccessoriesAndSticker.Print(); } // { // AbstractMessage message = new SqlMessage(); // var de = new CheckUserWrapper(message); // Console.WriteLine(de.Get()[0].Message); // // var d2 = new CheckInputWrapper(message); // Console.WriteLine(d2.Get()[0].Message); // // var m3 = new CheckUserWrapper(message); // var m4 = new CheckInputWrapper(m3); // Console.WriteLine(m4.Get()[0].Message); // } { var dark = new DarkRoast("黑咖啡"); var sugar = new Sugar("加糖", dark); Console.WriteLine($"{sugar.GetDescription()}{sugar.Cost()}元");; var milk = new Milk("加奶", sugar); Console.WriteLine($"{milk.GetDescription()}{milk.Cost()}元");; } }
public static void Main() { Tank tank = new T50(); DecoratorA da = new DecoratorA(tank); da.Shot(); DacoratorB db = new DecoratorB(da); db.Run(); DacoratorC dc = new Decorator(dc); dc.Run(); }
public static void Demo() { Console.WriteLine("Decorator Pattern\n"); IComponent component = new Component(); Display("1. Basic component: ", component); Display("2. A-decorated : ", new DecoratorA(component)); Display("3. B-decorated : ", new DecoratorB(component)); Display("4. B-A-decorated : ", new DecoratorB(new DecoratorA(component))); // Explicit DecoratorB DecoratorB b = new DecoratorB(new Component()); Display("5. A-B-decorated : ", new DecoratorA(b)); // Invoking its added state and added behavior Console.WriteLine("\t\t\t" + b.addedState + b.AddedBehavior()); }
public void DecoratorPattern_TestIfDecoratedResultIsValid() { // Arrange var component = new Component(new DecorationResult()); var decoratorA = new DecoratorA(component); var decoratorB = new DecoratorB(decoratorA); // Act var result = decoratorB.Operation(); // Assert const int excpectedResult = 12; Assert.AreEqual(result.Force, excpectedResult); }
static void Main(string[] args) { Console.WriteLine("Decorator Pattern\n"); IComponent component = new Component(); Display("1. Basic Component: ", component); Display("2. A-Decorated : ", new DecoratorA(component)); Display("3. B-Decorated : ", new DecoratorB(component)); Display("4. B-A-Decorated : ", new DecoratorB(new DecoratorA(component))); // Explicit DecoratorB DecoratorB b = new DecoratorB(component); Display("5. A-B-decorated : ", new DecoratorA(b)); // Invoking its added state and added behavior Console.WriteLine("\t\t\t" + b.addedState + b.AddedBehavior()); }
private static void ExecuteDecorator() { //Decorator; Console.WriteLine("Decorator Pattern\n"); var component = new Decorator(); Display("1. Basic component: ", component); Display("2. A-decorated : ", new DecoratorA(component)); Display("3. B-decorated : ", new DecoratorB(component)); Display("4. B-A-decorated : ", new DecoratorB(new DecoratorA(component))); // Explicit DecoratorB var b = new DecoratorB(new Decorator()); Display("5. A-B-decorated : ", new DecoratorA(b)); // Invoking its added state and added behavior Console.WriteLine("\t\t\t" + b.AddedState + b.AddedBehavior()); }
static void Main() { Console.WriteLine("Decorator Pattern\n"); IDoctor doctor = new Doctor(); Display("1. Basic doctor: ", doctor); Display("2. A-decorated : ", new DentistDecorator(doctor)); Display("3. B-decorated : ", new SurgeonDecorator(doctor)); Display("4. B-A-decorated : ", new SurgeonDecorator(new DentistDecorator(doctor))); // Explicit DecoratorB SurgeonDecorator surgeon = new DecoratorB(new Doctor()); Display("5. A-B-decorated : ", new DecoratorA(b)); // Invoking its added state and added behavior Console.WriteLine("\t\t\t" + b.addedState + b.AddedBehavior()); Console.ReadKey(); }
public void DecoratorPatternCodeTest() { // 在原来的基础上增加功能 Console.WriteLine("在原来的基础上增加功能"); ConcreteComponent concreteComponent = new ConcreteComponent(); DecoratorA decoratorA = new DecoratorA(concreteComponent); DecoratorB decoratorB = new DecoratorB(decoratorA); decoratorB.Operation(); Console.WriteLine("-------------------------------------"); // 覆盖掉原来的功能 Console.WriteLine("覆盖掉原来的功能"); CurrComponent currComponent = new CurrComponent(); currComponent.Operation(); }
public void Testing() { var component = new Component(); var componentTest = component.Operation(); var decoratorA = new DecoratorA(component); var decoratorB = new DecoratorB(component); var decoratorATest = decoratorA.Operation(); var decoratorBTest = decoratorB.Operation(); var decoratorBA = new DecoratorB(new DecoratorA(component)); var decoratorAB = new DecoratorA(new DecoratorB(component)); var decoratorBATest = decoratorBA.Operation(); var decoratorABTest = decoratorAB.Operation(); var result = decoratorB.AddedState + decoratorB.AddedBehavior(); }
public static void Main() { new_Console = new Background_Color(new New_Console()); new_Console = new Text_Color(new_Console); new_Console.WriteLine("Decorator Pattern\n"); IComponent component = new Component(); Display("1. Basic component: ", component); Display("2. A-decorated : ", new DecoratorA(component)); Display("3. B-decorated : ", new DecoratorB(component)); Display("4. B-A-decorated : ", new DecoratorB( new DecoratorA(component))); // Explicit DecoratorB DecoratorB b = new DecoratorB(new Component()); Display("5. A-B-decorated : ", new DecoratorA(b)); // Invoking its added state and added behavior new_Console.WriteLine("\t\t\t" + b.add_state + b.Added_Behavior()); Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); Console.WriteLine("Decorator Pattern:"); Console.WriteLine("=================="); IComponent component = new Component(); Display("1. Basic component: ", component); Display("2. Decorator A: ", new DecoratorA(component)); Display("2. Decorator B: ", new DecoratorB(component)); Display("4. B-A-decorated : ", new DecoratorB(new DecoratorA(component))); DecoratorB b = new DecoratorB(new Component()); Display("5. A-B-decorated : ", new DecoratorA(b)); Console.WriteLine("\t\t\t" + b.addState + b.AddBehavior()); Console.WriteLine("Proxy Pattern:"); Console.WriteLine("=============="); }