Tải bản đầy đủ (.pdf) (18 trang)

Software design: Lecture 29 - Sheraz Pervaiz

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (324.57 KB, 18 trang )

1

Software Design
Lecture : 29


2

Defination of Factory Method 


3

Factory Method Pattern 
Factory  Method  Pattern  defines  an  interface  for 
creating  the  objects  but  let  subclass  decides 
which  object  to  instantiate.  Factory  Method 
Pattern let a class instantiation to the subclass.


4


5

Consequences
Factory  Design  pattern  may  make  your  program 
complex.

Be sure that it is required as context is critical



6

Problem Statement


7

 There is a pizzashop which is offering different delicious 
pizza’s  to  it’s  customer.  There  are  some  standard 
processes which are involved in a pizza creation  like first 
the  pizza  is  prepared  by  putting  together  all  the 
ingredients, then it is prepared for baking , after baking it 
is cut and put into the boxes of as per order placed by the 
customer.  There  are  different  type  of  pizzas  like  cheese, 
chicken,  vegetable  etc;  and  this  list  keeps  on  increasing 
with addition of in demand pizzas and removal of not in 
demand pizzas


8

Proposed Class Diagram


9


10


Franchising the Store
 Now  the  pizza  store  want  to  offer  the  franchises  in 
different cities but want to make sure that the processes 
are  uniform  across  all  the  franchises  so  to  maintain  the 
standard. 

 Only  thing  which  might  differ  is  the  regional  style  of 
pizzas  i­e  may  be  Chicken  is  liked  more  Lahore  as 
compare to Karachi where thick crust is likedetc


11

Solution
 We  need  a  framework  to  localize  all  the  pizza  making 
activities of the pizza store and yet let the franchises add 
their own local styles.

 We  need  to  re­work  our  classes  to  modify  as  per  new 
franchise requirements.


12

Reworking the PizzaStore Class
We  place  createpizza  method  in  abstract 
pizzastore  class  instead  of  Factory  and  define  it 
abstract because it varies across region.

 We define subclass of pizzastore for each region



13

Modified Class Diagram


14


15

Working of Orderpizza Method()
 This method is present in PizzaStore only not in the subclasses. 

 Since Pizza is abstract so this method does not know which subclass 
is called i­e it is decoupled

 When order pizza calls create pizza then one of the subclass is called 
depending  upon  the  franchise  from  where  the  call  is  i­e  lahore  or 
karachi


16

Overall Class Diagram


17



18

Code Implementation in Java



×