design patterns factory pattern - hacettepe...
TRANSCRIPT
![Page 1: Design Patterns Factory Pattern - Hacettepe Üniversitesiyunus.hacettepe.edu.tr/~ebru/an/BBM486-Slide-4.pdf · The Factory Method Pattern •This is a ‘Creational’ pattern, i.e](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fcb406a52aa550709485812/html5/thumbnails/1.jpg)
Design Patterns
Factory PatternWho should create?
http://yunus.hacettepe.edu.tr/~ebru/
@ebru176
Ekim 2017
*revised from, www.uwosh.edu/faculty_staff/huen/262/f09/slides/10_Strategy_Pattern.ppt
![Page 2: Design Patterns Factory Pattern - Hacettepe Üniversitesiyunus.hacettepe.edu.tr/~ebru/an/BBM486-Slide-4.pdf · The Factory Method Pattern •This is a ‘Creational’ pattern, i.e](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fcb406a52aa550709485812/html5/thumbnails/2.jpg)
Last point we remained
2
Duck should not know:-Its subclasess-Its changing behaviour types
![Page 3: Design Patterns Factory Pattern - Hacettepe Üniversitesiyunus.hacettepe.edu.tr/~ebru/an/BBM486-Slide-4.pdf · The Factory Method Pattern •This is a ‘Creational’ pattern, i.e](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fcb406a52aa550709485812/html5/thumbnails/3.jpg)
Who can create, who should create?
3
A<interface>
doA()
A1
doA()
A2
doA()
B<interface>
doB()
B1
doB()
B2
doB()
MyObjectA refA;B ref B;
doAction()
public void doAction(int i) {if (i<0) {
refA.doA();refB.doB();
} else{
refB.doB();refA.doA();
}}
Client1
doSomething()
MyObject m;
Client2
doSomething()
MyObject m;
public void doSomething(){
if !(m) {m = new MyObject();if (condition4A) m.setRefA(new A1());
else m.setRefA(new A2());if (condition4B) m.setRefB(new B1());
else m.setRefA(new B2());}m.doAction();
}
public MyObject(){
MYObject m;m = new MyObject();if (condition4A) m.setRefA(new A1());
else m.setRefA(new A2());if (condition4B) m.setRefB(new B1());
else m.setRefA(new B2());
m.doSomething();}
![Page 4: Design Patterns Factory Pattern - Hacettepe Üniversitesiyunus.hacettepe.edu.tr/~ebru/an/BBM486-Slide-4.pdf · The Factory Method Pattern •This is a ‘Creational’ pattern, i.e](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fcb406a52aa550709485812/html5/thumbnails/4.jpg)
Define an interface for creating an object, but let subclasses decide which class to instantiate
Factory Method Pattern
revision on march 2017, @ebru
![Page 5: Design Patterns Factory Pattern - Hacettepe Üniversitesiyunus.hacettepe.edu.tr/~ebru/an/BBM486-Slide-4.pdf · The Factory Method Pattern •This is a ‘Creational’ pattern, i.e](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fcb406a52aa550709485812/html5/thumbnails/5.jpg)
Factory Method: Applicability
• Use the Factory Method pattern when• to make client class as unable to anticipate the class of
objects it must create/have
• a class wants its subclasses to specify the objects it creates
![Page 6: Design Patterns Factory Pattern - Hacettepe Üniversitesiyunus.hacettepe.edu.tr/~ebru/an/BBM486-Slide-4.pdf · The Factory Method Pattern •This is a ‘Creational’ pattern, i.e](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fcb406a52aa550709485812/html5/thumbnails/6.jpg)
The Factory Method Pattern
• This is a ‘Creational’ pattern, i.e. it is concerned with object instantiation
• Used where an abstract class (A) may, itself, need to create objects of other classes
• where the precise class is not necessarily known.
• The precise class to be instantiated may only be known within a sub-class of A.
• However, all sub-classes of A will share the common signature of the super-class. Therefore, the abstract class (A) may interact with the created object through this interface.
![Page 7: Design Patterns Factory Pattern - Hacettepe Üniversitesiyunus.hacettepe.edu.tr/~ebru/an/BBM486-Slide-4.pdf · The Factory Method Pattern •This is a ‘Creational’ pattern, i.e](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fcb406a52aa550709485812/html5/thumbnails/7.jpg)
Factory Method pattern
• Define an interface for creating an object, but let subclasses decide which class to instantiate. It lets a class defer instantiation to subclasses
• PROBLEM:
• A framework with abstract application classes and application-specific subclasses to instantiate to realize different implementations
• SOLUTION:
• The Factory Method pattern encapsulates the knowledge of which subclass to create and moves this knowledge out of the framework
![Page 8: Design Patterns Factory Pattern - Hacettepe Üniversitesiyunus.hacettepe.edu.tr/~ebru/an/BBM486-Slide-4.pdf · The Factory Method Pattern •This is a ‘Creational’ pattern, i.e](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fcb406a52aa550709485812/html5/thumbnails/8.jpg)
Example: Simple Pizza Class
![Page 9: Design Patterns Factory Pattern - Hacettepe Üniversitesiyunus.hacettepe.edu.tr/~ebru/an/BBM486-Slide-4.pdf · The Factory Method Pattern •This is a ‘Creational’ pattern, i.e](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fcb406a52aa550709485812/html5/thumbnails/9.jpg)
Creation and use of Pizza Class
CREATION
USE
![Page 10: Design Patterns Factory Pattern - Hacettepe Üniversitesiyunus.hacettepe.edu.tr/~ebru/an/BBM486-Slide-4.pdf · The Factory Method Pattern •This is a ‘Creational’ pattern, i.e](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fcb406a52aa550709485812/html5/thumbnails/10.jpg)
Pizza starts subclassing
![Page 11: Design Patterns Factory Pattern - Hacettepe Üniversitesiyunus.hacettepe.edu.tr/~ebru/an/BBM486-Slide-4.pdf · The Factory Method Pattern •This is a ‘Creational’ pattern, i.e](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fcb406a52aa550709485812/html5/thumbnails/11.jpg)
CREATION KNOWLEDGE
![Page 12: Design Patterns Factory Pattern - Hacettepe Üniversitesiyunus.hacettepe.edu.tr/~ebru/an/BBM486-Slide-4.pdf · The Factory Method Pattern •This is a ‘Creational’ pattern, i.e](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fcb406a52aa550709485812/html5/thumbnails/12.jpg)
CHANGE IN CREATION
![Page 13: Design Patterns Factory Pattern - Hacettepe Üniversitesiyunus.hacettepe.edu.tr/~ebru/an/BBM486-Slide-4.pdf · The Factory Method Pattern •This is a ‘Creational’ pattern, i.e](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fcb406a52aa550709485812/html5/thumbnails/13.jpg)
![Page 14: Design Patterns Factory Pattern - Hacettepe Üniversitesiyunus.hacettepe.edu.tr/~ebru/an/BBM486-Slide-4.pdf · The Factory Method Pattern •This is a ‘Creational’ pattern, i.e](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fcb406a52aa550709485812/html5/thumbnails/14.jpg)
![Page 15: Design Patterns Factory Pattern - Hacettepe Üniversitesiyunus.hacettepe.edu.tr/~ebru/an/BBM486-Slide-4.pdf · The Factory Method Pattern •This is a ‘Creational’ pattern, i.e](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fcb406a52aa550709485812/html5/thumbnails/15.jpg)
![Page 16: Design Patterns Factory Pattern - Hacettepe Üniversitesiyunus.hacettepe.edu.tr/~ebru/an/BBM486-Slide-4.pdf · The Factory Method Pattern •This is a ‘Creational’ pattern, i.e](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fcb406a52aa550709485812/html5/thumbnails/16.jpg)
![Page 17: Design Patterns Factory Pattern - Hacettepe Üniversitesiyunus.hacettepe.edu.tr/~ebru/an/BBM486-Slide-4.pdf · The Factory Method Pattern •This is a ‘Creational’ pattern, i.e](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fcb406a52aa550709485812/html5/thumbnails/17.jpg)
![Page 18: Design Patterns Factory Pattern - Hacettepe Üniversitesiyunus.hacettepe.edu.tr/~ebru/an/BBM486-Slide-4.pdf · The Factory Method Pattern •This is a ‘Creational’ pattern, i.e](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fcb406a52aa550709485812/html5/thumbnails/18.jpg)
Factory Method: class diagram
• the subclasses redefine abstract methods of the abstract class to return the appropriate subclass
![Page 19: Design Patterns Factory Pattern - Hacettepe Üniversitesiyunus.hacettepe.edu.tr/~ebru/an/BBM486-Slide-4.pdf · The Factory Method Pattern •This is a ‘Creational’ pattern, i.e](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fcb406a52aa550709485812/html5/thumbnails/19.jpg)
Client ConcreteCreater1 ConcreteProduct1
Request
createProduct()
newconcreteProduct1
Factory Method Sequence Diagram
![Page 20: Design Patterns Factory Pattern - Hacettepe Üniversitesiyunus.hacettepe.edu.tr/~ebru/an/BBM486-Slide-4.pdf · The Factory Method Pattern •This is a ‘Creational’ pattern, i.e](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fcb406a52aa550709485812/html5/thumbnails/20.jpg)
Factory Method: class diagram sample
we call createDocument() the factory method because it is
responsible for “manufacturing” an object
![Page 21: Design Patterns Factory Pattern - Hacettepe Üniversitesiyunus.hacettepe.edu.tr/~ebru/an/BBM486-Slide-4.pdf · The Factory Method Pattern •This is a ‘Creational’ pattern, i.e](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fcb406a52aa550709485812/html5/thumbnails/21.jpg)
Factory Method pattern
• applicabilities:
• the class that must instantiate classes only knows about abstract classes, which it cannot instantiate. It only knows when or how to create an object but not what kind oh object to create, because this is application-specific
• a class want its subclasses to specify the objects to be created
• classes delegate responsibility to one or several helper subclasses and you want to localize the knowledge of which helper subclass is the delegate
![Page 22: Design Patterns Factory Pattern - Hacettepe Üniversitesiyunus.hacettepe.edu.tr/~ebru/an/BBM486-Slide-4.pdf · The Factory Method Pattern •This is a ‘Creational’ pattern, i.e](https://reader036.vdocuments.net/reader036/viewer/2022062415/5fcb406a52aa550709485812/html5/thumbnails/22.jpg)
Factory Method pattern
• CONSEQUENCES: • Factory methods eliminate the need to bind application-
specific classes into your code
• The code only deals with the Product interface and then it can work with any user-defined ConcreteProduct class
• Clients might have to subclass the Creator class to create a particular ConcreteProduct object