Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

New answers tagged

2

What I haven't seen mentioned in the answers above is the following. Using the builder pattern makes for cleaner code. Cleaner code Say you've created a library which exposes a POJO that's essential to the function of your library. The constructor of that POJO has one or two parameters which are essential. Now for a new feature that POJO needs another ...


1

but as you can see, there's no reason to use inheritence in this case at all, since we won't be accessing super You are right that there is no point in making SubTypeX inherit from BaseType, but this does not mean that you are on the wrong track altogether. Amongst others the GOF promoted using composition over inheritance (Favor Composition over ...


1

After the comment exchange, you mentioned that the core of your question is on the necessity of custom DTOs as opposed to sending the basic entity to your consumers. I think the most important for me that using custom DTOs is the way to go instead of giving the main entity to everyone. That is mostly a consideration you have to make yourself. You have to ...


0

UML is the Unified Modeling Language it supports a range of paradigms for programming not just OO with its Class diagrams. Use whichever diagrams add value to your documentation or when discussing the implementation at a whiteboard or in review. Structural diagrams Class diagram - You can treat each module as a class for diagramming purposes. Package ...


0

So you want to describe the functional implementation of the application with a diagram You can use a sequence diagram to depict the functional flow among your modules. This is a bit more at a component level. If you are talking about specific logic and a bit more detail go for a State diagram Cheers..


2

So why create a nested class? I can think of couple of important reasons: 1. Enable encapsulation Many times nested classes are implementation details of the class. Users of the main class should not have to care about their existence. You should be able to change them at will without requiring the users of the main class to change their code. 2. Avoid ...


0

Nested Class can be used whenever you want to create more than once instance of the class or whenever you want to make that type more available. Nested Class increases the encapsulations as well as it will lead to more readable and maintainable code.


0

I think you could move all behaviour which are depend on context (where the link is rendered) into the separate 'strategies'. Link provides public API (e.g. add/remove/change icon) which strategy could use in order to change the link. In terms of Angular it might be @Decorator which in constructor takes the Link instance and call link public methods. In ...


0

I use public nested classes for related helper classes. public class MyRecord { // stuff public class Comparer : IComparer<MyRecord> { } public class EqualsComparer : IEqualsComparer<MyRecord> { } } MyRecord[] array; Arrays.sort(array, new MyRecord.Comparer()); Use them for related variations. // Class that may or may not ...


0

The reason to avoid using new, is to create a seam, which allows you to 'unstitch' the program and change its behaviour without editing that part of the code. So to decide whether to avoid using 'new', you need to think about whether you're likely to want to run the code you're writing in multiple different ways, either for different types of use, or for ...


0

A version entirely without static; concurrency safe. public enum Singleton { INSTANCE; public final AtomicLong counter; Singleton() { counter = new AtomicLong(System.currentMillis()); } /** * For unit testing. * @param value. */ void setCounter(long value) { counter.set(value); } } And ...


8

The programming paradigm that organizes the software into functions is called procedural programming. For example, C is a procedural programming language. Most dynamic languages like JS or Python can be used in a procedural style. While you are also organizing your code into separate files, there is no commonly used name that describes this. For example, ...


-2

Make the parameter strongly typed, and make the type responsible for performing the conversions. An example of this pattern is the C# TimeSpan class. Instances can be obtained from various static methods with self-documenting names like FromMinutes. And through properties like TotalMinutes, the value of an instance can be expressed in various units. ...


2

Well, you wrote to take care of converting arguments to public methods to some unit system internal to the package so that private (or hidden, since we're talking about Python) methods don't have to "think" about units. so for yourself, you already see the major drawback of the "arbitrary unit" approach: it is way less tedious any less error prone to ...


0

Yes the design with those globals is bad because it is hard to change them or make them into variables without breaking lots of other places in your code(tight coupling). The point is that the code is not flexible to change and when working in the real world, change is common! It is software, we want to be able to change it easily. Here is some design ...


1

When you instantiate the object CreditCardApplicationEvaluator, it needs to be ready to evaluate applications. We must think of the failure case where you can't pull this data from the database. So if you put the db call for those constants in the constructor, it might never return, so you will never instantiate your object or it will fail. Also another ...


7

Constructors should not do work. The initialization of a new object should happen very quickly, and making database calls, or interacting with any resource outside of the current process, can take considerably longer. Instead, your constructor should either require the data from the database as separate arguments, so that the database calls are made before ...


0

I can see many solution but at first you should separate models. One for the client side and another for the server (and some mapping). Then you can for example have a flag new used for not-saved data. Another option like this written here (hash or Id).


0

You can impose validation in every layer of your application. Where to impose which rules depend on your application. For example, entities implement methods that represent business rules while use-cases implement rules specific to your application. If your building an e-mailservice like Gmail one could argue that the rule "users should have a unique e-...


1

What your doing is at this point not object-oriented at all. Don't worry, this is not a value judgement, you don't have to be object-oriented if you don't want to. If you do however, you still have work to do. First, you are data-modeling. Having properties and relations, like you sometimes see in UML diagrams is a way of describing data not objects. You ...


3

The CompanyAddress and PersonAddress are good but not on the class level and certainly not as inheriting entities (no is-a relationship). They should be (link) tables keeping relations between companies and addresses and between persons and addresses respectively. Then you can do anything. They do not represent anything in the real world, you probably do not ...


4

It's actually hard to say whether your object-oriented design is good or bad: At the moment all you have is some pure data objects (only setters and getters) - see Anemic Domain Model. In short: Good OOP design is when your classes do have non-trivial behavior, which helps you to deal with the cases at hand. Please also note, that Company and Person are ...


2

Company has an address too. So in class Address I would have both relations to Person and Company. Addresses should not refer to a Company or Person (but rather vice versa: a Company or Person has one primary (mailing/billing) address). What will happen to your data model if two companies or two people share an address? You won't be able to express it, so ...


0

There are two conditions for your logger (and for all singletons): 1. The singleton should be callable at any time. 2. The singleton should be callable at any time, from any thread. (1) means that you cannot expect to set the filename before the singleton gets called the first time. You can set it of course while the singleton is being created, from the ...


0

Problem with BaseClassParams Using struct BaseClassParams { struct DerivedClass_A_Params { }; struct DerivedClass_B_Params { }; }; to capture the data needed to construct derived clsses is DOA in my book. As soon as you need DerivedClass_C, you'll have to go back to the base class and update it to struct BaseClassParams { ...


-1

The right time to set the file name is any time before the first call to the Log method. Just throw an exception from there if the object has not been initialized yet.


-1

You can change the properties of the singleton object this way. singleton.js export var SingletonTester = (function () { // our instance holder var instance; // an emulation of static variables and methods var _static = { name: "SingletonTester", getInstance: function() { if( instance === undefined ) { ...


6

TL;DR You're violating the Open/Closed Principle by writing your code in a way that requires constant updating when classes are added to the assembly. You're also violating LSP by expecting that a base class can do more than its definition defines. Note: The link refers to LSP, but the top answer show a workaround which in turn violates OCP, and that is ...


0

It's completely fine to have one TwitterClient per user. It seems like the program memory would dramatically increase if every user initiated the instantiation of a program object? Why, what instance data does the client object actually have? I believe it would only have the user's authentication data; the rest of it is just methods. If for whatever ...


0

Inheritance is not bad per se, however it is often misunderstood. I'm quoting some relevant parts of your question: Before trying to apply composition over inheritance, each different page would be a subclass of the base Page object, and would override getData and getTitle functions. ... This behavior is not easily refactored to config data. This is ...


0

Creating a base Page class is a good idea. A "page" is a well known abstraction. The problem is the getData method. The data for any given page is going to be unique, so using a loosily typed language, or a strongly typed language that supports generics or class templates would be the right approach. The data providers should be constructor arguments passed ...


2

As far as I remember the principle states prefer composition over inheritance. Not "never use inheritance". Furthermore I think the original motivation was that pieces of duplicated code should be extracted into helper classes not in some sort of base class which in such case would be a permature abstraction. On the contrary, in your case Page is example of ...


1

This is a classic scenario where it helps to separate the client API from the service provider interface (SPI). Your SPI should be an interface that is easy for providers to implement, so it will likely have just one method that sends an email: public interface EmailServiceSpi { void sendEmail(String recipient, EmailContext context); } Your client API ...


1

If it was me, along with Alex answer here, I would have segregated interface into more granular level so that ISP (Interface Segregation Principle) is not violated. public interface EmailFactory { Email createInvitation(Payload payload); Email createNotification(Payload payload); Email createPasswordReset(User user); Email createValidator(...


0

I have to disagree with most responders here and claim that template design pattern is not a great solution due to inheritance. From Wikipedia: the template method pattern is a behavioral design pattern that defines the program skeleton of an algorithm in an operation, deferring some steps to subclasses. Key here is behavioral and deferring. This means ...


0

Your approach does not address spelling issues during development. Nor do you get any type hinting for your IDE. There is however a doc annotation that comes in handy: /** * @property-read string $name * @property-read string $email * @property-read string $username */ class User { public $name; public $email; public $username; With this ...


Top 50 recent answers are included