(5) My Opinion on the Design Pattern in Section 23-dectoratorpattern)

Source: Internet
Author: User

Today, I learned the charm of the decoration mode, and I/O Stream of Java in JDK should work like this. The decoration mode is common in real life. Taking the simplest example as an example, the house we decorated has different house types. This is the foundation. We need to renovate the house and add paint, or wallpaper, or chandelier, furniture, etc. These are auxiliary materials, that is, decorative materials. Add whatever you need. However, the architecture of the most basic house will not change. Here is a reference to the classical Book Design Model to define the decoration mode: dynamically attach the responsibility to the object. To expand the function, the decoration provides a more flexible alternative solution than inheritance. The modifier mode has the following features: 1. the decorator and the decorator have a common base class. you need a quote from the decorator. 3. the decorator needs to be instantiated in the decorator. The following is a structure that uses Starbucks as an example to implement the decoration mode. Paste code ........\

public abstract class Beverage {  String description="Unknown Beverage";  public static final int TALL=1;  public static final int GRANDE=2;  public static final int VENTI=3;  int size;  public int getSize() {return size;}public void setSize(int size) {this.size = size;}public String getDescription() {return description;}  public abstract double cost();}

This is the base class shared by the decorator and the decorator. It contains descriptions and prices for the object.

public abstract class CondimentDecorator extends Beverage{   public abstract String getDescription();}

This is a base class for decoration. The getdescription () method must be implemented.

The following are the common base classes of beverage inherited by the decorator, with different types of coffee in the table:

public class DarkRoast extends Beverage {   public DarkRoast(){   description="Dark Roast Coffee";   }@Overridepublic double cost() {// TODO Auto-generated method stubreturn 1.33;}}

public class Decaf extends Beverage {   public Decaf() {description="Decaf Coffee";}@Overridepublic double cost() {// TODO Auto-generated method stubreturn 0.98;}}

public class Espresso extends Beverage{public Espresso() {description="Espresso";}@Overridepublic double cost() {// TODO Auto-generated method stubreturn 1.99;}}

public class HouseBlend extends Beverage{   public HouseBlend(){   description="House Blend Coffee";   }@Overridepublic double cost() {// TODO Auto-generated method stubreturn 0.89;}}

Next we need to really write the modifier class:

The first soy modifier adds the judgment of the cup in the big cup and small cup, and adds the method later.

public class Soy extends CondimentDecorator {    Beverage beverage;    public Soy(Beverage beverage) {// TODO Auto-generated constructor stub    this.beverage=beverage;}@Overridepublic String getDescription() {// TODO Auto-generated method stubreturn beverage.getDescription()+",Soy";}public int getSize(){return beverage.getSize();}@Overridepublic double cost() {double cost=0.30+beverage.cost();if(getSize()==Beverage.TALL){cost+=0.10;}else if(getSize()==Beverage.GRANDE){cost+=0.15;}else if(getSize()==Beverage.VENTI){cost+=0.20;}else { return cost;}return cost;}}

public class Mocha extends CondimentDecorator {    Beverage beverage;    public Mocha(Beverage beverage) {// TODO Auto-generated constructor stub    this.beverage=beverage;}@Overridepublic String getDescription() {// TODO Auto-generated method stubreturn beverage.getDescription()+",Mocha";}@Overridepublic double cost() {// TODO Auto-generated method stubreturn 0.20+beverage.cost();}}

public class Whip extends CondimentDecorator {    Beverage beverage;    public Whip(Beverage beverage) {// TODO Auto-generated constructor stub    this.beverage=beverage;}@Overridepublic String getDescription() {// TODO Auto-generated method stubreturn beverage.getDescription()+",Whip";}@Overridepublic double cost() {// TODO Auto-generated method stubreturn 0.40+beverage.cost();}}

public class Whip extends CondimentDecorator {    Beverage beverage;    public Whip(Beverage beverage) {// TODO Auto-generated constructor stub    this.beverage=beverage;}@Overridepublic String getDescription() {// TODO Auto-generated method stubreturn beverage.getDescription()+",Whip";}@Overridepublic double cost() {// TODO Auto-generated method stubreturn 0.40+beverage.cost();}}

public class Milk extends CondimentDecorator {    Beverage beverage;    public Milk(Beverage beverage) {// TODO Auto-generated constructor stub    this.beverage=beverage;}@Overridepublic String getDescription() {// TODO Auto-generated method stubreturn beverage.getDescription()+",Milk";}@Overridepublic double cost() {// TODO Auto-generated method stubreturn 0.50+beverage.cost();}}

The following is the operation class of the Starbucks workshop, so that we can test our design model. During the test, I found that the addition of floating point numbers will cause an error, the solution is to write a class to accumulate bigdecemal for floating point numbers and use this class for writing. I don't want to write about it or let him accumulate it. we just look at the Paster's pattern.

public class StarBuzzCoffee {  public static void main(String[] args) {Beverage beverage=new Espresso();beverage.setSize(3);beverage=new Soy(beverage);System.out.println(beverage.getDescription()+"$"+beverage.cost());Beverage beverage2=new DarkRoast();beverage2=new Mocha(beverage2);beverage2=new Mocha(beverage2);    beverage2=new Whip(beverage2);System.out.println(beverage2.getDescription()+"$"+beverage2.cost());Beverage beverage3=new HouseBlend();beverage3.setSize(2);    beverage3=new Soy(beverage3);    beverage3.cost();beverage3=new Mocha(beverage3);beverage3=new Whip(beverage3);System.out.println(beverage3.getDescription()+"$"+beverage3.cost());}}

So far, the decorator mode has been achieved, and we suddenly think that the I/O Stream is like this. We constantly nest different I/O streams. Therefore, the decorator model is widely used.

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.