Big talk design pattern-decorator mode problems with C # vs. Java

Source: Internet
Author: User

Recently saw the decorator pattern in the design pattern book, and then wrote it in C #, and found that the results were different from the book, and then wrote it again in Java with the same code, different compilers and the runtime, Java and. NET.
the first is the Java implementation
Beverage (Beverage abstract Class)
Condimentdecorator (Spices abstract class, inheriting beverage)
Latte (latte drink, inherit beverage)
Mocha (Mocha seasoning, inheriting Condimentdecorator)

publicabstractclass Beverage {    public"Unkonw";    publicgetDescription(){        return description;     }    publicabstractdoublecost();}
public   Abstract  class   Condimentdecorator  extends  beverage  {  public  abstract  String getdescription  ();}  
publicclass Latte extends Beverage {    publicLatte(){        "Latte Coffee";    }    @Override    publicdoublecost() {        // TODO Auto-generated method stub        return21.05;    }}
 Public  class Mocha extends condimentdecorator {Beverage beverage; Public Mocha(Beverage beverage) { This. beverage = Beverage; }@Override     PublicStringgetdescription() {//TODO auto-generated method stub        returnBeverage.getdescription () +", Mocha condiment"; }@Override     Public Double  Cost() {//TODO auto-generated method stub        return .+ Beverage.cost (); }}
publicstaticvoidmain(String[] args) {        // TODO Auto-generated method stub        new Latte();        new Mocha(beverage1);        System.out.println("小明点了:"+beverage1.getDescription()+"咖啡,花了:"+beverage1.cost()+" RMB");    }

Run results

Here's a look at the implementation in C #

Beverage (Beverage abstract Class)
Condimentdecorator (Spices abstract class, inheriting beverage)
Espresso (espresso, inherit beverage)
Soymilk (soy sauce, inherited condimentdecorator)

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  (); }
publicclass Espresso :Beverage    {        publicEspresso()         {            "Espresso Coffee(浓咖啡)";        }        publicoverridedoublecost()        {            return19.00;        }    }
 Public classSoymilk:condimentdecorator {PrivateBeverage beverage; Public Soymilk(Beverage Bev) { This. beverage = Bev; } Public Override string getdescription()        {returnBeverage.getdescription () +"soymilk (soy sauce)"; } Public Override Double  Cost()        {return 1.25+ Beverage.cost (); }    }
publicstaticvoidMain(string[] args)        {             new Espresso();             new SoyMilk(espresso);            Console.WriteLine("小张点了一杯:"" 咖啡, 价格是:"" RMB");            Console.ReadLine();        }

As you can see, the code is the same except for the name of the drink, and here are the results of the run

Compared to the above Java results are really different, below the C # Beverage and Condimentdecorator change
The GetDescription method of beverage is written as a virtual method

publicvirtualstringgetDescription()         {            return description;        }

and add the Condimentdecorator to the override.

publicabstractoverridestringgetDescription();

Here is the result of the run

It's normal, isn't it amazing?

To analyze the execution steps in Java

new//父类实例化Latte        new//Latte作为参数实例化Mocha        //beverage1getDescription首先调用Mocha中的getDescription方法,然后再调用Latte里面的getDescription方法(隐式继承了父类)beverage1.getDescription();

Always do not understand how in the C # in the end is how to carry out, have learned please leave me a message, thank you very much

Big talk design pattern-decorator mode problems with C # vs. Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.