Suggested Videos
Part 17 - Adapter Design Pattern - Text - Slides
Part 18 - Bridge Design Pattern - Text - Slides
Part 19 - Composite Design Pattern - Text - Slides
In this video we will learn
Decorator Design Pattern : As per the GOF definition, decorator Pattern states that we need to “Attach additional responsibilities to an object dynamically. Decorators provide A flexible alternative to sub classing for extending functionality.”
This pattern Falls under the category of Structural Design Pattern and is also known as Wrapper.
(追記) (追記ここまで)
Implementation Guidelines : We need to use Decorator Design Pattern when
decorator design pattern implementation
Step 1 : Add Interface ICar
Step 2 : Create Concrete component Hyndai to implement ICar
Step 3 : Create Concrete component Suzuki to implement ICar
Step 4 : Create Decorator Class CarDecorator to decorate the Icar using CarDecorator
Step 5 : Create Concrete Decorator OfferPrice to decorate the Icar using CarDecorator
Step 6 : Use the car decorator in the main program to achieve the output
Step 7 : Run the application to see the expected output
Make : Sedan Price:1000000 DiscountPrice: 800000
Step 8 : Above mentioned classes are created under the below folder structure. Again it’s not mandatory to follow this folder structure as we are just doing this for representation purpose.
decorator design pattern implementation c#
Design Patterns tutorial for beginners
Part 17 - Adapter Design Pattern - Text - Slides
Part 18 - Bridge Design Pattern - Text - Slides
Part 19 - Composite Design Pattern - Text - Slides
In this video we will learn
- What is Decorator Design Pattern
- Implementation guidelines of decorator design pattern
- Simple Decorator Design Pattern example
Decorator Design Pattern : As per the GOF definition, decorator Pattern states that we need to “Attach additional responsibilities to an object dynamically. Decorators provide A flexible alternative to sub classing for extending functionality.”
This pattern Falls under the category of Structural Design Pattern and is also known as Wrapper.
(追記) (追記ここまで)
Implementation Guidelines : We need to use Decorator Design Pattern when
- We need to add responsibilities to individual objects dynamically and transparently,
- The extension by sub classing is impractical.
- Class definition may be hidden or otherwise unavailable for sub classing.
decorator design pattern implementation
- Client : Clients use the Component interface to interact with objects
- Component : Defines the interface for objects that can have responsibilities added to them dynamically.
- ConcreteComponent : Defines an object to which additional responsibilities can be attached.
- Decorator : Maintains a reference to a Component object and defines an interface that conforms to Component's interface.
- ConcreteDecorator : Adds responsibilities to the component.
Step 1 : Add Interface ICar
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
DecoratorDemo.Component
{
publicinterfaceICar
{
string Make { get; }
double
GetPrice();
}
}
Step 2 : Create Concrete component Hyndai to implement ICar
using
DecoratorDemo.Component;
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
DecoratorDemo.ConcreteComponent
{
publicsealedclassHyndai : ICar
{
publicstring Make
{
get { return"HatchBack"; }
}
publicdouble GetPrice()
{
return 800000;
}
}
}
Step 3 : Create Concrete component Suzuki to implement ICar
using
DecoratorDemo.Component;
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
DecoratorDemo.ConcreteComponent
{
publicsealedclassSuzuki : ICar
{
publicstring Make
{
get { return"Sedan"; }
}
publicdouble GetPrice()
{
return 1000000;
}
}
}
Step 4 : Create Decorator Class CarDecorator to decorate the Icar using CarDecorator
using
DecoratorDemo.Component;
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
DecoratorDemo.Decorator
{
publicabstractclassCarDecorator : ICar
{
private ICar car;
public
CarDecorator(ICar Car)
{
car = Car;
}
publicstring Make { get { return car.Make;
} }
publicdouble GetPrice()
{
return car.GetPrice();
}
publicabstractdouble
GetDiscountedPrice();
}
}
Step 5 : Create Concrete Decorator OfferPrice to decorate the Icar using CarDecorator
using
DecoratorDemo.Component;
using
DecoratorDemo.Decorator;
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
DecoratorDemo.ConcreteDecorator
{
publicclassOfferPrice :
CarDecorator
{
public
OfferPrice(ICar car) : base(car)
{
}
publicoverridedouble
GetDiscountedPrice()
{
return .8 * base.GetPrice();
}
}
}
Step 6 : Use the car decorator in the main program to achieve the output
using
DecoratorDemo.Component;
using
DecoratorDemo.ConcreteComponent;
using
DecoratorDemo.ConcreteDecorator;
using
DecoratorDemo.Decorator;
using System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
DecoratorDemo
{
classProgram
{
staticvoid Main(string[] args)
{
ICar car = new Suzuki();
CarDecorator decorator = new
OfferPrice(car);
Console.WriteLine(string.Format("Make
:{0} Price:{1} " +
"DiscountPrice
: {2}"
, decorator.Make, decorator.GetPrice().ToString(),
decorator.GetDiscountedPrice().ToString()));
Console.ReadLine();
}
}
}
Step 7 : Run the application to see the expected output
Make : Sedan Price:1000000 DiscountPrice: 800000
Step 8 : Above mentioned classes are created under the below folder structure. Again it’s not mandatory to follow this folder structure as we are just doing this for representation purpose.
decorator design pattern implementation c#
Design Patterns tutorial for beginners
No comments:
Post a Comment
It would be great if you can help share these free resources
[フレーム]