0
2
3
 Name: Factory Method
 Intent: Define an interface for creating an object, but
let subclasses decide which class to in...
4
Product is the interface for the type of object that the Factory Method
creates. Creator is the interface that defines t...
5
 You want to connect to a database but you want to
decide at run time which type of database it is (i.e.
SQL, Oracle, M...
6
7
public interface ConnectionFactory{
public Connection createConnection();
}
class MyConnectionFactory implements Connect...
8
public interface Connection{
public String description();
public void open();
public void close();
}
class SQLConnection...
9
class TestConnectionFactory{
public static void main(String[]args){
MyConnectionFactory con = new
MyConnectionFactory("M...
10
11
 Name: Strategy
 Intent: Define a family of algorithms, encapsulate each
one, and make them interchangeable. Strategy...
12
13
 Strategy : declares an interface common to all
supported algorithms. Context uses this interface to
call the algorith...
14
 We are developing an e-commerce application where
we need to calculate Tax…there are two tax
strategies…Tax rate is 1...
15
16
public interface TaxStrategy{
public void calcTax();
}
class Citizen implements TaxStrategy{
public void calcTax(){
Sys...
17
class TaxContext{
private TaxStrategy TS;
public void setStrategy(TaxStrategy ts){ TS=ts; }
public void calculateTax(){...
18
 There are different Taxing algorithms or strategies, and
they change over time. Who should create the
strategy?
 A s...
19
20
21
public interface StrategyFactory{
public TaxStrategy getStrategy();
}
class MyStrategy implements StrategyFactory{
Stri...
22
class Client{
public static void main(String[]args){
MyStrategy m = new MyStrategy("Citizen");
TaxContext cont = new Ta...
23
24
 Name: Proxy
 Intent: Provide a surrogate or placeholder for another
object to control access to it.
 Problem: You w...
25
26
 Subject - Interface implemented by the RealSubject
and representing its services. The interface must be
implemented b...
27
 Consider an image viewer program that lists and
displays high resolution photos. The program has to
show a list of al...
28
29
public interface Image{
public void showImage();
}
class HRImage implements Image{
public HRImage(){
System.out.println...
30
class ProxyImage implements Image{
private Image proxyImage;
public ProxyImage(){
System.out.println("Loading a proxy i...
31
 Virtual Proxies: delaying the creation and
initialization of expensive objects until needed, where
the objects are cr...
32
Factory method & strategy pattern
Upcoming SlideShare
Loading in...5
×

Factory method & strategy pattern

346

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
346
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Factory method & strategy pattern"

  1. 1. 2
  2. 2. 3  Name: Factory Method  Intent: Define an interface for creating an object, but let subclasses decide which class to instantiate. Defer instantiation to subclasses.  Problem: A class needs to instantiate a derivation of another class, but doesn't know which one. Factory Method allows a derived class to make this decision.  Solution: Provides an abstraction or an interface and lets subclass or implementing classes decide which class or method should be instantiated or called, based on the conditions or parameters given.
  3. 3. 4 Product is the interface for the type of object that the Factory Method creates. Creator is the interface that defines the Factory Method.
  4. 4. 5  You want to connect to a database but you want to decide at run time which type of database it is (i.e. SQL, Oracle, MySQL etc.)  Apply Factory Method
  5. 5. 6
  6. 6. 7 public interface ConnectionFactory{ public Connection createConnection(); } class MyConnectionFactory implements ConnectionFactory{ public String type; public MyConnectionFactory(String t){ type = t; } public Connection createConnection(){ if(type.equals("Oracle")){ return new OracleConnection(); } else if(type.equals("SQL")){ return new SQLConnection(); } else{ return new MySQLConnection(); } } }
  7. 7. 8 public interface Connection{ public String description(); public void open(); public void close(); } class SQLConnection implements Connection{ public String description(){ return "SQL";} } class MySQLConnection implements Connection{ public String description(){ return "MySQL” ;} } class OracleConnection implements Connection{ public String description(){ return "Oracle"; } } These three implementing classes should have definition for Open & Close Methods of above interface as well to be concrete.
  8. 8. 9 class TestConnectionFactory{ public static void main(String[]args){ MyConnectionFactory con = new MyConnectionFactory("My SQL"); Connection con2 = con.createConnection(); System.out.println("Connection Created: "); System.out.println(con2.description()); } }
  9. 9. 10
  10. 10. 11  Name: Strategy  Intent: Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.  Problem: How to design for varying, but related, algorithms or policies?  Solution: Define each algorithm/policy/strategy in a separate class, with a common interface.
  11. 11. 12
  12. 12. 13  Strategy : declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.  ConcreteStrategy: implements the algorithm using the Strategy interface.  Context : is configured with a ConcreteStrategy object.  maintains a reference to a Strategy object.  may define an interface that lets Strategy access its data.
  13. 13. 14  We are developing an e-commerce application where we need to calculate Tax…there are two tax strategies…Tax rate is 10% for old citizens and 15% otherwise.
  14. 14. 15
  15. 15. 16 public interface TaxStrategy{ public void calcTax(); } class Citizen implements TaxStrategy{ public void calcTax(){ System.out.println("Tax Deduction @ 15%");}} class OldCitizen implements TaxStrategy{ public void calcTax(){ System.out.println("Tax Deduction @ 10%");}}
  16. 16. 17 class TaxContext{ private TaxStrategy TS; public void setStrategy(TaxStrategy ts){ TS=ts; } public void calculateTax(){ TS.calcTax(); }} class Client{ public static void main(String[]args){ TaxContext cont = new TaxContext(); cont.setStrategy(new Citizen()); cont.calculateTax(); } }
  17. 17. 18  There are different Taxing algorithms or strategies, and they change over time. Who should create the strategy?  A straightforward approach is to apply the Factory pattern
  18. 18. 19
  19. 19. 20
  20. 20. 21 public interface StrategyFactory{ public TaxStrategy getStrategy(); } class MyStrategy implements StrategyFactory{ String type; public MyStrategy(String t){ type=t; } public TaxStrategy getStrategy(){ if(type.equals("Citizen")) return new Citizen(); else return new OldCitizen();} }
  21. 21. 22 class Client{ public static void main(String[]args){ MyStrategy m = new MyStrategy("Citizen"); TaxContext cont = new TaxContext(); cont.setStrategy(m.getStrategy()); cont.calculateTax(); } }
  22. 22. 23
  23. 23. 24  Name: Proxy  Intent: Provide a surrogate or placeholder for another object to control access to it.  Problem: You want to control the access to an object for different reasons. You may want to delay the creation / initialization of expensive objects or you may want to provide a local representation of a remote object.  Solution: Provide a Stub / placeholder for actual object.
  24. 24. 25
  25. 25. 26  Subject - Interface implemented by the RealSubject and representing its services. The interface must be implemented by the proxy as well so that the proxy can be used in any location where the RealSubject can be used.  Proxy- Maintains a reference that allows the Proxy to access the RealSubject. Implements the same interface implemented by the RealSubject so that the Proxy can be substituted for the RealSubject. Controls access to the RealSubject and may be responsible for its creation and deletion.  RealSubject- the real object that the proxy represents.
  26. 26. 27  Consider an image viewer program that lists and displays high resolution photos. The program has to show a list of all photos however it does not need to display the actual photo until the user selects an image item from a list.
  27. 27. 28
  28. 28. 29 public interface Image{ public void showImage(); } class HRImage implements Image{ public HRImage(){ System.out.println("loading a High Resolution image"); } public void showImage(){ System.out.println("Showing a High Resolution Image"); } }
  29. 29. 30 class ProxyImage implements Image{ private Image proxyImage; public ProxyImage(){ System.out.println("Loading a proxy image");} public void showImage(){ proxyImage = (HRImage)new HRImage(); proxyImage.showImage();} } class ImageViewer{ public static void main(String[]args){ Image HRImage1 = new ProxyImage(); Image HRImage2 = new ProxyImage(); Image HRImage3 = new ProxyImage(); HRImage1.showImage();} }
  30. 30. 31  Virtual Proxies: delaying the creation and initialization of expensive objects until needed, where the objects are created on demand  Remote Proxies: providing a local representation for an object that is in a different address space. A common example is Java RMI stub objects. The stub object acts as a proxy where invoking methods on the stub would cause the stub to communicate and invoke methods on a remote object (called skeleton) found on a different machine.  Protection Proxies: where a proxy controls access to RealSubject methods, by giving access to some objects while denying access to others.
  31. 31. 32
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×