design-patterns

Pattern/Strategy for creating BOs from DTOs

时间秒杀一切 提交于 2020-01-14 14:53:27
问题 I like the approach of having property bag objects (DTOs) which define the interface to my server, but I don't like writing code like this: void ModifyDataSomeWay(WibbleDTO wibbleDTO) { WibbleBOWithMethods wibbleBO = new WibbleBOWithMethods(); wibbleBO.Val1 = wibbleDTO.Val1; wibbleBO.Val2 = wibbleDTO.Val2; } This copying code is quite laborious to write. If the copying code is unavoidable, then where do you put it? In the BO? In a factory? If it is possible to manually avoid writing the

Object Pool Pattern in Java

假如想象 提交于 2020-01-14 14:47:54
问题 So I've implemented my own Object Pool Pattern and it works just fine and as expected. Returning my "Teacher" objects from a list and creating them when there aren't any. My question: The object being returned "Teacher" then needs to be casted into one of its sub classes which is specialised e.g. "Biology-Teacher". What is the best way to get this kind of functionality? Edit: Sorry, I didn't think code was needed but here goes. Below is the casting I was talking about. This throws a run-time

Can I define/constrain a member as implementing two interfaces, without generics?

こ雲淡風輕ζ 提交于 2020-01-14 10:37:08
问题 The following code shows what I would like to do; that is, I would like to constrain anObject, so that it can be used as a parameter to various methods with use IInterfaceOne or IInterfaceTwo, where neither inherits from the other. public interface IInterfaceOne { } public interface IInterfaceTwo { } public class Implementation : IInterfaceOne, IInterfaceTwo { } public interface IInterfaceOneAndTwo : IInterfaceOne, IInterfaceTwo { } public class UsingImplementation { IInterfaceOneAndTwo

What is the best strategy for Dependency Injection of User Input?

别来无恙 提交于 2020-01-14 10:32:32
问题 I've used a fair amount of dependency injection, but I'd like to get input on how to handle information from the user at runtime. I have a class that connects to a com port. I allow the user to select the com port number. Right now, I have that com port parameter as a constructor argument. The reasoning being that the class cannot function without that information, and it's implementation specific (a mock version of this class wouldn't need a com port). The alternative is to have a "Start"

MouseAdapter: which pattern does it use?

ぃ、小莉子 提交于 2020-01-14 09:54:07
问题 I've been able to find great resources that tell me that MouseAdapter from the Java API doesn't make use of the Adapter pattern. The question is: is there a pattern that MouseAdapter does implement? I know what it does: it makes a concrete class for the MouseListener interface so you can just extend the class to avoid implementing unnecessary patterns. I was thinking it may be part of the Bridge pattern. I'm not sure though, as I'm not familiar with this pattern. 回答1: Great question! I can

Best practice to hide a service implementation

非 Y 不嫁゛ 提交于 2020-01-14 09:44:47
问题 I want to hide the implementation (concrete class) of a service (interface) from the API user. The implementation is provided to the user by a factory which uses the Java API ServiceLoader mechanism. This Loader requires the implementation class to have public visibility. This is OK as long as the implementation is hidden in a different JAR (apart from the API JAR) the user is not directly depending on. However, for easy distribution the content of the JAR with the default implementation is

ServiceLocator and the Open/Closed Principle

痞子三分冷 提交于 2020-01-14 09:38:27
问题 I'd like to: Make commonly required services visible to all classes that need them, with a minimum of boilerplate, and without sacrificing testability! It's a small project and I think DI might be overkill, but maybe I'm wrong? Anyhow, I have been focusing on the ServiceLocator pattern as described by Martin Fowler In a client class' constructor, I have something like this: this->db = Locator::getDb(); this->log = Locator::getLogger(); Then the rest of the class' methods access the service

Java Swing GUI design techniques for user actions validations (MVC-FSM)

旧城冷巷雨未停 提交于 2020-01-14 09:38:10
问题 I have implemented GUIs using Swing and always tried to follow the MVC pattern as much as I could. I was wondering though, if MVC is not the only part of the puzzle. Are FSMs used in GUI's design as well? I have never used an FSM in a GUI and my approach was: on action, the corresponding method of the controller was called, to endup updating the model, finishing with updating the view. Usually upon a request triggered by a user's action I used some simple checks to make sure that the user

What type should Struts ActionForm properties be?

不羁的心 提交于 2020-01-14 08:53:06
问题 I inherited this gigantic legacy Java web app using Struts 1.2.4. I have a specific question regarding ActionForms. Some of them have only String properties (even for numbers), some of them use the seemingly appropriate types (Integer, Date, String, etc). What's the best practice here? Also, it seems that if a property is of type Integer, and the value the user entered is not an integer value, Struts silently swallows this and just doesn't set the property. What's up with this? 回答1: For

Design patterns for network protocols?

守給你的承諾、 提交于 2020-01-14 04:09:12
问题 So I'm building a fairly simple file transfer server/client but having a hard time to figure out a good design for processing different commands and states within the protocol.. Say that you have 3 different commands upload , remove , download , you could use 3 different if-statements and keep track of states with additional if-statements, but that wont scale and will be impossible to maintain.. The Chain Of Responsibility design pattern could be used for sequential stuff such as encryption