Visitor Design Pattern Application Example 7/1/2016 CS 631: Behavioral Design Patterns: Visitor 1 Outline • Application – Types of Traders • Hedgers • Speculators • Arbitrageurs – Trade Simulator • Class hierarchy • Adding a class • Adding a method • Visitor Pattern – Design – Analysis 7/1/2016 CS 631: Behavioral Design Patterns: Visitor 2 Trading • Derivatives markets have been successful as they attracted many types of traders and created a great deal of liquidity. – There are usually always investors willing to take two sides of a contract. • There three broad categories of traders: – Hedgers use futures, forwards, and options to reduce the risk from future movements in the market. – Speculators use them to bet on the future direction of the market. – Arbitrageurs take offsetting positions in two or more instruments to lock in a profit. 7/1/2016 CS 631: Behavioral Design Patterns: Visitor 3 Hedgers • Hedging on options: – An investor owns 1,000 Microsoft shares in May, 2000. • He is concerned the antitrust case may cause the share price to decline in two months. – He buys a put option contract for with a strike price $65 and strike date in July 2000. • It cost $2.50 per share = $2,500 for 1000 shares. – The market price < $65 • Options are exercised for $65,000 – The amount realized is $65000-$2500=$62,500 – The market price > $65 • Options are not exercised – The value of the holding is greater than $65,000-$2,500 = $62,500 – The hedger ensures that the value of his portfolio > $62,500 by July, 2000 7/1/2016 CS 631: Behavioral Design Patterns: Visitor 4 Speculators • Speculating on options – In October the Cisco's share price is $20. • A speculator considers the stock price likely to increase in two months. • He is willing to spend $4,000 – He buys 4,000 two-month call options with a $25 strike price at $1. – Say the hunch is correct and stock price is $35 in two months. • The profit is 4000 ($35-$25) - $4,000 = $36,000 – The profit would be much smaller if buying the stock directly: • 200 ($35-$20) = $3000 – Assume the market price is $15 • The loss is $4000 in case of options • The loss is 200 ($20-$15) = $1,000 in case of buying the stock 7/1/2016 CS 631: Behavioral Design Patterns: Visitor 5 Arbitrageurs • Locking in a riskless profit by entering transactions in two markets: – The same stock is trading on the NYSE at $152 and on the London Stock Exchange at £100. • The exchange rate is $1.550 per pound. – An arbitrageur buys 100 shares in NY and sells them in London • The profit is 100 [($1.55 100) - $152] = $300 – (transaction costs) – Transaction costs would eliminate the profit for a small investor, but would work for a large investor. – Arbitrage opportunities cannot last for long. • The forces of supply and demand will cause the prices to even out. 7/1/2016 CS 631: Behavioral Design Patterns: Visitor 6 Trade Simulator • Consider an application that simulates the market by executing various trades. • Such application depends on a pool of traders. – We assume the traders are classified broadly into the types discussed. – Each trader is characterized by name, address, current capital, etc. – Each trader needs to be able to evaluate and possibly execute different types of trades. • The application needs a well defined class hierarchy of traders. 7/1/2016 CS 631: Behavioral Design Patterns: Visitor 7 Trader Class Hierarchy 7/1/2016 CS 631: Behavioral Design Patterns: Visitor 8 Trader Hierarchy: Analysis • Pros: – Flexible in allowing to add another type of trader; – All necessary information about a trade is encapsulated in a separate class (Deal). • A Deal class can have its own hierarchy • Cons – All Trader classes need to have access to the Deal definition. • When the Deal interface changes all Trader classes need to be recompiled. – The actual operations of evaluating and executing a deal are spread throughout subclasses implementation. • Multiple files need to be updated when logic changes. – Adding a new operation, to say compare two deals will cause adding it to the whole hierarchy • A solution is to move all operations to the Deal class. – This class would be responsible for what to do for each concrete type. 7/1/2016 CS 631: Behavioral Design Patterns: Visitor 9 Trader Hierarchy with TraderVisitor 7/1/2016 CS 631: Behavioral Design Patterns: Visitor 10 TraderVisitor: Analysis • TraderVisitor class implements virtual functions for each Trader subclass. – It can reuse any algorithm within the same class hierarchy. • The Trader hierarchy implements a unique bouncing virtual function. – Calls accept() to be invoked on a different subclass. • The TraderVisitor hierarchy represents different types of operations to be performed by Traders. – It contains a Deal as a member data. – One class is responsible for evaluating a deal, the other one executes it. – The result is reported by getStatus() 7/1/2016 CS 631: Behavioral Design Patterns: Visitor 11 Visitor Pattern: Analysis • Intent – Represents an operation to be performed on the elements of an object structure. It lets define a new operation without changing the classes of the elements on which it operates. • Applicability – an object structure contains many classes with differing interfaces, and operations performed depend on concrete classes. – many distinct and unrelated operations are performed on objects. • to avoid "polluting" the classes with operations. – the classes rarely change, but new operations are often added or modified. 7/1/2016 CS 631: Behavioral Design Patterns: Visitor 12 Visitor Pattern: Participants • Visitor (TradeVisitor) – Declares a visit operation on each class of the hierarchy. The operation's name and signature is linked to the concrete subclass. • ConcreteVisitor (EvaluatingVisitor) – implements each operation declared by visitor. Each operation implements a fragment or a particular algorithm. • Element (Trader) – defines an accept operation that takes a visitor as an argument. • ConcreteElement (Hedger, ...) – implements accept() • ObjectStructure (MarketSimulator) – provides an interface to allow the visitor to visit its elements. 7/1/2016 CS 631: Behavioral Design Patterns: Visitor 13