抽象工厂模式-未使用的代码 [英] Abstract Factory Pattern - unused code

查看:68
本文介绍了抽象工厂模式-未使用的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Desgin模式,并在 HERE 中遇到了一个非常奇怪的示例。如果我们有一个类:

I'm learning Desgin patterns and come across very weird example in HERE. 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!
}
}

和第二种实现:

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

}
}

这是我的问题,在两种情况下(混凝土工厂)是一个完全没有用且不应存在的方法,但是在创建AbstractFactory类时,我们必须实现这两个方法。在不需要的方法中创建不需要的方法,这是不好的做法吗?

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-您的具体实现不正确。当然,它们不遵循抽象工厂模式。抽象工厂谈论产品系列。抽象工厂样本(根据我的假设)将类似于以下代码。您的示例仅使用一种方法将滥用模式并很快中断。

@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.

我已经回答了类似的问题,请也请注意在下面的示例中使用Abstract Factory而不是factory方法?

I have already answer similar question please have a look to that also What are the real benefits of using the Abstract Factory in the following example, instead of the factory method?

    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{}

这篇关于抽象工厂模式-未使用的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆