質問

I am trying to see if there is a design pattern that can solve this:

I have created an abstract class A with properties a,b and c. I have another class B that extends the class A and adds more properties: x,y,z. Then I have another class C that again extends A and adds i,j,k. Lastly I have a factory method that determines which instance to create B or C and thats the part where I need help. How or what should this factory method return: If it returns an instance of A than I wouldnt know which specific instance was created B or C? I need this factory method to create a concrete instance. Now, I know that I could write static method like createB or createC but I am looking for a more general solution maybe another design pattern here.

Update: The reason I want to know the concrete class is that I need to pass this object to a frontend jsp. That JSP would need to know what specific class was instantiated so it can call the appropriate getters.

役に立ちましたか?

解決

I would Keep Factory Pattern as it should. so the return type would be the Abstract A class. B & C Should also inherit their properties through Proxy Pattern.

So Make CAble & BAble Interfaces and use C & B as instances of A (as they're both generated by the same factory), then cast B & C to act as their Interfaces Describe.

Cheers!

UPDATE:

I think I've figured what you need, picture the following: In a School Page. There is a general view of Students & Professors, both have common and individual fields, but the common request is schoolMember.

class SchoolMember // The return type of your Factory
  -name
  +getView():SchoolMemberView // Will be used by the View//View Model

Interface Professor
  getProfession()
Interface Student
  getSemester() 

class FacultyMamber: SchoolMember,Professor
  -profession
  +getView():SchoolMemberView
class UniStudent: SchoolMember,Student
  -semmester
  +getView():SchoolMemberView

他のヒント

By the sound of your description both B and C have nothing in common with A. You've mentioned that each class has particular properties but nothing about what they have in common / how they are related. If there is no relationship between your classes you shouldn't be using inheritence.

However, if there is a common relationship which you have not mentioned in your question, the factory method pattern would probably be what you're looking for.

The only thing that comes to mind when reading your problem is the abstract factory pattern:

http://en.wikipedia.org/wiki/Abstract_factory_pattern

Factory method should return an instance of A. If you need to know if concrete object is B or C, you can use instanceof operator, which is Reflection pattern. Ideally, you shouldn't need to know if you have B or C; that logic should all be handled by polymorphism of methods in A.

Exactly the point mentioned as a warning in the description of Factory pattern.

When you design an application just think if you really need it a factory to create objects. Maybe using it will bring unnecessary complexity in your application. Anyway if you have many object of the same base type and you manipulate them mostly as abstract objects, then you need a factory. I you're code should have a lot of code like the following, reconsider it.

if (genericProduct typeof ConcreteProduct)
    ((ConcreteProduct)genericProduct).doSomeConcreteOperation();

Source : http://www.oodesign.com/factory-method-pattern.html

I would suggest a Builder pattern that will know what object to build based on the specialized field set to the Builder.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top