The factory method and the abstract factory pattern use
different object oriented
mechanisms are used to create objects. Explain what mechanism each
pattern uses.
(shortly explanation)
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.
A factory pattern is one of the core design principles to create an object, allowing clients to create objects of a library(explained below) in a way such that it doesn’t have tight coupling with the class hierarchy of the library.
Abstract Factory design pattern is one of the Creational pattern. Abstract Factory pattern is almost similar to Factory Pattern is considered as another layer of abstraction over factory pattern. Abstract Factory patterns work around a super-factory which creates other factories.
Abstract factory pattern implementation provides us a framework that allows us to create objects that follow a general pattern. So at runtime, abstract factory is coupled with any desired concrete factory which can create objects of desired type.
Abstract Factory provides interfaces for creating families of related or dependent objects without specifying their concrete classes.
Client software creates a concrete implementation of the
abstract factory and then uses the generic interfaces to create the
concrete objects that are part of the family of objects.
The client does not know or care which concrete objects it gets
from each of these concrete factories since it uses only the
generic interfaces of their products.
So with this idea of Abstract Factory pattern, we will now try to create a design that will facilitate the creation of related objects.
Let’s take an example, Suppose we want to build a global car factory. If it was factory design pattern, then it was suitable for a single location. But for this pattern, we need multiple locations and some critical design changes.
We need car factories in each location like IndiaCarFactory, USACarFactory and DefaultCarFactory. Now, our application should be smart enough to identify the location where it is being used, so we should be able to use appropriate car factory without even knowing which car factory implementation will be used internally. This also saves us from someone calling wrong factory for a particular location.
Here we need another layer of abstraction which will identify the location and internally use correct car factory implementation without even giving a single hint to user. This is exactly the problem, which abstract factory pattern is used to solve.
The factory method and the abstract factory pattern use different object oriented mechanisms are used to create objects....
The factory method and the abstract factory pattern use different object oriented mechanisms are used to create objects. Explain what mechanism each pattern uses. (shortly explanation)
Consider the following Scala code that uses classes, objects, inheritance and dynamic method binding abstract class A { def m) Int 1 val a Int = 2 class B extends A { val b: String "one" override def m() : Int = 3 def n(): Int 4 class C extends A { val x : Int 5 override def m() : Int = 6 val array an array of ten A objects array (5) val obj: A obj.m() = (a)...
Consider an object oriented program for a temperature controller. What objects could use to do this? What methods and attributes would these objects have?
(The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make implement the Edible interface. howToEat() and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML diagram for the classes and the interface 2. Use Arraylist class to create an...
Exercise 8 (The interface class-like) Assume you have the Edible interface with its abstract method Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make Chicken and Cow as subclasses of Dairy. The Sheep and Dairy classes implement the Edible interface. howToEat) and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML...
Program in Java: Use the Factory pattern to address the following challenge... Your job is to make it easy to obtain a Pet from a factory-like class: ● Pet is an abstract class that has 2 attributes ○ Name ○ Sound ● You should create 3 concrete types of pets ○ Parakeet (makes the sound "Tweet tweet") ○ Dog (makes the sound "Woof woof") ○ Lion (makes the sound "Roar roar") ● You should create a factory class that allows...
JAVA CODING: (14 points) Abstract Class exercise You are given two abstract classes with no abstract method in the NoGo.java file. The purpose of this exercise is to get familiar with abstract class and its object creation 4. Follow the steps below: (be sure common on each to score points) 1) 2) 3 points) Create a subclass Go1 with Nogo1 as its super class and create Go1() constructor 3) (2 points) Now inside the NoGo class create an instance of...
Briefly explain the terms used in object-oriented programming with examples. NO COPY AND PASTE PLEASE USE UR OWN WORDS
Java is an object-oriented programming language that enables us to define classes and to instantiate them into objects. These objects then call each other’s methods to implement the behavior of the application. The Unified Modeling Language (UML) is an object-oriented visual notation to document the design of object-oriented classes. For this discussion, you will practice designing a Java class called Course, drawing a UML class diagram for the Course class, and then implementing the Course class in Java code. Review...
Imagine 4 different objects with the same coefficient of static friction µs. Use the system to set and test these 4 masses. Rank in order from smallest to largest: the minimum applied force required to make the objects move if their masses are: Object 1 = m/2 Object 2 = m Object 3 = 2m Object 4 = 4m Now imagine that the objects in the question above are pushed with a force F for 4 seconds...