Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+6 votes

Anyone has a better version of explaining the factory design patterns since there is an abstract and a normal one. The reading is confusing, the video helps a bit but i need a textual context.

asked in CSC305 Fall 2021 by (1 point)
+1

The Factory design pattern allows us to make objects without specifying the exact class of the object that will be created and at the same time keep the privacy of the code (by allowing us to encapsulate object creation) from the client/user. From what I have understood, factory pattern is useful when the developer does not initially know the type of objects that need to be created, we use the Factory design pattern when we want to define the class of an object at runtime. A good example would be creating a game where new characters might need to be added later to add to the game. It uses a shared interface, which can be used by the client to establish new types of objects. The main goal of this design pattern is to provide users/clients with an interface for creating new objects in a superclass. The factory design pattern allows the sub-classes to choose the type of objects it wants to create. The video was very helpful in understanding the working of factory design patterns. I noticed how the objects that were returned from the factory methods were referred to as “products”. The client is introduced to EnemyShipFactory which is the factory, which in turn uses an abstract class called EnemyShip. From EnemyShip, implementation is provided to UFOEnemyShip and RocketEnemyShip class.

1 Answer

+2 votes

Factory design pattern is a convenient software development tool that uses an interface for creating an object, but this allows the subclasses to decide which type of object to create. This type of pattern is very dynamic and it is useful when the implementation of an interface changes often with new types of objects. If you try to create new types of objects repeatedly that follow a specified trend, then factory pattern is efficient to use.

answered by (1 point)
...