law-of-demeter

Law of Demeter in Java

十年热恋 提交于 2021-01-28 07:04:02
问题 I have been building a RTS to improve my Java skills. I have been reading a lot about the Law of Demeter because I want to keep my code clean but I'm still quite confused! At the moment I have some code like this in the view to show how many of a certain ship there are in a fleet on the selected planet: int numberOfFrigates = model.getSelectedPlanet().getFleet().getNumberOfFrigates(); Which from what I understand goes against the Law of Demeter. If I'm only supposed to have 'one dot' do I

law of demeter and planeshift

旧时模样 提交于 2020-01-06 03:22:07
问题 The source code of this game is open source, so I decided to check it out. In it, I found something like: // This ActionManager is basically a controller like in the MVC pattern. void ActionManager::HandleQueryMessage(csString xml, Client* client) { //check the two hands as a start. psItem* item = client->GetCharacterData()->Inventory().GetInventoryItem(PSCHARACTER_SLOT_RIGHTHAND); if(!item || !item->GetItemCommand().Length()) item = client->GetCharacterData()->Inventory().GetInventoryItem

Simple Custom Refactoring in IntelliJ

坚强是说给别人听的谎言 提交于 2020-01-01 04:08:13
问题 This question is a follow-up for this. Say I have some class Foo. class Foo { protected String x = "x"; public String getX() { return x; } } I have a program that uses Foo and violates LoD (Law of Demeter). class Bar { protected Foo foo; public Bar() { this.foo = new Foo(); } public Foo getFoo() { return foo; } } public static void main(String [] args) { Bar bar = new Bar(); String x = bar.getFoo().getX(); } I can refactor this code to use LoD in two steps. ⌥ ⌘ m bar.getFoo().getX() ->

What is Law of Demeter?

有些话、适合烂在心里 提交于 2019-12-22 03:45:51
问题 Let's start with Wikipedia: More formally, the Law of Demeter for functions requires that a method m of an object O may only invoke the methods of the following kinds of objects: O itself m's parameters Any objects created/instantiated within m O's direct component objects A global variable, accessible by O, in the scope of m Rule 1: public class ClassOne { public void method1() { method2(); } public void method2() { } } Rule 2: public class ClassOne { public void method1(ClassTwo classTwo) {

What is Law of Demeter?

岁酱吖の 提交于 2019-12-22 03:45:16
问题 Let's start with Wikipedia: More formally, the Law of Demeter for functions requires that a method m of an object O may only invoke the methods of the following kinds of objects: O itself m's parameters Any objects created/instantiated within m O's direct component objects A global variable, accessible by O, in the scope of m Rule 1: public class ClassOne { public void method1() { method2(); } public void method2() { } } Rule 2: public class ClassOne { public void method1(ClassTwo classTwo) {

Law of Demeter - Data objects

安稳与你 提交于 2019-12-20 12:33:04
问题 I'm trying to follow the Law Of Demeter ( see http://en.wikipedia.org/wiki/Law_of_Demeter , http://misko.hevery.com/code-reviewers-guide/flaw-digging-into-collaborators/ ) as I can see the benefits, however I've become a little stuck when it comes to domain objects. Domain objects do naturally have a chain and sometimes it's necessary to display the information about the entire chain. For instance, a shopping basket: Each order contains a user, delivery info and a list of items Each order

Is it appropriate to repeat data in models to satisfy using law of demeter in collections?

早过忘川 提交于 2019-12-19 10:28:09
问题 This is a contrived example, say I want to list the population of a country that a person has a friend in, here are two setups below. Would it be best to repeat data in the models? I've been told that the Law of Demeter is important to follow, the example is you tell a dog to walk, it is folly to command his legs to walk. In my wealth of inexperience (noob) I have found that the query would be much easier to do when the models repeat data, People.where(:country => friend.country) , vs

How to solve the violations of the Law of Demeter?

我是研究僧i 提交于 2019-12-17 22:03:59
问题 A colleague and I designed a system for our customer, and in our opinion we created a nice clean design. But I'm having problems with some coupling we've introduced. I could try to create an example design which includes the same problems as our design, but if you forgive me I'll create an extract of our design to support the question. We're developing a system for the registration of certain treatments for a patients. To avoid having a broken link to image I'll describe the conceptual UML

Integrity of Law of Demeter preserved by using helper function (removed two dots)?

徘徊边缘 提交于 2019-12-13 17:09:44
问题 public House { WeatherStation station; public float getTemp() { //Law of Demeter has been violated here return station.getThermometer().getTemperature(); } } public House { WeatherStation station; public float getTemp() { //Law of Demeter has been preserved? Thermometer thermometer = station.getThermometer(); return getTempHelper(thermometer); } public float getTempHelper(Thermometer thermometer) { return thermometer.getTemperature(); } } In the code above you can see two different House

How to write read-only accessor functions in an aggregate root class?

ぐ巨炮叔叔 提交于 2019-12-13 07:19:37
问题 Overall design : I have an aggregate class C that contains N member variables of type M_i, i = 1 ... N that each have a common write-only update() interface as well as class-specific read-only accessor functions [F]un_i(), [F] = any letter, i = 1 .. N (they do not have such regular names in reality). Each of the member types M_i forms an independent abstraction of its own, and is used elsewhere in my program. The aggregate class needs to update all the members in a single transaction, so it