Software design - Lecture 29. The main topics covered in this chapter include: defination of factory method; factory method pattern; factory design pattern may make your program complex; be sure that it is required as context is critical;...
1 Software Design Lecture : 29 Defination of Factory Method 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 Consequences Factory Design pattern may make your program complex Be sure that it is required as context is critical Problem Statement 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 Proposed Class Diagram 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 ie 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 rework 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 ie 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 ie lahore or karachi 16 Overall Class Diagram 17 18 Code Implementation in Java