0% found this document useful (0 votes)
10 views3 pages

Factory in OOP

factory concep in object oriented programming

Uploaded by

elsa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Factory in OOP

factory concep in object oriented programming

Uploaded by

elsa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 3
In Object-Oriented Programming (OOP), the factory pattern is a design pattern used to create objects in a more flexible and maintainable way. Instead of using a direct constructor to create an objec, a factory method or class is used to abstract the instantiation process, allowing for better control and decoupling of object creation from its implementation Key Concepts of the Factory Pattern in OOP: 1. Encapsulation of Object Creation: ‘= Inthe factory pattern the logic for creating objects is encapsulated in a separate method or class. This means that ifthe instantiation logic changes (for example, adding configuration options or different types of objects), the client code does not need to be modified Example: ina BD) coy code public class Shaperactory public shape eneateshape’ if circle") ( return new Circle(); } else if square")) { return new Square(); ) else return mull. 2. Flexibility and Decoupling: ‘The factory pattern decouples the client code from the concrete classes that are instantiated. Instead of hard-coding object creation (using nea() ), the client interacts with an interface or base class and leaves the creation to the factory. This allows the system to scale and accommodate new types of objects without altering the client code, Example: a O coop cote ShapeFactory factory - new ShapeFactory(); Shape ayShape rel 3. Types of Factory Patterns: There are different variations of the factary pattern: ‘© Simple Factory: A basic implementation where a single method creates objects based on input, ‘= Factory Method Pattern: Each subclass implements its own factory method to create an object. This adds more flexibility by allowing subclasses to decide which class to instantiate, bxample: im cons ce abstract class AninaiFactory ( abstract Aninal createtninal(); ) class Dogtactory extends Aninalfactory public Dog ereatedninal() { return new Dog(); ) ) elass CatPactory extends AnimalFactory { public cot createdninal ‘Abstract Factory Pattern: A factory of factories. It provides an interface to create families of related objects without specifying their concrete classes. xample: im cons cote Gutfactory { public actton createbutton() { return new WinButton(); ) public checibox createCheckbox() { return wacsutton(); ) public checkbox ereatecheckbox() { return new Haccheckbox 4, Advantages: + Encapsulation of Creation Logic: The factory pattern hides the details of object creation from the client. = Decoupling: It decouples the client code from specific classes, allowing for easier code maintenance and updates. = Extensibility: New types of objects can be introduced without altering the existing client code. 5, Disadvantages: + Complexity: The factory pattern can add complexity to the system, especially ifthe creation logic is simple and doesn't require abstraction. © Overuse: Using factories when they aren't necessary can lead to over-engineering. Practical Example: Imagine you have different types of database connections (eg, MySQl, PostgreSQl), A factory pattern could be used to abstract the creation of these connections based on configuration, without the client needing to know which database class to instantiate. in coop cote pubitc class oatabaseFactory { public Database ceeateDatabase(String dbType) { LF (dbType.eqvals("aysql")) { return new Hysaldatabase(); ) else if (dbType.cquals(“postgres')) { return new Postgresbatabase(); ) else ( throw new TlegalArgunentException("Unkrown database type In conclusion, the factory pattern in OOP helps manage object creation, making systems more flexible and maintainable by abstracting and decoupling the instantiation process.

You might also like