design-patterns

Name of Design Pattern: get class from class level

谁说胖子不能爱 提交于 2020-05-12 20:32:09
问题 Especially in unittests we use this "design pattern" I call "get class from class level" framworktest.py: class FrameWorkHttpClient(object): .... class FrameWorkTestCase(unittest.TestCase): # Subclass can control the class which gets used in get_response() HttpClient=FrameWorkHttpClient def get_response(self, url): client=self.HttpClient() return client.get(url) mytest.py: class MyHttpClient(FrameWorkHttpClient): .... class MyTestCase(FrameWorkTestCase): HttpClient=MyHttpClient def test

Name of Design Pattern: get class from class level

 ̄綄美尐妖づ 提交于 2020-05-12 20:31:29
问题 Especially in unittests we use this "design pattern" I call "get class from class level" framworktest.py: class FrameWorkHttpClient(object): .... class FrameWorkTestCase(unittest.TestCase): # Subclass can control the class which gets used in get_response() HttpClient=FrameWorkHttpClient def get_response(self, url): client=self.HttpClient() return client.get(url) mytest.py: class MyHttpClient(FrameWorkHttpClient): .... class MyTestCase(FrameWorkTestCase): HttpClient=MyHttpClient def test

Is this an incorrect warning?

喜欢而已 提交于 2020-05-12 02:29:30
问题 Let's see this code pattern I'm seeing often: struct Foo { template <typename T> T* as1() { /* ... */ } template <typename T> T* as2(T*) { /* ... */ } }; The former method is to be used like this: SomeComplexTypeAndNotAuto * a = foo.as1<SomeComplexTypeAndNotAuto>(); While the latter is more convenient to use since you don't need to repeat the complex type: SomeComplexTypeAndNotAuto * a = foo.as2(a); However, most compiler rejects the 2nd case with a Wuninitialized warning: warning: variable

Is this an incorrect warning?

别来无恙 提交于 2020-05-12 02:26:22
问题 Let's see this code pattern I'm seeing often: struct Foo { template <typename T> T* as1() { /* ... */ } template <typename T> T* as2(T*) { /* ... */ } }; The former method is to be used like this: SomeComplexTypeAndNotAuto * a = foo.as1<SomeComplexTypeAndNotAuto>(); While the latter is more convenient to use since you don't need to repeat the complex type: SomeComplexTypeAndNotAuto * a = foo.as2(a); However, most compiler rejects the 2nd case with a Wuninitialized warning: warning: variable

MVC design pattern, service layer purpose?

我的未来我决定 提交于 2020-05-09 18:05:29
问题 Let's say I have a following repo pattern : interface IGenericRepo<T> where T : class { IEnumerable<T> GetAll(); T GetById(object id); void Insert(T obj); void Update(T obj); void Delete(T obj); void Save(); } interface ICustRepo : IGenericRepo<Cust> { IEnumerable<Cust> GetBadCust(); IEnumerable<Cust> GetGoodCust(); } public class CustRepo : ICustRepo<Cust> { //implement method here } then in my controller : public class CustController { private ICustRepo _custRepo; public CustController

Is there buffered lock pattern?

自古美人都是妖i 提交于 2020-05-09 17:34:28
问题 In Go there is a concept of buffered channel. That is a channel that will not be blocked until you fill its buffer. Is there any general pattern for general buffered locking ? It will lock some resource for limited amount of clients. 回答1: The primitive that locks some resource for a limited amount of clients is called a semaphore. It's easily implemented with a buffered channel: var semaphore = make(chan struct{}, 4) // allow four concurrent users func f() { // Grab the lock. Blocks as long

Using JPA with multiple AND operations

不打扰是莪最后的温柔 提交于 2020-04-28 21:22:08
问题 I'm working on a Spring app and defining various find methods on a repository: @Repository public interface TicketRepository extends JpaRepository<TicketEntity, Long> { List<TicketEntity> findByTicketId(@Param("ticketId") Long ticketId); List<TicketEntity> findByTicketIdAndState(@Param("ticketId") Long ticketId, @Param("state") String state); List<TicketEntity> findByTicketIdAndStateAndFlagged(@Param("ticketId") Long ticketId, @Param("state") String state, @Param("flagged") String Flagged); }

Design pattern flexibility issue Factory Method

拈花ヽ惹草 提交于 2020-04-14 09:20:09
问题 I've been having a recent discussion with a colleague regarding the Factory Method design pattern. One basic approach is that the static method (from the factory class for example )should hide the complex creation logic of the created class: class IObject { //Interface }; class A :public IObject { }; class Factory { static IObject * create(int type) { //All logic goes here } }; The issue is that in our case the factory class will always only return an explicit object of type A. My honest

Java: Is it possible to always execute a certain function before other functions are called? (Like @Before in JUnit)

走远了吗. 提交于 2020-04-10 03:41:13
问题 Is there a way to always execute a function before any other function of a class is called? I have a class where I need to refresh some fields always before any function is called: public class Example { private int data; public void function1(){ } public void function2(){ } //@BeforeOtherFunction private void refresh(){ // refresh data } } Because it seems to be bad programming, I don't want to call refresh at the beginning of every other function. Since other persons are going to work on

Doesn't having more than 1 method break the Single Responsibility Principle?

亡梦爱人 提交于 2020-03-16 06:58:58
问题 I am quite confused with the Single Responsibility Principle. The Principle states that there should only be one reason for the class to change. The problem which I am facing is, any change to a method or any logic change in doing things would change the class. For example, consider the following class: class Person{ public void eat(){ }; public void walk(){ }; public void breathe(){ }; public void run(){ }; public void driveCar(Car car){ }; } Uncle Bob describes it as there should ONLY be a