Questions tagged [design-patterns]

15124 questions
0

votes
0

answer
3

Views

Is it a good idea to storage validations in DAO?

I have web application. The structure: controller which take an entity and DAO which save the entity and making some other actions. Is this a good idea to check stateless validations (size of name for example) in controller and sate validations (duplicate email in db for example) in DAO. Is there go...
faoxis
1

votes
2

answer
67

Views

Object oriented design. What better? [closed]

I have a Printer class that should print a number and text. The number never changes for each Client class. I have more Client objects with different number values. What design is better? In the sample1 the number sends to print() method as argument, therefore all Client objects use single Print obj...
1

votes
1

answer
43

Views

Allow user to select implementation of code to use

I am starting a new project were we need to do some calculations based on some formulas, those formulas are valid for a limited interval for example year: 2017 to Year:2018 each couple of years they can change. However we need to allow the user to select the 'set' of formulas to use for example the...
forcewill
1

votes
2

answer
115

Views

Which design pattern to use strategy or factory design?

I received a homework assignment. I am not able to decide which design pattern to use. I have currently implemented strategy pattern. But some of my friends are implementing factory design pattern. I just need validation for the same. This is the problem: Suppose you are working at a new startup whe...
Soumil Kanwal
1

votes
2

answer
48

Views

Why use Adapter design pattern

So I am looking at the Adapter design pattern. I see that the intention is to allow a client to access the methods of a class where their interfaces are incompatible. Now I have been looking at this example. interface ITarget { List GetProducts(); } public class VendorAdaptee { public List GetListOf...
Stackedup
1

votes
1

answer
49

Views

Stackable trait/decorator and abstract class

I have an abstract class (Java library) that takes constructor arguments and has a method called execute that I want to decorate: public abstract class Task { private final String name; protected Task(String name) { this.name = name; } public abstract void execute(String str) throws Exception } And...
Alexey Sirenko
1

votes
2

answer
49

Views

Java's “Scanner” Method vs. Facade GoF Design Pattern

I'm studying design patterns to improve my programming skills. Right now, I'm exploring the facade design pattern. I may be confusing myself, but, as an example: isn't the Scanner is a facade? Note that I'm not asking what is a Facade, but trying to identify if Scanner is. Well, I declare it so I ca...
Lodi
1

votes
4

answer
64

Views

Why State Design Pattern over If-else

Everywhere I find people saying it is better to use state design pattern over if else. I want to know why: It is an improvement to if-else in real world use case? Does it make code more testable? can any one please provide an example where state pattern actually improves the case, it is difficult to...
Nikhil Kamani
0

votes
0

answer
6

Views

How to manage multiple clients website having same code structure but different UI?

I want to create websites for many colleges using MVC. Since coding structure will be common for all the client websites but the website templates will be different I need suggestions on handling this. Do I need to create a core framework for this and use the same for other website projects? If I do...
Karthik
1

votes
1

answer
1.5k

Views

Creating reusable widgets with javascript

I'm working on creating small, embeddable widgets which have a handle outside of their own JS files. Page Structure For example, I have a page which may contain n-number of 'power-widget's in it. 'n' is determined by the web server serving the page. A 'power-widget' is nothing but a div with the n...
vinayakshukl
-1

votes
0

answer
18

Views

Poker game loop , what is the appropriate approach?

I am trying to create a test environment for my algorithm(Poker AI and using Java). State transitions really boggles my mind. This is not something like 'init()-update()-draw()-repeat' cycle , or i can't simplify like that.Basically my pseudo code will be something like this: Create players deck etc...
RSDv
1

votes
1

answer
63

Views

Abstract builder constructing immutable object

I have following code implementing abstract builder (as in effective java book): interface I { I ret(); } abstract class A implements I { private String s = ''; A(Builder b) { s = b.s; } @Override public I ret() { String s = 'some new string from calc.'; //HERE I NEED TO CONSTRUCT class B //THIS IS...
Bojan Vukasovic
1

votes
1

answer
71

Views

Achieve Open-Closed Principle (SOLID) Using Interface

I have multiple types of payment options (Stripe, Paypal, PayUMoney etc.). I want to create seperate class for each payment type and an Payment Interface to be implemented by those classes like this, interface PaymentInterface { public function payment($params); } class Stripe implements PaymentInte...
Muhaimenul Islam
1

votes
2

answer
31

Views

Replacing null checks using ploymorphism

Recently I was reading through the book 'clean code' by Robert Martin, and in chapter 7 there was a section where he said you should try to replace null checks using the special case design pattern. I thought this was a great idea but then I thought about the following instance. foreach(thing in so...
James
1

votes
1

answer
50

Views

DDD Microservices

I've been researching DDD pattern for a few weeks ago and I got no answer for a question. Following Eric Evans principles, the Domain module should have no dependencies with other modules, packages or anything. And here should be included all the models such as errors, entities, interfaces... My que...
Cristian Batista
1

votes
2

answer
112

Views

Design pattern to enrich a class with new features [closed]

I have implemented a series of classes to manage search / detail page with JSF and PrimeFaces. More in detail, I've created an abstract class SearchDetailView in order to centralize common functionality for Search/Detail page. In short, I've a class MyView that extends the base SearchDetailView. Now...
Alessandro
1

votes
2

answer
57

Views

Custom visitor pattern implementation

I'm trying to implement a sort of visitor pattern. Most of examples on web show a visitor class with a 'visit' method and multiple overloads of that method. In this case i've called my 'visit' method CalculateFee (it's a semantic matter) with its overloads. Everything it's ok so far, but now i need...
FredE
1

votes
2

answer
54

Views

Functional Programming: How to handle complex data without bloated functions?

Lets say in your program you have defined a complex car object. That object holds a very long list of predefined key value pairs (wheels,engine,color, lights, amountDoors etc.), each being either a part number or a list of part number, or a specific value. //** PSEUDO CODE: var inputCar = { 'engine...
Marcus Blättermann
1

votes
1

answer
80

Views

What design pattern is suitable for handing many conditional branching

I have a a bunch of rows in a table which store some variables and associated values. I have to fire a task depending the values of the variables from different rows. Multiple values from multiple rows can be overlapped by AND/OR conditions. What design pattern will be helpful to reduce cyclomatic c...
1

votes
3

answer
39

Views

In Java, How to implement the functions similar to using the template parameter to avoid creating Creator subclasses in C++?

Recently, when I was reading the book named 'Design Pattern-Elements of Reusable Object-Oriented Software', there was such a paragraph in the book: Using templates to avoid subclassing. As we've mentioned, another potential problem with factory methods is that they might force you to subclass just t...
xun yan
9

votes
3

answer
881

Views

Why do we need to use the builder design pattern when we can do the same thing with setters? [duplicate]

This question already has an answer here: When would you use the Builder Pattern? [closed] 15 answers public class Employee { private String name; private String address; private int id; public Employee() { // TODO Auto-generated constructor stub } @Override public String toString() { return 'Emplo...
Priyanka Taneja
3

votes
1

answer
105

Views

How to manage unicity in my buisness logic layer?

In a aspnetcore mvc executing context . I have this simple entity. public class Foo { public int Id { get; private set; } public string Name{ get; private set; } public string Code { get; private set; } private Foo() { } public Foo(string Name, string Code) { GuardClauses.IsNullOrWhiteSpace(Name,nam...
Christophe Debove
1

votes
0

answer
29

Views

What is the need of passing additional arguments to browserify's module constructor?

I am just checking how browserify creates a bundle code. prelude.js contains the IIFE that is executed with arguments of the format: ({ id_one: [function(require, module, exports) { // Module Code }, { dependency_one: id_two, dependency_two: id_three }], }, {}, [entryid]) However, I couldn't underst...
Nishant
1

votes
0

answer
41

Views

About AbstractDocumentPattern

Hi I'm junior Java developer and I am studying design pattern. In AbstractDocumentPattern, Document interface has 3 methods like below. public interface Document { Void put(String key, Object value); Object get(String key); Stream children(String key, Function constructor); } My ques...
Hacking J
1

votes
1

answer
1.1k

Views

Passing props to higher-order components

Lets say if we have the following HOC: const myHoc = someProps => ComposedComponent => { const propsToPass = { /* ...some calculations... */ }; return ; }; How would I pass a prop to ComposedComponent in myHoc outside of myHoc? Use case I am writing an app used with redux-orm. Naturally, I'd want t...
Liau Jian Jie
1

votes
0

answer
58

Views

Is it good design to loop through many users? - SpringBoot

I am looking into good design principles and would just like some feedback. I want to go through every user in my application that is a patient, check their scheduled activities, see if there is one upcoming and send them an email. It looks a little like this: List listOfAllPatients = patientReposi...
jackabe
1

votes
0

answer
75

Views

Can I name this example as an Adapter design Pattern? If 'No', Is any other design Pattern I can name for the same scenario?

Please help me in this. I created an interface with m1,m2 methods. Let say I want flexibility for the class who wish to override these methods, Let say, either they want to override m1, m2, both or none. So My objective is to find out name of respective design pattern. Can this example be a name of...
Lokesh Kumar
1

votes
0

answer
31

Views

Repository pattern, different identifiers

TLDR; I have one repo interface and multiple data sources, each with a different data identifier - how can I maintain having only one method in my interface? I have a need to read a domain object OrderData from two different repositories: one is an outside service and the other one is a local databa...
Robotron
1

votes
1

answer
57

Views

Is it better to wrap chain of responsibility functionality than have it directly in a class?

I have been focusing on learning programming principles and patterns but the chain of responsibility examples I have found all seem to contradict other principles/patterns. The placement of sethandler and nexthandler directly in a class that will do more than just that seems like a really really bad...
Dusky Pixel
1

votes
0

answer
415

Views

jQuery-Mask-Plugin validate date by regex pattern

So i'm trying to use jQuery-Mask-Plugin but the documentation its not very clear to me regarding validation. Meaning i'm trying to use the basic setup for a date input. $('.mydateinput').mask('00-00-0000'); This works fine but with this i can put a a date like so 99-99-9999 and i would like to restr...
Henrique C.
1

votes
0

answer
58

Views

The Object-Serialization-Issue: Dealing with Inheritance while Mapping Objects

I am struggeling with the following Problem which is pretty common and I am not sure if properly solved by existing frameworks. I would call the problem object-mapping problem or object-serialization problem. I am writing a Java program where I have an abstract object wich has many implemenations. T...
Christof Tinnes
1

votes
4

answer
59

Views

writing a custom get method for a scala map

I have a map which is something like val m = Map('foo' -> 'bar', 'faz' -> 'baz') I need to write a custom get method, so that the key can be the key in the map with a number in the end. So for example: m.get('foo1') should return 'bar' I am looking for a good scala pattern to solve this problem. Al...
Vinit Sacheti
1

votes
1

answer
365

Views

static factory methods inside interface

This is a design question regarding using static factory methods inside of static interface methods. I am looking for improvements or disadvantages of using this pattern. public interface SampleService { // 1) do work void doWork(); static SampleService create(final String service) { // 2) Dispatch...
Greg
1

votes
0

answer
24

Views

Best way to pass data into plugin objects

I often have the problem that I have different plugins that all essentially perform the same task, but all in a different way. So each plugin needs different kinds of parameters, but how to pass them? IMySpecificTask plugin = PluginResolver.Resolve(typeof(IMySpecificTask)); Each implementation of IM...
codymanix
1

votes
1

answer
27

Views

How to model data for similar requests?

I have an issue, I'm not sure how should I model the data from the backend when I have some really similar views on the front, particularly some forms. For example in one of them the user sends an id for a seller and a product code, but in another one the inputs ask for an id seller, a product code,...
Alex D.
1

votes
1

answer
181

Views

Construction of command object with optional parameters in CQRS

My Command object contains a combination of required and optional information used to construct my domain object. class Command { /* | Required stuff */ private $req1; private $req2; /* | Optional stuff */ private $opt1; private $opt2; private $opt4; private function __construct($req1, $req1, $opt1,...
Unflux
1

votes
0

answer
184

Views

Spring Project not creating bean

I'm trying to develop a Spring MVC project but after running it on tomcat I'm getting 404 after hitting: http://localhost:8080/magicart-service/product I injected static code in Controller bean to see if its getting created but I don't see any output in the console. Here're all my classes: AppConfig...
Uttiya Ghosh
1

votes
0

answer
27

Views

Is assigning a bound function to a var really the only option?

Given the Object below, is this.delegateBind = this.delegate.bind(this) my only option when dealing with event listeners and this? I've read other related questions that suggest this is the proper pattern, but it seems inelegant... Is there a more succinct pattern I'm missing? const foo = { init:...
calipoop
1

votes
0

answer
33

Views

Descriptor pattern : trouble with property and instance attribute

I have implemented the following descriptor class. I want to follow Player instances attribute credit. #! /usr/bin/env python3 # -*- coding: utf-8 -*- class SignalDescriptor(object): subscriptions = {} @classmethod def warn_me(cls, obj, attr, callback): cls.subscriptions.setdefault(obj, {}).setdefa...
buffalo974
1

votes
1

answer
33

Views

What pattern or principle to use, in order to simplify generation of content that is based on lot of parameters?

In a situation, where you need to generate a bunch of , and tag contents, based on many query search parameters for thousands of pages. What principle or pattern is best fit in a situation like that? For example: You will get some array with lot's of query parameters, like the type of some object,...
Igor

View additional questions