-2

Personally, I call this the Blueprint Pattern; however, I am sure someone else must have already named it. Static members, are in most cases, not a good thing. In languages such as C# and Java, they remove polymorphism and encourage poor class design. However, many times, there are properties that all instances share.

//Original class where shared properties are static
class StandardHenchman
{
    static int MaxHealth;
    static int Speed;
    int CurrentHealth;
    int X;
    int Y;
}

//More maintainable and flexible design 
class NPCType
{
    int MaxHealth;
    int Speed;
}

class NPC
{
   NPCType Type;
   int CurrentHealth;
   int X;
   int Y;
}

henchman1 = new NPC();
henchman1.Type = standardHenchman;
henchman2 = new NPC();
henchman2.Type = standardHenchman;

It is possible to just duplicate the shared values in every instance, but I think that also comes with problems.

What is the pattern called where you declare shared/static members with composition?

closed as primarily opinion-based by gnat, Doc Brown, Robert Harvey, Jörg W Mittag, Greg Burghardt Apr 29 '17 at 14:34

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • This is not a pattern, it is a refactoring. And since it I cannot find it in Fowler' catalog, I doubt there is a broadly accepted term for this transformation. – Doc Brown Apr 28 '17 at 21:10
  • Well, is there a name for if you just do it that way initially? If not, I am just going to keep calling it the blueprint pattern – TheCatWhisperer Apr 28 '17 at 21:12
  • Wow, looking at the down votes, people love their static members :D – TheCatWhisperer Apr 28 '17 at 21:13
  • 2
    Your refactoring doesn't have the same semantics as the original (your henchmen no longer share the same MaxHealth and Speed), so it's not really a refactoring either, but merely a rewrite. In other news, "Blueprint" doesn't seem like a very good name for this. – Robert Harvey Apr 28 '17 at 21:19
  • 2
    Not every tiny code transformation has a special name (or needs one). I would call it 1. "creation of a new type which bundles two scalar values" followed by 2. "replacing a static variable by a non-static one". Note you are doing two unrelated things here, not just one. – Doc Brown Apr 28 '17 at 21:34
0

It's similar to the Flyweight pattern in that "the object is divided into two pieces: the state-dependent (extrinsic) part, and the state-independent (intrinsic) part. "

But it is different in that the motivation behind Flyweight is saving memory (not code reuse) when instantiating a lot of objects. It does so by separating the part that is common to all instances in a separate class, including behavior, not necessarily because they are static. Also Flyweight involves a factory and caching of objects.

I would say it is a simple version of Flyweight albeit, of course, not "the" Flyweight.

  • I looked more into flyweight, and this is best name for it, I don't think the Wikipedia article makes it as clear as others. – TheCatWhisperer Apr 29 '17 at 22:12
  • In java generally you 'code to interfaces'. And the interface would hold the static data, not the implementation. – Richard May 2 '17 at 1:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.