Assignment 1 SDP
Assignment 1 SDP
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:
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.
Class Diagram:
Code:
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.
Class Diagram:
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.
Class Diagram:
Code:
Factory Class
Concrete Classes
Class Diagram:
Factory code
Concrete items
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.
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.