Creational Design Patterns:
Creational Design Patterns:
Factory Pattern:
new means instantiating a concreate class
By Coding to interface, the class will work with any new classes implementing that classes through Polymorphism.
But if using the new operator/concrete classes it would make class liable to modification thus, "closed for modification" fails.
hence we need to take that code out from class to different object and encapsulate the same since addeing new implementation clesses
through modification will need to change the code. The new Object is called factory that takes out the object creation code out from
class defined earlier.PreRequisite: Differentiate which part of Code is supposed to be eligile to move out of the earlier class defination and hence
eligible for encapsulation.
By pulling the Object creation from earlier class to Factory class will make that concreate objects thus created to be eligible
to be called from different client, means there could be many different client calling for same Factory objects, hence
1.Concrete implementation is removed from client code.
2.By Encapsulating the Object creation at only one place, change of code for modification is restricted to that specific area.
Static Method Vs Simple Factory?
Write a class to implement interface: Not only implementing interface BUT could also be a concfete class implementing a method from
a supertype that could be clss/interface
1. case: When the Pizza Store is Single
different classes.(Abstract Factory Pattern)
new means instantiating a concreate class
By Coding to interface, the class will work with any new classes implementing that classes through Polymorphism.
But if using the new operator/concrete classes it would make class liable to modification thus, "closed for modification" fails.
hence we need to take that code out from class to different object and encapsulate the same since addeing new implementation clesses
through modification will need to change the code. The new Object is called factory that takes out the object creation code out from
class defined earlier.PreRequisite: Differentiate which part of Code is supposed to be eligile to move out of the earlier class defination and hence
eligible for encapsulation.
By pulling the Object creation from earlier class to Factory class will make that concreate objects thus created to be eligible
to be called from different client, means there could be many different client calling for same Factory objects, hence
1.Concrete implementation is removed from client code.
2.By Encapsulating the Object creation at only one place, change of code for modification is restricted to that specific area.
Static Method Vs Simple Factory?
Write a class to implement interface: Not only implementing interface BUT could also be a concfete class implementing a method from
a supertype that could be clss/interface
1. case: When the Pizza Store is Single
public class PizzaStore{2. Case: When the Pizza Stores are Different ,so need to abstract the method and have concrete implementations in
SimplePizzaFactory factory; // HAS-A Composition // Reference to Interface
public PizzaStore(SimplePizzaFactory factory){
this.factory = factory;
}
public Pizza orderPizza(String type){
Pizza pizza;// Reference to interface /Abstract Class
pizza = factory.createPizza(type);// Code to be moved from concrete implementation from here to another Object(Factory)
// This is the part to be Encapsulated so that futher modifications will be restricted to this code itself
pizza.prepare();//Remains as earlier, where we had the concrete implementations
}
}
class abstract Pizza{
abstract createPizza();
}
class SimplePizzaFactory{
public Pizza createPizza(String type){// Return Interface to Object Called Pizza
if(type==""){
/*********************/
}elseif(type=""){
/*******************/
}
}
}
different classes.(Abstract Factory Pattern)
public abstract class PizzaStore{//Defined Abstract so that implementations with be the type of factory
public abstract Pizza createPizza(type);
pizza.prepare();
}
abstract createPizza(String type);
/*
belos are the implementations for the PizzaStore
*/
public class NYPizza extends PizzaStore{
//Pizza createPizza(String type){
}
}
public class ChicagoPizza extends PizzaStore{
//Pizza createPizza(String type){
}
}
Here the subclasses ChicagoPizza and NYPizza is supposed to make decision of the Object creation
No comments:
Post a Comment