New answers tagged

1

In general visitor patterns in other languages are manage with tables, maps or any other struct/object type, here is the improvement that you can make on your code class TaxVisitor(CostVisitor): def cost(self,node): if isinstance(node, Milk): return 150 if isinstance(node, Sugar): return 100 if isinstance(...


5

I will have to agree that you do have a bit much synchronized blocks. All of them could go away if you would use for example Collections.synchronizedList around your ArrayList or CopyOnWriteArrayList. Your fun statistics() could be an alternative constructor to Statistics A few other Kotlin-related tips: var max = if (count > 0) liveTransactions.first()...


4

Disclaimer... I have very little experience with Kotlin. Not really the focus of your question, but synchronized blocks should be only as big as they need to be. Looking at your statistics code, you put the whole thing in the block.: synchronized(transactions) { val threshold = getCurrentThreshold() val liveTransactions = transactions....


3

There are some issues with your class, but I don't think that they are so specific to implementing a strategy pattern. addToPage() { this.statement = document.createElement("p") this.statement.innerHTML=this.flying.fly() document.body.append(this.statement) } This assumes there is a global document variable, and that statement is part of the ...


2

Your code does effectively nothing but add a useless layer of indirection. There is functionally nothing different between a simple std::queue<std::function<void()>> queue; and your OOP for the sake of OOP hierarchy and queue of unique_ptrs to pure interface.


0

You need to simplify your class to its standard form, which is House needs Name and Type, then go from there. Take it this way, if you need to create a new house, then you can write pseudo code that want something like : var house = new House { Name = "House Name", Type = new WoodenHouse { WoodType = "Pine", WoodAge = 100, ...


3

Currently your factories instantiate the new objects and then fill in all of their properties with the right values. You could split instantiation from property value assignment. Your StoneHouseCreator could instantiate a StoneHouse, use a HousePopulator that populates the values that all objects of type House have in common, and then the StoneHouseCreator ...


1

I have done some revisions. Thanks Book.java (New version) Converted a concrete class. No more keep "readers" information in book class. package oopdesign.onlineBookReaderSystem; public class Book { private long bookId; private String name; private String category; private String author; ...


3

Naming conventions package OOPDesign.onlineBookReaderSystem; Java has well established naming conventions. Package names should be all lower case. This should thus be oopdesign.onlinebookreadersystem. Single responsibility public abstract class Book { ... ArrayList<User> readers = new ArrayList<>(); A book is a data object. It should ...


5

Some basics... throws public int startReading(Book book) throws InterruptedException This exception is never dealt with in main (it's not caught or declared). It also doesn't look like it's thrown anywhere. Don't add unnecessary throws. unused private Date subsciptionDate; This is never used. Unnecessary code creates confusion by making the relevant ...


1

Prefer static const variables over #defines Don't #define constants if you could just as well declare them as regular static const variables. One advantage of this is that you can put those variables inside a namespace, whereas macros are always globally visible. Avoid giving new names to existing types Don't make a new type Byte when it's just the same ...


1

If this was a school exercise It's a basic command pattern without any safety checks against programming errors. :) It would be a good idea to check that a word can not be removed from an empty list. If this was a job interview assignment While the code shows that you know how to code a command pattern it doesn't tell me if you understand why one would ...


2

Your solution just doesn't work Your validation check always fails. Yes int(value) != int is always true. I don't think an instance of a class will ever be the class that it's an instance of. Your mutating a class variable. The tool you're utilizing forces you to take instances, normally there's a pretty good reason. If we remove the previous error and run ...


1

First of all, as per PEP8 I'd recommend you follow the naming conventions. A couple of examples: updateStates -> update_states defaultValue -> default_value I think you were headed in the right direction, but instead of overriding the __get__ and __set__ methods, I would just override the __add__ method, which would allow you to reuse the updateStates ...


2

Do you necessarily want to have one Event-Manager only ? If you would have EventManager<t>, instead of 3 generic methods, you would have one Eventmanager by Type. It's already resolved at compile time, which dictionary to access. Today, Trigger<int> and Trigger<string> put their things in the same dictionary, which you need to subdivide ...


3

Generic is your call, as using generic would avoid casting objects. Would this version work with you ? : internal class EventManager<T> { private readonly Dictionary<int, Action<T>> events = new Dictionary<int, Action<T>>(); public virtual void Subscribe(int incidentId, Action<T> action) { if (...


3

I don't see any glaring issues with the generic EventManager. Yes Dictionary of Dictionary does happen and is sometimes needed. One thing to consider is this implementation violates the Liskov substitution principle of SOLID. Usually in a non generic version the code would take the object and do "as" their type and check if not null. While the that ...


2

I have modified yours, (not tested), but I want to share it just to give you some insights, not the best design, but I felt I need to share it. //singleton class public sealed class HttpRequestClient { internal enum MethodType { GET, POST, PUT, DELETE } internal enum RequestContentType { JSON, XML, x_www_form_urlencoded } ...


4

Good implementation. Few minor bits: Mark _httpWebRequest as readonly as you're not assigning (nor want anyone to assign) to it outside of the constructor. Extract an interface (IHttpRequestBuilder) and have each method return that rather than the concrete class. This will allow for additional implementations or mocking for unit testing other parts of your ...


Top 50 recent answers are included