null-object-pattern

Get a dummy slf4j logger?

南楼画角 提交于 2021-02-07 12:27:25
问题 Can I get a dummy logger from slf4j? (Think the null object design pattern.) If so, can someone provide an example? Or will I have to implement a custom logger if I want to do that? I'm hoping to write a function along the lines of private Logger logger; static Logger nullLogger; static { nullLogger = getMeADummyLogger(); } public Logger getLogger() { return this.logger == null ? nullLogger : this.logger; } // then, elsewhere: this.getLogger().info("something just happened"); and not get a

How can i implement the NULL Object Design Pattern in a generic form?

岁酱吖の 提交于 2020-01-14 19:27:26
问题 Is there a way to implement the null object design pattern in a generic form so that i don't need to implement it for every buisness object. For me, there are two high level classes you'll need for every business class. One for a single record and another for a list. So i think there should be a way to implement the NULL Object design pattern at a high level and not have to implement it for every class. Is there a way and how please? 回答1: In my understanding, the NULL-class does not have to

Can null be inserted into Cache?

风流意气都作罢 提交于 2019-12-24 00:06:56
问题 I actively use caching in my ASP.NET web site. Though, some objects requested from db are really absent. For instance, I'm checking if there is a Discount for the particular product by looking record with ProductId=@ProductId in ProductsDiscount table. Absence of the record means no discount. Do you think it is a good idea to put null discount objects into Cache? Or I would better invent something better (using null-object pattern, for instance). In fact, I don't really like idea to start

Design pattern for default implementation with empty methods

二次信任 提交于 2019-12-12 07:30:21
问题 Is there a specific design pattern that describes the scenario where a non-abstract default implementation is provided that implements all or some of the methods on the interface with empty, NO-OP implementations. This being done with the intent of alleviating subclasses with the burden of implementing methods that they themselves may not need/use: public interface MyInterface { public void doThis(); public void doThat(); public void done(); } public class MyClass implements MyInterface {

How to implement Null Object pattern on data class in Kotlin?

守給你的承諾、 提交于 2019-12-11 17:31:36
问题 I have a Kotlin data class: data class PaymentAccount( val accountId: Int, val accountNumber: String, val title: String ) This is what I'd do in Java: Create an abstract class: public abstract class PaymentAccount { protected int accountId; protected String accountNumber; protected String title; public PaymentAccount(int accountId, String accountNumber, String title) { this.accountId = accountId; this.accountNumber = accountNumber; this.title = title; } } Create null object and extend

Generic null object pattern in C#

末鹿安然 提交于 2019-12-08 14:44:01
问题 I'm wondering if there is any approach to implement generic null object pattern in C#. The generic null object is the subclass of all the reference types, just like Nothing in Scala. It seems like public class Nothing<T> : T where T : class But it can't compile and I've no idea how to implement the methods of T to provide default behavior or throw an exception . Here are some thinkings: Use reflection? Use expression tree when creating Nothing<T> ? It maybe looks like Moq. And another

Ruby nil-like object

岁酱吖の 提交于 2019-12-06 18:44:58
问题 How can I create an Object in ruby that will be evaluated to false in logical expressions similar to nil? My intention is to enable nested calls on other Objects where somewhere half way down the chain a value would normally be nil , but allow all the calls to continue - returning my nil-like object instead of nil itself. The object will return itself in response to any received messages that it does not know how to handle and I anticipate that I will need to implement some override methods

Is it feasible to create a NullObject for every class? ( with a tool of course )

余生颓废 提交于 2019-12-06 04:13:13
问题 The NullObjectPattern is intended to be a "safe" ( neutral ) behavior. The idea is create an object that don't do anything ( but doesn't throw NullPointerException either ) For instance the class defined as: class Employee { private String name; private int age; public String getName(){ return name; } public int getAge() { return age; } } Would cause a NullPointerException in this code: class Other { Employee oscar; String theName = oscar.getName(); // NPE } What the NOP says, is you can have

Null object design pattern question

老子叫甜甜 提交于 2019-12-05 00:35:39
问题 I recently watched this youtube tutorial on the Null Object design pattern. Even though there were some errors in it: such as the NullCar that doesn't do anything creates an infinite loop, the concept was well explained. My question is, what do you do when the objects that can be null have getters, and are used in your code? How do you know which value to return by default? Or should I implement this pattern inside all the objects? What if I need to return strings or primitives? I'm talking

Ruby nil-like object

一个人想着一个人 提交于 2019-12-05 00:34:11
How can I create an Object in ruby that will be evaluated to false in logical expressions similar to nil? My intention is to enable nested calls on other Objects where somewhere half way down the chain a value would normally be nil , but allow all the calls to continue - returning my nil-like object instead of nil itself. The object will return itself in response to any received messages that it does not know how to handle and I anticipate that I will need to implement some override methods such as nil? . For example: fizz.buzz.foo.bar If the buzz property of fizz was not available I would