Design mode – decorator mode

  1. Define
    The decorator pattern dynamically attaches responsibility to the object. To extend functionality, decorators provide a more flexible alternative than inheritance.
  2. Implementation points
    The decorator and the decorated class need to inherit from the same interface to achieve type matching. The decorator holds an instance of the class being decorated.
  3. Code example

    /** * Basic components*/abstract class Beverage {private String description = "Unknown Beverage"; public String getDescription() {return description;} public void setDescription(String description) {this.description = description;} public abstract double cost();}/** * Decorator base class*/abstract class Decorator extends Beverage {@Override public abstract String getDescription();}/** * The decorated class*/class Espresso extends Beverage {public Espresso() {setDescription("Espresso");} @Override public double cost() {return 1.99; }}/* * * Decorator*/class Mocha extends Decorator {private Beverage beverage; public Mocha(Beverage beverage) {this.beverage = beverage;} @Override public double cost() {return beverage.cost() + 0.29;} @Override public String getDescription() {return beverage.getDescription() + ", Mocha"; }}class CoffeeTest {public static v oid main(String[] args) {Beverage beverage = new Espresso(); beverage = new Mocha(beverage); System.out.println(beverage.getDescription() + "" + beverage.cost()); }}< /pre>

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2839 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.