0% found this document useful (0 votes)
4 views5 pages

Design Pattern Visitor

The document discusses the Visitor design pattern, which allows for adding operations to a class hierarchy without altering its structure, making code more flexible and maintainable. It provides a practical example using Delphi, illustrating how to apply discounts to different items in a shopping cart through interfaces and classes. The Visitor pattern enables the addition of new operations without modifying existing item classes, promoting cleaner code architecture.

Uploaded by

marcelojaloto
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Design Pattern Visitor

The document discusses the Visitor design pattern, which allows for adding operations to a class hierarchy without altering its structure, making code more flexible and maintainable. It provides a practical example using Delphi, illustrating how to apply discounts to different items in a shopping cart through interfaces and classes. The Visitor pattern enables the addition of new operations without modifying existing item classes, promoting cleaner code architecture.

Uploaded by

marcelojaloto
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
910712025, 12:48 (20) Activity | MaresloJaloto | Linkedin Marcelo Jaloto fl « you te Senior Software Architect @ Jaloto Software| Senior Software Engineer | Delphi Specialist yr ® Design Pattern: Visitor Your Ticket to Cleaner and More Adaptable Code Looking to add operations to a class hierarchy without altering its structure? The Visitor design pattern is here to help! It allows you to separate algorithms from the objects they operate on, making your code more flexible and easier to maintain. Let’s dive into a simple example in Delphi to see how it works Example: A Shopping Cart with Various Items Imagine we have a shopping cart with different types of items. We want to apply a discount operation to each item, but the calculation varies by item type. Here’s how we can implement the Visitor pattern to achieve this type (Cartitem = interface: (CartVisitor = interface [(08177501-7AE0-4845-8450-470FDO4ADF95)'] procedure VisitBook(const Item: ICartltem); procedure VisitElectronics(const Item: ICartltem), end, htips:[Link]-activtyal! 48 910712025, 12:48 (20) Activity | MaresloJaloto | Linkedin (Cartltem interface ((07228566-A045-4DD1-9B5A-928784A95893)) procedure Accept(Visitor: ICartVisitor) end; TBook = class(TinterfacedObject, ICartitem) private Price: Double; public procedure Accept(Visitor: ICartVisitor) property Price: Double read FPrice write FPrice; end; Télectronics = class(TinterfacedObject, ICartltem) private Price: Double; public procedure Accept(Visitor: ICartVisitor); property Price: Double read FPrice write FPrice; end, TDiscountVisitor = class(TinterfacedObject, ICartVisitor) public procedure VisitBook(const Item: ICartltem); procedure VisitElectronics(const Item: ICartltem), end, implementation procedure TBook Accept(Visitor: ICartVisitor); htips:[Link]-activtyal! revor20as, 1248 (20) tty | Marlo Jt | Linkecn begin Visitor. VisitBook(Self); end, procedure [Link](Vi r: ICartVisitor); begin Visitor. VisitElectronics(Self); end; procedure TDiscountVisitor VisitBook{(const Item: ICartitem); begin WriteLn(‘Applying 10% discount to book:’, end; procedure [Link](const Item: ICartltem); begin WriteLn(Applying 5% discount to electronics.) end; How you can use the example begin var Book := TBook Create Book Price := 0; var Electronics := TElectrot Electronics Price := 200; var Visitor := [Link], htips:[Link]-activtyal! 35 910712025, 12:48 (20) Activity | MaresloJaloto | Linkedin WriteLn(‘Calculating discounts for items in cart...) Book Accept Visitor); Electronics. Accept(Visitor); end. In this example, ICartltem and its implementations (TBook and TElectronics) represent the items in the shopping cart. The ICartVisitor interface and its implementation (TDiscountVisitor) define operations to be performed on these items By using the Visitor pattern, we can easily add new operations (like additional discounts or tax calculations} without modifying the item classes themselves Embrace the Visitor pattern to keep your code clean, flexible, and maintainable! #SoftwareEngineering #DesignPatterns #VisitorPattern #Delphi #CodeQuality #DelphiDeveloper htips:[Link]-activtyal! 8

You might also like