Wrong OO architecture, sharing objects and dependencies? Maybe i'm missing a design pattern here?

Asked
Active3 hr before
Viewed126 times

7 Answers

90%

I'm sure this isn't correct. There must be something that i'm missing... i understand and use Dependency Injection, but i'm quite sure a Dependency Injection Container will not solve my problem., @vinny you don't understand what I'm saying. Don't have your client instantiate a new model object, inject it instead. It doesn't matter whether it's MVC or not. Don't mix factory and business logic. – Madara's Ghost May 14 '12 at 5:24 ,Everything is beautiful here. Client is responsible for connecting and running commands through Socket to a small C application i built. I'm just playing with architecture here. Client::getModel returns an instance of Model. During getModel, i inject Client inside Model through the constructor. Something like this:,I can't really speak to your entire design because you didn't post most of it, but I think there is a pattern you are missing. One of the core OO design principles is that you don't ask an object for data/objects and then operate on that data/object, you ask an object to DO something for you--perhaps that part was just edited for brevity, but you might want to consider doing less "Getting".

If you would post the code for setClient() and getClient() I could verify that.

setClient($client) {
   $this - > client = $client;
}

getClient() {
   return $this - > client;
}
load more v
88%

Yes, you can hve polymorphism w/o inheritance. But that would not be object oriented. You really need the full complement to claim that: encapsulation, polymorphism, inheritance and abstraction. ,Polymorphism. Literally, shape-changing, this concept allows one object or method, whether it’s a generic, an interface, or a regular object, to serve as the template for other objects and methods. There are many forms of polymorphism. A single function can be overloaded, shape-shift and adapt to whichever class it’s in. Object oriented programming tends to use a lot of subtyping polymorphism and ad-hoc polymorphism, but again, this is not a concept limited to OOP. ,Further, you can have polymorphism without inheritance. Role/trait-based objects allow for polymorphism, as does duck-typing.,Now, all the other tenets of OOP are useful too, but I think the issue is that the examples used to teach those concepts, especially inheritance and polymorphism, are often silly if not downright bad designs.

So what about the developers themselves? Our Developer Survey this year shows that they are gaining more and more purchasing influence. Well, if we also look at what developers prefer to work with, Haskell and Scala are among the most loved programming languages. Scala gets you the second highest salary. So maybe with more FP evangelism, they will climb the list of most popular languages, too.

There is some movement though, big companies like Twitter are running their backend almost entirely on Scala code. Facebook who has been recently applying Haskell and many of the major OOP languages are also adopting functional features. .NET has LINQ and Java 8 introduced Lambdas. JavaScript is increasingly functional despite the introduction of classes in ES6. Swift may be the happy medium between an object-oriented and a functional language. So maybe there’s no need to choose: you can have your class Cake and EatCake() it too. 

class Cake
load more v
72%

Although design patterns have been applied practically for a long time, formalization of the concept of design patterns languished for several years.[4] ,In order to achieve flexibility, design patterns usually introduce additional levels of indirection, which in some cases may complicate the resulting designs and hurt application performance. ,Design patterns may be viewed as a structured approach to computer programming intermediate between the levels of a programming paradigm and a concrete algorithm. ,Software design techniques are difficult to apply to a broader range of problems.[citation needed] Design patterns provide general solutions, documented in a format that does not require specifics tied to a particular problem.

65%

There are two ways in which the Flyweight pattern can be applied. The first is at the data-layer, where we deal with the concept of sharing data between large quantities of similar objects stored in memory., Remember that not every algorithm, best practice or solution represents what might be considered a complete pattern. There may be a few key ingredients here that are missing and the pattern community is generally wary of something claiming to be one unless it has been heavily vetted. Even if something is presented to us which appears to meet the criteria for a pattern, it should not be considered one until it has undergone suitable periods of scrutiny and testing by others.,The disadvantages of this pattern are that there may be easier or more optimal approaches to achieving this goal (e.g. deep object extension / merging) which I cover earlier in the section.,The general idea behind the Command pattern is that it provides us a means to separate the responsibilities of issuing commands from anything executing commands, delegating this responsibility to different objects instead.

The most common approach to achieving this is by defining a JavaScript function where we then create an object using the new keyword. this can be used to help define new properties and methods for the object as follows:

// A car "class"
function Car(model) {

   this.model = model;
   this.color = "silver";
   this.year = "2012";

   this.getInfo = function() {
      return this.model + " " + this.year;
   };

}

We can then instantiate the object using the Car constructor we defined above like this:

var myCar = new Car("ford");

myCar.year = "2010";

console.log(myCar.getInfo());

// Each of the following options will create a new empty object:

var newObject = {};

// or
var newObject = Object.create(Object.prototype);

// or
var newObject = new Object();

// ECMAScript 3 compatible approaches

// 1. Dot syntax

// Set properties
newObject.someKey = "Hello World";

// Get properties
var value = newObject.someKey;

// 2. Square bracket syntax

// Set properties
newObject["someKey"] = "Hello World";

// Get properties
var value = newObject["someKey"];

// ECMAScript 5 only compatible approaches
// For more information see: http://kangax.github.com/es5-compat-table/

// 3. Object.defineProperty

// Set properties
Object.defineProperty(newObject, "someKey", {
   value: "for more control of the property's behavior",
   writable: true,
   enumerable: true,
   configurable: true
});

// If the above feels a little difficult to read, a short-hand could
// be written as follows:

var defineProp = function(obj, key, value) {
   var config = {
      value: value,
      writable: true,
      enumerable: true,
      configurable: true
   };
   Object.defineProperty(obj, key, config);
};

// To use, we then create a new empty "person" object
var person = Object.create(Object.prototype);

// Populate the object with properties
defineProp(person, "car", "Delorean");
defineProp(person, "dateOfBirth", "1981");
defineProp(person, "hasBeard", false);

console.log(person);
// Outputs: Object {car: "Delorean", dateOfBirth: "1981", hasBeard: false}

// 4. Object.defineProperties

// Set properties
Object.defineProperties(newObject, {

   "someKey": {
      value: "Hello World",
      writable: true
   },

   "anotherKey": {
      value: "Foo bar",
      writable: false
   }

});

// Getting properties for 3. and 4. can be done using any of the
// options in 1. and 2.

// Usage:

// Create a race car driver that inherits from the person object
var driver = Object.create(person);

// Set some properties for the driver
defineProp(driver, "topSpeed", "100mph");

// Get an inherited property (1981)
console.log(driver.dateOfBirth);

// Get the property we set (100mph)
console.log(driver.topSpeed);

function Car(model, year, miles) {

   this.model = model;
   this.year = year;
   this.miles = miles;

   this.toString = function() {
      return this.model + " has done " + this.miles + " miles";
   };
}

// Usage:

// We can create new instances of the car
var civic = new Car("Honda Civic", 2009, 20000);
var mondeo = new Car("Ford Mondeo", 2010, 5000);

// and then open our browser console to view the
// output of the toString() method being called on
// these objects
console.log(civic.toString());
console.log(mondeo.toString());

function Car(model, year, miles) {

   this.model = model;
   this.year = year;
   this.miles = miles;

}

// Note here that we are using Object.prototype.newMethod rather than
// Object.prototype so as to avoid redefining the prototype object
Car.prototype.toString = function() {
   return this.model + " has done " + this.miles + " miles";
};

// Usage:

var civic = new Car("Honda Civic", 2009, 20000);
var mondeo = new Car("Ford Mondeo", 2010, 5000);

console.log(civic.toString());
console.log(mondeo.toString());
load more v
75%

Let’s say it again: Python is a high-level programming language with dynamic typing and dynamic binding. I would describe it as a powerful, high-level dynamic language. Many developers are in love with Python because of its clear syntax, well structured modules and packages, and for its enormous flexibility and range of modern features.,Again, we just demonstrated how implementing this wonderful design pattern in Python is just a matter of using the built-in functionalities of the language.,These might not be patterns in the traditional sense, but these are rules that define the “Pythonic” approach to programming in the most elegant and useful fashion.,Let’s say some words about the infamous Duck Typing approach to see how it fits in this paradigm: program to an interface.

What is so good about Python’s philosophy? Let’s start with this (explore it in the Python terminal):

> >>
import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren 't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one--and preferably only one--obvious way to do it.
Although that way may not be obvious at first unless you 're Dutch.
Now is better than never.
Although never is often better than * right * now.
If the implementation is hard to explain, it 's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea--
let 's do more of those!
load more v
40%

This process is automatic. Your browser will redirect to your requested content shortly.

22%

A class using the singleton design pattern will include,,In this post, we will go through one basic design pattern for each classified type.,But with the decorator pattern will require only 16 classes in this scenario. Wanna bet?,A public static method, to return the single instance of the class.

Eager Beaver

public class EagerSingleton {
   // create an instance of the class.
   private static EagerSingleton instance = new EagerSingleton();

   // private constructor, so it cannot be instantiated outside this class.
   private EagerSingleton() {}

   // get the only instance of the object created.
   public static EagerSingleton getInstance() {
      return instance;
   }
}
load more v

Other "undefined-undefined" queries related to "Wrong OO architecture, sharing objects and dependencies? Maybe i'm missing a design pattern here?"