- Abstract Factory pattern G GKwU interface Gi gva¨‡g related/dependent object mg~‡ni bundle create K‡i| G‡ÿ‡Î Dnv‡`i concrete class mg~n specify Kiv nq bv|
- A_©vr Abstract Factory pattern Gi gva¨‡g GKwU super-factory ‡_‡K wewfbœ Factory create Kiv nq|
- GRb¨ Bnv‡K factories Gi Factory ejv nq|
- G‡ÿ‡Î factories Gi cÖwZwU factory wbR¯^ object mg~n represent K‡i|
UML Diagram
_
AbstractFactory:
Bnv GKwU interface hvnv abstract
product
objects
create
K‡i|
_
ConcreteFactory:
Concrete product objects create Kivi Rb¨ AbstractFactory interface
implement K‡i|
_ AbstractProduct:
Specific Product type object Gi Rb¨ declared interface
_ ConcreteProduct:
Corresponding concrete factory
KZ…©K created product object
hvnv AbstractProduct Interface
†K implement
K‡i|
Non Software Example:
2.1 Abstract Factory Example
Ø
Automobile company ‡Z sheet metal stamping equipment use nq|
Ø
G‡ÿ‡Î stamping
equipment GKwU Abstract Factory
hvnv auto body parts create K‡i.
Ø
Same machinery use K‡i different models of car Gi
Rb¨
o
Right hand doors
o
Left hand doors
o
Right front fenders
o
Left front fenders
o
Hoods etc. create
Kiv nq|
Ø
GLv‡b stamping
equipment n‡jv Abstract Factory
Ø
Right hand doors, Left hand doors, Right front
fenders, Left front fenders, Hoods etc n‡jv
GK GKwU Factory
Ø
GK GKwU specific parts n‡jv Factory
Gi GK GKwU Object.
Steps to Implement:
Step 1: Create an
interface for Shapes.
IShape.cs
public interface IShape
{
void draw();
}
Step 2: Create
concrete classes implementing the same interface.
Rectangle.cs
public class Rectangle:IShape
{
public void draw()
{
Console.WriteLine("Inside
Rectangle::draw() method.");
}
}
Square.cs
public class Square:IShape
{
public void draw()
{
Console.WriteLine("Inside
Square::draw() method.");
}
}
Circle.cs
public class Circle:IShape
{
public void draw()
{
Console.WriteLine("Inside
Circle::draw() method.");
}
}
Step 3: Create an
interface for Colors.
IColor.cs
public interface IColor
{
void fill();
}
Step4: Create
concrete classes implementing the same interface.
Red.cs
public class Red:IColor
{
public void fill()
{
Console.WriteLine("Inside
Red::fill() method.");
}
}
Green.cs
public class Green:IColor
{
public void fill()
{
Console.WriteLine("Inside Green::fill()
method.");
}
}
Blue.cs
public class Blue:IColor
{
public void fill()
{
Console.WriteLine("Inside
Blue::fill() method.");
}
}
Step 5: Create an
Abstract class to get factories for Color and Shape Objects.
IAbstractFactory.cs
public interface IAbstractFactory
{
IColor getColor(String color);
IShape getShape(String shape);
}
Step 6: Create
Factory classes extending AbstractFactory to generate object of concrete class
based on given information.
ShapeFactory.cs
public class ShapeFactory:IAbstractFactory
{
public IShape getShape(String shapeType)
{
if (shapeType == null)
{
return null;
}
if (shapeType.CompareTo("CIRCLE") == 0)
{
return new Circle();
}
else if (shapeType.CompareTo("RECTANGLE") == 0)
{
return new Rectangle();
}
else if (shapeType.CompareTo("SQUARE") == 0)
{
return new Square();
}
return null;
}
public IColor getColor(String color)
{
return null;
}
}
ColorFactory.cs
public class ColorFactory:IAbstractFactory
{
public IShape getShape(String shapeType)
{
return null;
}
public IColor getColor(String color)
{
if (color == null)
{
return null;
}
if (color.CompareTo("RED") == 0)
{
return new Red();
}
else if (color.CompareTo("GREEN") == 0)
{
return new Green();
}
else if (color.CompareTo("BLUE") == 0)
{
return new Blue();
}
return null;
}
}
Step 7: Create a
Factory generator/producer class to get factories by passing an information
such as Shape or Color
FactoryProducer.cs
public class FactoryProducer
{
public static IAbstractFactory getFactory(String choice)
{
if (choice.CompareTo("SHAPE") == 0)
{
return new ShapeFactory();
}
else if (choice.CompareTo("COLOR") == 0)
{
return new ColorFactory();
}
return null;
}
}
Step 8: Use the
FactoryProducer to get AbstractFactory in order to get factories of concrete
classes by passing an information such as type.
AbstractFactoryPatternDemo.cs
public class AbstractFactoryPatternDemo
{
public static void main(String[] args)
{
//get
shape factory
IAbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
//get an
object of Shape Circle
IShape shape1 = shapeFactory.getShape("CIRCLE");
//call
draw method of Shape Circle
shape1.draw();
//get an
object of Shape Rectangle
IShape shape2 = shapeFactory.getShape("RECTANGLE");
//call
draw method of Shape Rectangle
shape2.draw();
//get an
object of Shape Square
IShape shape3 = shapeFactory.getShape("SQUARE");
//call
draw method of Shape Square
shape3.draw();
//get
color factory
IAbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
//get an
object of Color Red
IColor color1 = colorFactory.getColor("RED");
//call
fill method of Red
color1.fill();
//get an
object of Color Green
IColor color2 = colorFactory.getColor("Green");
//call
fill method of Green
color2.fill();
//get an
object of Color Blue
IColor color3 = colorFactory.getColor("BLUE");
//call
fill method of Color Blue
color3.fill();
}
}
Step 9: Verify the output.
Inside
Circle::draw() method.
Inside
Rectangle::draw() method.
Inside
Square::draw() method.
Inside
Red::fill() method.
Inside
Green::fill() method.
Inside
Blue::fill() method.
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন