class-design

How to model cycles between immutable class instances?

柔情痞子 提交于 2019-12-09 11:47:37
问题 Immutable classes are great but there is one big problem i cant think of a sensible way to solve - cycles. class Friend { Set<Friend> friends(); } How does one model Me having You as a friend who in turn has me as a Friend back ? IMMUTABILITY This class from the outside world should definitely be immutable. The value held internally should be constant for the purposes of equality checks. 回答1: [[[ Edit: Added code to demonstrate fully immutable concept ]]] That's why builders are so nice for

In what cases superclass shouldn't be abstract?

杀马特。学长 韩版系。学妹 提交于 2019-12-09 11:20:39
问题 In this thread I found some interesting moment, If class uses only as superclass there isn't rule to make it abstract. Why so? Thanks 回答1: It all depends on whether or not it makes sense to have instances of the class . Suppose you for instance have one class Dog and one class Cat . They both extend Animal . Now an Animal may have a name and a few methods, but it doesn't make sense to have Animal s running around. An Animal is... well an abstract notion. In other circumstances you may have

Is it good practice override methods with a higher visibility?

南笙酒味 提交于 2019-12-08 16:41:51
问题 Answering this question: How to GUI - Using paintcomponent() to initialize a GUI and then to add GUI based on mouse I've stated this: You don't override paintComponent() properly. This is a protected method, not public. If you add @Override annotation on this method then the compiler will complain. But @peeskillet wisely pointed out this: The compiler will not complain about public or protected with paintComponent . You can override with a higher visibility but not a lower one. public is

How to change the access modifier of a user control

丶灬走出姿态 提交于 2019-12-08 14:45:52
问题 I have a user control created in xaml, lets name it "View". In the View.xaml.cs I changed the access modifier for the class View to internal: internal partial class View : ViewBase { ... } After changing the access modifier the compiler states the error: Partial declarations of 'A.B.View' have conflicting accessibility modifiers My first guess was that the view has to be made internal via the xaml code. So I added two lines of xaml: x:Name="View" x:FieldModifier="internal" But this did not

Javascript: why does jQuery do this: (function(){ …});, and how does it work?

纵然是瞬间 提交于 2019-12-08 06:52:50
问题 EDIT: I THOUGHT The jQuery source I was looking at did something like this: (function(){ var bunchOfVariables = 7; jQuery = " ....."; //.... }); I was wrong about that. Ignore this question. I don't understand what that does. Can someone please explain it? This is the very first line in jQuery-1.3.2.js. It appears to define an anonymous function, and NOT execute it. Where does the function go? How does it get run? If I use code like that in a test script, it never gets called. On the other

Composite pattern in PHP, how to design classes to work around the need to extend two classes

旧巷老猫 提交于 2019-12-08 04:43:13
问题 I'm using the composite pattern to have reusable elements to build a page. for this i have a simple interface which drives the pattern interface Ai1ec_Renderable { /** * This is the main function, it just renders the method for the element, * taking care of childrens ( if any ) */ public function render(); } Since only some of the elements will be allowed to have children, i have created an abstract class that adds that behaviour abstract class Ai1ec_Can_Have_Children { /** * * @var array */

MVC3 - Where is UserId and Roles information

我的梦境 提交于 2019-12-08 00:23:17
问题 This is the Model code generated in MVC3 for Account/Membership class for a sample project I created. I do not see UserId field/attribute or Roles information.. Where is the UserId and Roles information ? public class ChangePasswordModel { [Required] [DataType(DataType.Password)] [Display(Name = "Current password")] public string OldPassword { get; set; } [Required] [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] [DataType(DataType

Python: How should I make instance variables available?

房东的猫 提交于 2019-12-07 08:44:23
问题 Suppose I have: class myclass: def __init__(self): self.foo = "bar" where the value of foo needs to be available to users of myclass. Is it OK to just read the value of foo directly from an instance of myclass? Should I add a get_foo method to myclass or perhaps add a foo property? What's the best practice here? 回答1: The applicable Python maxim would be "we're all adults here" - if users need direct access to the value of foo , let them access it directly. A getter or property would make

Objective-C partial implementation of classes in separate files

让人想犯罪 __ 提交于 2019-12-07 02:44:16
问题 I am using core data and am generating classes from my data model. I implement custom methods in these classes, however when i regenerate i generate over the top so i end up copying and pasting a bit. What i would like to do is split my implementation files ('.m') so i can have one header file with multiple '.m' files. then i can keep my custom methods in one and not have to worry about erasing them when i regenerate. I use this technique in .NET a lot with its partial keyword. Is there

Class design suggestions: extending a class and code reuse

萝らか妹 提交于 2019-12-07 02:32:09
问题 The gist of this question is about extending a class, minimizing jam-packing everything into a single class, and maximizing code re-use. After reading this question, please feel free to edit the title or description to make it more succinct. Though the post looks long, I am just trying to be thorough by using a lot of examples. Suppose I have a class: class UsedByManyPeople { // ...has many fields }; As the name implies, this class is used by a lot of developers. I have to add 2 features to