What is the difference between a private static function vs. a public static function in typescript? - class

In the context of an angular2 service; what is the difference between a private static function vs. a public static function in typescript?
public static getUserStockList(): Stock[] {
/* TODO: implement http call */
return WATCHLIST;
}
vs.
private static getUserStockList(): Stock[] {
/* TODO: implement http call */
return WATCHLIST;
}
EDIT:
When would it be appropriate to use a private static function over a private function?

Private static methods can be invoked from instances of the class.
An example of this:
interface Data {
// ...
}
interface StrictData {
// ...
abstract class MyClass {
protected constructor(data: StrictData) {
// ...
}
}
class AnotherClass extends MyClass {
private static normalizeData(data?: Data | StrictData): StrictData {
// ...
}
constructor(data?: Data | StrictData) {
super(AnotherClass.normalizeData(data));
}
}
AnotherClass.normalizeData is accessible from the instance and the compiler is just fine with this.
However, this:
console.log(AnotherClass.normalizeData({}));
Will result in:
Property 'normalizeData' is private and only accessible within class
'AnotherClass'

Related

Why does my sub-dependency not get set in Dagger?

I am having a hard time figuring out how to inject CachedRithms into my RithmioManager and CachedKamms into my KamilManager?
I have the following files:
AppScopeModule:
#Module
(
library = true,
complete = false,
injects = {
KamilApplication.class,
KamilManager.class
}
)
public class AppScopeModule {
/* package */ static Context sApplicationContext = null;
private final Context mApplicationContext;
AppScopeModule(Context applicationContext) {
KamilManager.initInstance(applicationContext);
mApplicationContext = applicationContext;
}
#Provides
#Singleton
KamilManager provideKamilManager() {
return KamilManager.getInstance();
}
}
KamilApplication:
public class KamilApplication extends Application implements Injector {
private ObjectGraph mObjectGraph;
#Inject
KamilManager KamilManager;
#Override
public void onCreate() {
super.onCreate();
AppScopeModule sharedAppModule = new AppScopeModule(this);
// bootstrap. So that it allows no-arg constructor in AppScopeModule
sharedAppModule.sApplicationContext = this.getApplicationContext();
List<Object> modules = new ArrayList<Object>();
modules.add(sharedAppModule);
modules.add(new AuthModule());
modules.addAll(getAppModules());
mObjectGraph = ObjectGraph.create(modules.toArray());
mObjectGraph.inject(this);
}
}
KamilManager
public class KamilManager {
#Inject
CachedKamms mCachedKamms;
private static KamilManager instance;
private boolean mWearIsConnectedToMobile;
private KamilManager() {
Log.d(TAG, "KamilManager private constructor");
}
public static void initInstance(Context appContext) {
if (instance == null) {
instance = new KamilManager();
.....doing more things here...
}
}
public static KamilManager getInstance() {
return instance;
}
}
But mCAchedKamms is always blank when I initialize the app. Any idea what I'm doing wrong?
You need to call ObjectGraph.inject(this) somewhere in KamilManager.
I suggest you to add this code to your KamilApplication class:
public ObjectGraph getObjectGraph() {
return mObjectGraph;
}
After that you need to somehow get instance of KamilApplication(pass it via constructor maybe?) in KamilManager and call:
kamilApplication.getObjectGraph.inject(this);
after this call every field in class KamilManager annotated with #Inject should be injected.
OR
Just annotate constructor of CachedKamms with #Inject
Extra:
Avoid of using library = true and complete = false unless you know what are you doing. With this settings you disable some validations at compile time.

Injecting a Factory that accepts a Parameter with AutoFac

I've read over several examples that were more complex then I needed and I'm having trouble distilling this down to a simple, concise pattern.
Let's say I have an interface names ICustomService and multiple implementations of ICustomService. I also have a class Consumer that needs to determine at run time which ICustomService to use based upon a parameter.
So I create a classes as follows:
public class Consumer
{
private CustomServiceFactory customServiceFactory;
public Consumer(CustomServiceFactory _customServiceFactory)
{
customServiceFactory = _customServiceFactory;
}
public void Execute(string parameter)
{
ICustomService Service = customServiceFactory.GetService(parameter);
Service.DoSomething();
}
}
public class CustomServiceFactory
{
private IComponentContext context;
public CustomServiceFactory(IComponentContext _context)
{
context = _context;
}
public ICustomService GetService(string p)
{
return context.Resolve<ICustomService>(p); // not correct
}
}
public class ServiceA : ICustomService
{
public void DoSomething()
{
}
}
public class ServiceB : ICustomService
{
public void DoSomething()
{
}
}
Is there an advantage to having my factory implement an interface? How do I fix my factory and register these classes with Autofac so that Consumer.Execute("A") calls DoSomething on WorkerA and Consumer.Execute("B") calls DoSomething on WorkerB?
Thank you
You would register your implementations of ICustomService with keys. For example:
builder.RegisterType<FooService>.Keyed<ICustomService>("someKey");
builder.RegisterType<BarService>.Keyed<ICustomService>("anotherKey");
and then your factory method would be:
public ICustomService GetService(string p)
{
return context.ResolveKeyed<ICustomService>(p);
}
But, you can take this a step further and decouple CustomServiceFactory from IComponentContext:
public class CustomServiceFactory
{
private Func<string, ICustomService> _create;
public CustomServiceFactory(Func<string, ICustomService> create)
{
_create = create;
}
public ICustomService GetService(string p)
{
return _create(p);
}
}
which you would register like so:
builder.Register(c => {
var ctx = c.Resolve<IComponentContext>();
return new CustomServiceFactory(key => ctx.ResolveKeyed<ICustomService>(key));
});
And at that point, assuming CustomServiceFactory doesn't have any other behavior that was omitted for the question, then you as might as well just use and register Func<string, ICustomService> directly.

MvvmCross: IoC with Decorator pattern, two implementations of the same interface

I'd like to implement the Decorator pattern in one of my Mvx projects. That is, I'd like to have two implementations of the same interface: one implementation that is available to all of the calling code, and another implementation that is injected into the first implementation.
public interface IExample
{
void DoStuff();
}
public class DecoratorImplementation : IExample
{
private IExample _innerExample;
public Implementation1(IExample innerExample)
{
_innerExample = innerExample;
}
public void DoStuff()
{
// Do other stuff...
_innerExample.DoStuff();
}
}
public class RegularImplementation : IExample
{
public void DoStuff()
{
// Do some stuff...
}
}
Is it possible to wire up the MvvmCross IoC container to register IExample with a DecoratorImplementation containing a RegularImplementation?
It depends.
If DecoratorImplementation is a Singleton, then you could do something like:
Mvx.RegisterSingleton<IExample>(new DecoratorImplementation(new RegularImplementation()));
Then calls to Mvx.Resolve<IExample>() will return the instance of DecoratorImplementation.
However, if you need a new instance, unfortunately the MvvmCross IoC Container doesn't support that. It would be nice if you could do something like:
Mvx.RegisterType<IExample>(() => new DecoratorImplementation(new RegularImplementation()));
Where you'd pass in a lambda expression to create a new instance, similar to StructureMap's ConstructedBy.
Anyway, you may need to create a Factory class to return an instance.
public interface IExampleFactory
{
IExample CreateExample();
}
public class ExampleFactory : IExampleFactory
{
public IExample CreateExample()
{
return new DecoratorImplementation(new RegularImplementation());
}
}
Mvx.RegisterSingleton<IExampleFactory>(new ExampleFactory());
public class SomeClass
{
private IExample _example;
public SomeClass(IExampleFactory factory)
{
_example = factory.CreateExample();
}
}

how to instantiate a java class from within JSNI

I'm trying to instantiate a java type inside a JSNI method body, using the JSO's package-private's ::new() method, but all instances are returned as null handles.
how come?
Is it possible to instantiate the class using a similar syntax to Java?
Here is the relevant code:
public class OnChangeHandlerJso extends JavaScriptObject {
protected OnChangeHandlerJso() {};
public static native JavaScriptObject create() /*-{
return function(cm, changeInfo) {
var eventBus = #com.gigaspaces.codemirror_gwt.client.events.EventUtils::CODE_MIRROR_EVENT_BUS;
var event = #com.gigaspaces.codemirror_gwt.client.events.EditorContentChangeEvent::new()();
// could something like this work?
// var event = new #com.gigaspaces.codemirror_gwt.client.events.EditorContentChangeEvent;
event.#com.gigaspaces.codemirror_gwt.client.events.EditorContentChangeEvent::setCm(Lcom/gigaspaces/codemirror_gwt/client/jsni/CodeMirrorJso;)(cm);
event.#com.gigaspaces.codemirror_gwt.client.events.EditorContentChangeEvent::setChangeInfo(Lcom/gigaspaces/codemirror_gwt/client/jsni/ChangeInfoJso;)(changeInfo);
eventBus.#com.google.gwt.event.shared.EventBus::fireEvent(Lcom/google/gwt/event/shared/GwtEvent;)(event);
};
}-*/;
}
Solved like this:
The overlay type
public class OnChangeHandlerJso extends JavaScriptObject {
protected OnChangeHandlerJso() {};
public static native OnChangeHandlerJso create() /*-{
return function(cm, changeInfo) {
#com.gigaspaces.codemirror_gwt.client.vo.OnChangeHandler::handle(Lcom/gigaspaces/codemirror_gwt/client/jsni/CodeMirrorJso;Lcom/gigaspaces/codemirror_gwt/client/jsni/ChangeInfoJso;)(cm, changeInfo);
};
}-*/;
}
The handler
public class OnChangeHandler implements JsoWrapper<OnChangeHandlerJso> {
public static void handle(CodeMirrorJso cmJso, ChangeInfoJso changeInfoJso) {
// ...
EventUtils.CODE_MIRROR_EVENT_BUS.fireEvent(new EditorContentChangeEvent(cm, changeInfo));
}
}

GWT Extending the Constants Interface

I'm using the GWT's Constants Interface. i have an instance class class that create a Java instance of MyConstants using the GWT.create(Class) facility, and then hold it, for the use of other.
public class LocaleMsgReader {
private static LocaleMsgReader INSTANCE = new LocaleMsgReader();
private static ErrorMessages errorMessages;
private LocaleMsgReader () {
}
public void init (){
errorMessages = (ErrorMessages) GWT.create(ErrorMessages.class);
}
public ErrorMessages getErrorMessages() {
return errorMessages;
}
public static LocaleMsgReader getInstance() {
return INSTANCE;
}
}
on the init method the it create the java instance, but when i calling the method getErrorMessages(), i get null.
why?
You're making a singleton instance for LocalMsgReader so make errorMessages a instance variable instead of a class variable by removing static, and instantiate it in the constructor.
public class LocaleMsgReader {
private static LocaleMsgReader INSTANCE = new LocaleMsgReader();
private ErrorMessages errorMessages;
private LocaleMsgReader () {
this.errorMessages = (ErrorMessages) GWT.create(ErrorMessages.class);
}
public ErrorMessages getErrorMessages() {
return errorMessages;
}
public static LocaleMsgReader getInstance() {
return INSTANCE;
}
}