c# - Static abstract objects with virtual methods -


i'm working on personal project , i've run issue.

i have object couple of objects have same properties, methods, etc. things differ names, values of properties, , implementation of methods. need common default implementation of methods. right away, interface out of question.

so created base class properties , "default" methods. base class needs abstract. methods virtual can overridden.

the reason need them static objects properties of other objects.

so, example, objects referenced above (for sake of simplicity) objx, objy, objz. derived base, objw.

objcontainer unrelated object, has property of type objw, instance of either objx, objy, objz.

objx, objy, , objz never change. properties readonly. multiple objects of instance objcontainer have objx, objy, or objz.

public class objcontainer1 {     objw processor = new objy;  } public class objcontainer2 {     objw processor = new objy;  } 

how go doing this? wanted keep them static don't have multiple instances of same objects, when of them exact same, really.

do use singleton? factory pattern?

i'm lost direction go (if any). maybe i'm overthinking , there's simple solution/

you want use static classes sparingly. there obvious downsides static classes, such inability take advantage of polymorphic nature of class inheritance since can't inherit static class. time want use static class, really, when have set of related tools want make available across application , don't need maintain state. think of system.math class, example: set of math functions can use anywhere in application. having instance of class doesn't make sense, , rather cumbersome , unnecessary.

i suggest sticking non-static classes , creating instances of classes. if should ever have 1 instance of class, should use singleton, suggested.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -