Big talk design pattern-modifier pattern C # problems compared with Java
I recently read the decorator mode in the big talk Design Model Book and wrote it again with C #. I found that the running results are different from those in the book, then I wrote the same code as the book in Java, different compilers and runtime environments, Java and. NET
First, Java Implementation
Beverage (Beverage abstract class)
CondimentDecorator (this class inherits Beverage)
Latte (Latte Beverage, inheriting Beverage)
Mocha (Moka seasoning, inheriting CondimentDecorator)
public abstract class Beverage { public String description = Unkonw; public String getDescription(){ return description; } public abstract double cost();}
public abstract class CondimentDecorator extends Beverage { public abstract String getDescription();}
public class Latte extends Beverage { public Latte(){ description = Latte Coffee; } @Override public double cost() { // TODO Auto-generated method stub return 21.05; }}
public class Mocha extends CondimentDecorator { Beverage beverage; public Mocha(Beverage beverage){ this.beverage = beverage; } @Override public String getDescription() { // TODO Auto-generated method stub return beverage.getDescription() + , Mocha Condiment; } @Override public double cost() { // TODO Auto-generated method stub return .99 + beverage.cost(); }}
Public static void main (String [] args) {// TODO Auto-generated method stub Beverage beverage1 = new Latte (); beverage1 = new Mocha (beverage1); System. out. println (little bright: + beverage1.getDescription () + coffee, spent: + beverage1.cost () + RMB );}
Running result
Let's take a look at the implementation in C #. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3ryb25np1_vcd4ncjxwpkjldmvyywdlo6js + 8HPs + expires/expires + nt8ijrLzMs9BCZXZlcmFnZaOpPGJyIC8 + expires = "brush: java;"> public abstract class Beverage { public string description = Unknow; public string getDescription() { return description; } public abstract double cost(); }
public abstract class CondimentDecorator:Beverage { public abstract string getDescription(); }
Public class Espresso: Beverage {public Espresso () {description = Espresso Coffee (Espresso);} public override double cost () {return 19.00 ;}}
Public class SoyMilk: CondimentDecorator {private Beverage beverage; public SoyMilk (Beverage bev) {this. beverage = bev;} public override string getDescription () {return beverage. getDescription () + SoyMilk (soy milk seasoning);} public override double cost () {return 1.25 + beverage. cost ();}}
Public static void Main (string [] args) {Beverage espresso = new Espresso (); espresso = new SoyMilk (espresso); Console. writeLine (James ordered a cup: + espresso. getDescription () + coffee, Price: + espresso. cost () + RMB); Console. readLine ();}
As you can see, except for the beverage name, the Code is the same. below is the running result
The comparison with the above Java results is indeed different. Next we will change the Beverage and CondimentDecorator in C #.
Write the getDescription method of Beverage as a virtual method.
public virtual string getDescription() { return description; }
Then override is added to the CondimentDecorator.
public abstract override string getDescription();
The running result is as follows:
This is normal, isn't it amazing?
To analyze the Java execution steps
Beverage beverage1 = new Latte (); // The parent class instantiates Latte beverage1 = new Mocha (beverage1); // Latte is used as the parameter to instantiate Mocha // beverage1getDescription. The getDescription method in Mocha is called first, then call the getDescription method in Latte (which implicitly inherits the parent class) beverage1.getDescription ();
I have never understood how to execute this in C #. Please leave a message for me if you know it. Thank you very much.