91-9990449935 0120-4256464 |
Decorator PatternA Decorator Pattern says that just "attach a flexible additional responsibilities to an object dynamically". In other words, The Decorator Pattern uses composition instead of inheritance to extend the functionality of an object at runtime. The Decorator Pattern is also known as Wrapper. Advantage of Decorator Pattern
Usage of Decorator PatternIt is used:
UML for Decorator Pattern:Step 1:Create a Food interface. Step 2: Create a VegFood class that will implements the Food interface and override its all methods. File: VegFood.java
Step 3:Create a FoodDecorator abstract class that will implements the Food interface and override it's all methods and it has the ability to decorate some more foods. File: FoodDecorator.java
Step 4:Create a NonVegFood concrete class that will extend the FoodDecorator class and override it's all methods. File: NonVegFood.java
Step 5:Create a ChineeseFood concrete class that will extend the FoodDecorator class and override it's all methods. File: ChineeseFood.java
Step 6:Create a DecoratorPatternCustomer class that will use Food interface to use which type of food customer wants means (Decorates). File: DecoratorPatternCustomer.java
Output
Next TopicFacade Pattern
|