0% found this document useful (0 votes)
146 views16 pages

Assignment 1 SDP

The Factory Method Pattern allows sub-classes to choose the type of objects to create. It promotes loose coupling by eliminating the need to bind application-specific classes in the code. Instead, the code interacts solely with the interface or abstract class, so it will work with any classes that implement that interface or extend that abstract class. The pattern is used in situations where the class does not know what sub-classes it needs to create objects, or when the parent class chooses object creation for sub-classes. Some examples of its use include the Android Activity factory and a toy molding factory that creates different toy objects based on user selection.

Uploaded by

Harsih Kanga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
146 views16 pages

Assignment 1 SDP

The Factory Method Pattern allows sub-classes to choose the type of objects to create. It promotes loose coupling by eliminating the need to bind application-specific classes in the code. Instead, the code interacts solely with the interface or abstract class, so it will work with any classes that implement that interface or extend that abstract class. The pattern is used in situations where the class does not know what sub-classes it needs to create objects, or when the parent class chooses object creation for sub-classes. Some examples of its use include the Android Activity factory and a toy molding factory that creates different toy objects based on user selection.

Uploaded by

Harsih Kanga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 16

Factory Design Pattern

Abstract:
Factory method is a creational design pattern, i.e., related to object creation. In Factory pattern, we
create object without exposing the creation logic to client and the client use the same common
interface to create new type of object.
The idea is to use a static member-function (static factory method) which creates & returns
instances, hiding the details of class modules from user.

Description:-
When to use:
◆ When a class doesn't know what sub-classes will be required to create

◆ When a class wants that its sub-classes specify the objects to be created.

◆ When the parent classes choose the creation of objects to its sub-classes.

Structure:

Name: Ameya S. Daddikar, College I.D.:: 161070015


Final Year Btech. Computer Engineering, V.J.T.I.
Participants:
● Shape (Product)
Declares an interface for a generic object. This Product defines certain common attributes
and methods for it’s Concrete Products (subclasses).
● ShapeFactory (Factory)
Generates the Product object as per the parameters provided.
● FactoryDemo (SomeClass)
Any program/Class that uses the Factory.

 
Consequences:
1. Adding and removing objects at run-time
● Add object type by simply registering prototype instance with client.
2. Specifying new objects by varying values
● New behaviour implemented by composition, not by creating new classes.
● Client creates new “classes” by delegating responsibility to prototype.
● Reduces no. of classes needed by system.
3. Specifies new objects by varying structure
● Create new objects using a composite object that implements Clone as deep copy.
4. Reduced subclassing
● We clone prototypes directly instead of creating it using a creator class.
● Creator class hierarchies can be eliminated.
5. Configuring an application with classes dynamically
● Load classes into an application directly at run-time.
● Applications that require dynamically loaded classes cannot reference constructors
statically.
● Run-time environment creates an instance of each class when loaded and registers instance
with a prototype manager. The prototype manager then supplies new instances of these
newly loaded classes.

Name: Ameya S. Daddikar, College I.D.:: 161070015


Final Year Btech. Computer Engineering, V.J.T.I.
Problem 1:
Statement:
Consider the toy molding factory. User needs different types of toy such as car, duck, horse, etc. based on his/her
desire. There are different molds are available for specific toy and company may order new molds if required. When
user selects a specific toy then plastic grains input to machine and user get required toy from machine.
Design the system and draw class diagram to use factory method pattern. Implement system in Java.

Class Diagram:

Code:

Name: Ameya S. Daddikar, College I.D.:: 161070015


Final Year Btech. Computer Engineering, V.J.T.I.
The Machine class takes input of the type of Tyo to build, and accordingly sends back the
appropriate subclass of Toy.

Toy is an interface that defines two actions ‘play()’ and ‘display()’. The User later on uses these
functions to interact with the Toy.

Toy has three subclasses viz. Car, Duck and Horse. Each of these classes has their own set of
custom functions and attributes.

Name: Ameya S. Daddikar, College I.D.:: 161070015


Final Year Btech. Computer Engineering, V.J.T.I.
The User class is used as an interaction point for the user to request and play with the toy.

Name: Ameya S. Daddikar, College I.D.:: 161070015


Final Year Btech. Computer Engineering, V.J.T.I.
Problem 2:
Statement:
System need to log the messages of the progress of the task in the system for various purposes such as debugging and
monitoring progress. The log can be created in different formats like .txt, .html, database, xml, etc. User needs to
select type of the log mechanism required for him at the time of configuring system.
Design the system and draw class diagram to use factory method pattern. Implement system in Java.

Class Diagram:

Name: Ameya S. Daddikar, College I.D.:: 161070015


Final Year Btech. Computer Engineering, V.J.T.I.
Code:

The Utils class takes input of the type of Tyo to build, and accordingly sends back the appropriate
subclass of Logger.

Logger is an interface that defines the action ‘log()’ that returns output as string with the required
format..

Logger has three subclasses viz. XMLLogger, TextLogger and XMLLogger. Each of these classes
has their own set of private attributes to customize their output.

Name: Ameya S. Daddikar, College I.D.:: 161070015


Final Year Btech. Computer Engineering, V.J.T.I.
Name: Ameya S. Daddikar, College I.D.:: 161070015
Final Year Btech. Computer Engineering, V.J.T.I.
Name: Ameya S. Daddikar, College I.D.:: 161070015
Final Year Btech. Computer Engineering, V.J.T.I.
Problem 3:
Statement:
Consider a mobile company, it has different plans of offers to different customers. To generate bills of the customer
software needs to specify the plan that is associated with the customer thereby generating adequate bill of the
customer. Currently there are plans such as prepaid plan, post paid plan, offer plan, etc. Final bill is generated based
on plan and the consumed units.
Design the system and draw class diagram to use factory method pattern. Implement system in Java.

Class Diagram:

Code:
Factory Class

Name: Ameya S. Daddikar, College I.D.:: 161070015


Final Year Btech. Computer Engineering, V.J.T.I.
Abstract Plan

Concrete Classes

Name: Ameya S. Daddikar, College I.D.:: 161070015


Final Year Btech. Computer Engineering, V.J.T.I.
Name: Ameya S. Daddikar, College I.D.:: 161070015
Final Year Btech. Computer Engineering, V.J.T.I.
Problem 4:
Statement:
Consider an online supermarket system. Different type of products can be added in the cart. Final billing of the
shopping is based on the items selected in the cart. In advance type of item is not known to the system and it value.
Design the system and draw class diagram to use factory method pattern. Implement system in Java.

Class Diagram:

Name: Ameya S. Daddikar, College I.D.:: 161070015


Final Year Btech. Computer Engineering, V.J.T.I.
Code:

Factory code

Name: Ameya S. Daddikar, College I.D.:: 161070015


Final Year Btech. Computer Engineering, V.J.T.I.
Abstract item

Concrete items

Name: Ameya S. Daddikar, College I.D.:: 161070015


Final Year Btech. Computer Engineering, V.J.T.I.
Known Uses:
The Android Activity uses a factory design pattern for the various types of Activities. An activity is
a single, focused thing that the user can do. Almost all activities interact with the user, so the
Activity class takes care of creating a window for you in which you can place your UI with
setContentView(View). While activities are often presented to the user as full-screen windows, they
can also be used in other ways: as floating windows (via a theme with R.attr.windowIsFloating set),
Multi-Window mode or embedded into other windows.

There are two methods almost all subclasses of Activity will implement:

onCreate(Bundle)
Where you initialize your activity. Most importantly, here you will usually call setContentView(int)
with a layout resource defining your UI, and using findViewById(int) to retrieve the widgets in that
UI that you need to interact with programmatically.
onPause()
Where you deal with the user pausing active interaction with the activity. Any changes made by the
user should at this point be committed (usually to the ContentProvider holding the data). In this
state the activity is still visible on screen.

Known direct subclasses:


AccountAuthenticatorActivity, ActivityGroup, AliasActivity, ExpandableListActivity, ListActivity,
NativeActivity

Related Patterns:
Abstract Factory adds on top of the Simple Factory design pattern by creating an abstraction for
the Factory Class itself. Abstract Factory is used whenever you have families of products you need
to create and you want to make sure your clients create products that belong together.

References:
Design Patterns - Elements of reusable Object-Oriented Software - ​Erich Gamma, Richard
Helm, Ralph Johnson, John M. Vsillides.
Geeks for geeks: ​Factory Method Design Pattern

Summary:
● Factory Method Pattern allows the sub-classes to choose the type of objects to create.
● It promotes the loose-coupling by eliminating the need to bind application-specific classes into
the code. That means the code interacts solely with the resultant interface or abstract class, so
that it will work with any classes that implement that interface or that extends that abstract
class.

Name: Ameya S. Daddikar, College I.D.:: 161070015


Final Year Btech. Computer Engineering, V.J.T.I.

You might also like