2

I have a JavaScript question. Imagine a virtual pen. There are many different possible nibs the pen can have, although the pen can only have one nib. I want to create a pen object and then assign it a nib. The nib needs to access the properties and methods of the pen.

This is probably a well-known design pattern. What is the name of this design pattern?

function Pen() {
  var p1=5;
  this.nib=null;
}

function Nib1() {
  // needs access to p1.
}

function Nib2() {
  // needs access to p1.
}

var p = new Pen();
var n1 = new Nib1();
p.nib = n1;
// n1 needs access to p1
Jim Andrews
  • 375
  • 1
  • 2
  • 14
  • 1
    I honestly believe there is no such pattern because it is not a good design at all. If anything i would consider this an antipattern. It creates circular dependencies between those classes (because you say Nib needs access to Pen, but why?), which is always a bad thing. Things like Strategy pattern are supposed to address such problems. – K. Kirsz Jul 07 '17 at 18:44
  • 1
    What did you mean by "needs access to p1", generally the access is unidirectional to avoid circular dependency whenever possible (https://en.wikipedia.org/wiki/Circular_dependency). Maybe these thoughts on OOP concepts would help you ? (http://www.adobe.com/devnet/actionscript/learning/oop-concepts/composition-and-aggregation.html or https://stackoverflow.com/questions/2218937/has-a-is-a-terminology-in-object-oriented-language) I have a feeling you're just looking for "Composition" , where Pen has-a Nib, and Nib depends on some information that Pen passes into Nib's functions. – K.F Jul 07 '17 at 18:49
  • I'm not sure if you're making a strange analogy or you want to create a pen in js. You typically want to stay away from this type of co-dependant relationship...try looking at functional programming and closure. It's a solid way to use JS. Going down the whole OOP route with JS can get messy pretty fast. – Matthew Brent Jul 07 '17 at 18:52
  • I wouldn't say this is a JavaScript question so much as it is a general design question - this is probably a better fit for https://softwareengineering.stackexchange.com/ – TheHans255 Jul 07 '17 at 19:07
  • How does the below look instead? Basically I need a situation where a pen has a nib (could be any one of a number of possible nibs). function Pen() { this.p1=5; this.nib=null; } function nib1() { // needs access to p1. this.p1=78; } function nib2() { // needs access to p1. this.p1 =88; } var pen = new Pen(); pen.nib=nib1; pen.nib(); – Jim Andrews Jul 07 '17 at 19:07
  • Sorry, how do I include line breaks in comments? – Jim Andrews Jul 07 '17 at 19:08
  • 1
    "Name that thing" doesn't make for a very good question on SO. Perhaps you could describe your specific problem instead? – Robert Harvey Jul 07 '17 at 19:09
  • @TheHansinator when referring other sites, it is often helpful to point that [cross-posting is frowned upon](https://meta.stackexchange.com/tags/cross-posting/info) – gnat Jul 07 '17 at 19:35

1 Answers1

0

It sounds a bit like using a constructor. This chapter describes a similar situation but with rabbits instead of pens: http://eloquentjavascript.net/06_object.html#constructors.

The pen would be the constructor:

function Pen(nib) {
  // shared scope
  this.nib = nib;
}

Then you can create

var bluePen = new Pen(blue)

... Although I guess it's slightly different because you're describing a single pen and with the constructor you can end up with multiple instances.

Billy Reilly
  • 1,422
  • 11
  • 11