Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 10

Creational design patterns abstract the instantiation process.

They help make a system independent of how its objects are created, composed, and represented. Creational patterns become important as systems evolve to depend more on .object composition than class inheritance. There are two recurring themes in these patterns. First, they all encapsulate knowledge about which concrete classes the system uses. Second, they hide how instances of these classes are created and put together. The creational patterns give you a lot of flexibility in what gets created, who creates it, how it gets created, and when. They let you configure a system with "product" objects that vary widely in structure and functionality. Configuration can be static (that is, specified at compile-time) or dynamic (at run-time).

Factory Pattern is a way for an object to initiate the creation of another object without having to know the class of object created. The Abstract Factory Pattern is a way for objects to initiate the creation of a variety of different kinds of objects without knowing the classes of the objects created, but ensuring that the classes are a correct combination. The Builder Pattern is a way to determine the class of an object created by its content or context. The Prototype Pattern allows an object to create customized objects without knowing their exact class or the details of how to create them. The Singleton Pattern is a way for multiple objects to share a common objects without having to know whether it already exists.

Intent:
It creates objects without exposing the instantiation logic to the client. It refers to the newly created object through a common interface. It provides an interface for creating families of related or dependent objects without specifying their concrete classes. When to use a Factory Pattern? 1. When a class does not know which class of objects it must create. 2. A class specifies its sub-classes to specify which objects to create. 3. In programmers language (very raw form), you can use factory pattern where you have to create an object of any one of sub-classes depending on the data provided.

When we are using new operator, we are instantiating a concrete class. Means an implementation, not an interface. When this type of code is used, then when if there are any changes or extensions, you have to open all the code and examine what needs to be added or deleted. What is wrong in using new? When code makes the use of lots of concrete classes, it creates a trouble when code may have to be changed as new concrete classes are added. So , code will no be closed for modification. To extend with new concrete types, you have to reopen it. But when you code is written to an interface, then it will work with any new classes implementing that interfaces through polymorphism. Important Principle: Identify the aspects that vary and separate them from what stays the same

Product defines the interface of objects the factory method creates ConcreteProduct implements the Product interface Creator declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object. may call the factory method to create a Product object. ConcreteCreator overrides the factory method to return an instance of a ConcreteProduct.

Pizza orderPizza() { Pizza pizza = new Pizza(); pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; }

Pizza orderPizza(String type) { Pizza pizza = new Pizza(); if (type.equals(cheese) { pizza = new CheesePizza(); } else if (type.equals(greek) { pizza = new GreekPizza(); } pizza.prepare(); pizza.bake(); pizza.cut(); pizza.box(); return pizza; }

Here when you need more than one type of pizza, passing the type of pizza to orderPizza(). Based on the type, instantiate the concrete class and assign it to the pizza instance variable.

Pizza orderPizza(String type) { Pizza pizza = new Pizza(); if (type.equals(cheese)) { pizza = new CheesePizza(); } else if (type.equals(greek)) { This is what varies. So as per the selection changes, have to modify pizza = new GreekPizza(); code over here. } else if( type.equals(veggie)) { pizza = new VeggiePizza(); } pizza.prepare(); pizza.bake(); This will be stay the same. So, this code will not be change. pizza.cut(); pizza.box(); return pizza; }

You might also like