Abstract Factory Pattern - unused code


I'm learning Desgin patterns and come across very weird example in <a href="https://www.tutorialspoint.com/design_pattern/abstract_factory_pattern.htm" rel="nofollow">HERE</a>. If we got a class:

public abstract class AbstractFactory { abstract Color getColor(String color); abstract Shape getShape(String shape) ; }

which as we can see, has 2 types of methods which creates Objects: colors and shapes. This class is abstract so we have to create concrete implementation of this, so lets assume that we have:

public class ShapeFactory extends AbstractFactory { @Override public Shape getShape(String shapeType){ // I skip implementation to keep post brief } @Override Color getColor(String color) { return null; // It's useless method in this class! } }

and second implementation:

public class ColorFactory extends AbstractFactory { @Override public Shape getShape(String shapeType){ return null; // It's useless method in this class! } @Override Color getColor(String color) { // I skip implementation to keep post brief } }

And here comes my question, in both cases (concrete factories) there is an method that is completly useless and shoudn't be there, but as we created AbstractFactory class we have to implement both methods. Isn't it bad practice in programming to create useless methods in classes that don't need it? Should it be done in other way not as website suggest?


@Michael213 - Your concrete implementations are not correct. For sure they do not follow Abstract Factory pattern. Abstract factory talks about families of product. abstract factory sample (with my assumptions) will look like following code. your example using only one method will be misuse of pattern and will break soon.

I have already answer similar question please have a look to that also <a href="https://stackoverflow.com/questions/46384358/what-are-the-real-benefits-of-using-the-abstract-factory-in-the-following-exampl/46407888#46407888" rel="nofollow">What are the real benefits of using the Abstract Factory in the following example, instead of the factory method?</a>

public abstract class AbstractFactory { abstract Color getColor(String color); abstract Shape getShape(String shape) ; } /** * CONCRETE FACTORY1 */ class HighResolutionFactory extends AbstractFactory{ Color getColor(String color){ return new HighResolutionColor(); } Shape getShape(String shape){ return new HighResolutionShape(); } } /** * CONCRETE FACTORY2 */ class LowResolutionFactory extends AbstractFactory{ Color getColor(String color){ return new LowResolutionColor(); } Shape getShape(String shape){ return new LowResolutionShape(); } } class Color{} // ABSTRACT PRODUCT 1 class Shape{} // ABSTRACT PRODUCT 2 class HighResolutionColor extends Color{}// CONCRETE PRODUCT1 FACT 1 class HighResolutionShape extends Shape{}// CONCRETE PRODUCT2 FACT 1 class LowResolutionColor extends Color{}//... class LowResolutionShape extends Shape{}


Yes, that tutorial doesn't seem the best in that regards. It is not ideal although it still counts as a factory design pattern.


AbstractFactory is wrong. You do not have to think of a factory that makes different objects. It is right to make separate factories for each different type.

public interface AbstractColorFactory { public Color getColor(String color); } public interface AbstractShapeFactory { public Shape getShape(String shape); } public class ColorFactory implements AbstractColorFactory { public Color getColor(String color) { return .... } } public class ShapeFactory implements AbstractShapeFactory { public Shape getShape(String shape) { return .... } }


  • Libgdx NESTED ShapeRenderer not drawing lines
  • How can I fill a circle in libgdx?
  • Finding if my mouse is inside a rectangle in Java
  • c# recurring event (like for a calendar)
  • Simplifying the use of meshgrid in Matlab
  • Installing SSL cert on Amazon EC2 ELB
  • C++ calling the default constructor with parens vs without parens [duplicate]
  • C# foreach - Is collection computed with each iteration? [duplicate]
  • functions and when to use brackets/parenthesis
  • Rails Route parameters in AngularJS
  • web shop (shopping cart) on google app engine
  • Can I customize a Jackson ObjectMapper by adding a module?
  • Spree, Ruby on Rails - Add to cart multiple variants of the same product
  • How to trick Node.js to load .js files as ES6 modules?
  • When interface inheritance in Java is useful?
  • How to Divide an array on c#?
  • Returning this from a constructor function in JS
  • WP7 difficulties binding data to listbox itemssource - won't refresh
  • Is there a way to choose which files are displayed to the user via the standard OPENFILE dialogs?
  • CodeIgniter URI Parameter is partially bypassing an “if” statement
  • For loop with if condition on multiple R functions
  • Combining two different ActiveRecord collections into one
  • android.support.v7.widget.Toolbar VectorDrawableCompat IllegalStateException when using support lib
  • Get history of file changes from TFS to implement custom “blame”-behaviour of exceptions
  • How to use carriage return with multiple line?
  • Abort upload large uploads after reading headers
  • Refering to the class itself from within a class mehod in Objective C
  • Data Validation Drop Down Box Arrow Disappearing
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Free memory of cv::Mat loaded using FileStorage API
  • Memory offsets in inline assembly
  • Turn off referential integrity in Derby? is it possible?
  • LevelDB C iterator
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can i traverse a binary tree from right to left in java?
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize