My background is from java, so i can implement abstract classes and methods in java like given bellow:
Class 1
public class Base {
public void method( VerificationCallbacks verificationCallbacks){
verificationCallbacks.signInWithEmail();
};
}
Abstract class
public abstract class VerificationCallbacks {
public abstract void signInWithEmail();
public abstract void signUpInWithEmail();
}
so we can implement these classes like
Base base = new Base();
base.method(new VerificationCallbacks() {
#Override
public void signInWithEmail() {
}
#Override
public void signUpInWithEmail() {
}
});
But now i want to implement this technique in dart or flutter
Base base = new Base();
base.method(new VerificationCallbacks());
but when i write this code to implement override methods, it shows abstract classes cannot be instantiated dart, please anyone can help me to achieve this.
class Base {
void method({
VoidCallback signInWithEmailCallback,
VoidCallback signUpWithEmailCallback,
}) {
if (true) {
signInWithEmailCallback();
} else {
signUpWithEmailCallback();
}
}
}
and
Base base = Base();
base.method(signInWithEmailCallback: () {
//
}, signUpWithEmailCallback: () {
//
});
also you can define you own alias for callback like this
typedef VerificationCallback = void Function();
and use it
class Base {
void method({
VerificationCallback signInWithEmailCallback,
VerificationCallback signUpWithEmailCallback,
}) {
// logic here
}
}
Related
I want to reduce the verbosity of binding a generic interface to several implementations based on TypeLiterals...
I have an interface FieldComputer<T extends ComputeField> where ComputeField is my model interface.
Tried extending a ShortLiteral class (see example below) to reduce the verbosity but it doesn't seem to work. would like to understand why?
// A typical Guice Module
public class ConflationModule implements Module {
// typical overridden configure method
public void configure(Binder binder) {
// Works but is verbose....
bindField_1(binder,
new TypeLiteral<FieldComputer<ComputeFieldImpl>>(){},
FieldComputerImpl.class);
// Doesn't Work
bindField_1(binder,
new ShortLiteral<ComputeFieldImpl>(){},
FieldComputerImpl.class);
// Doesn't Work
bindField_2(binder,
new ShortLiteral<ComputeFieldImpl>(){},
FieldComputerImpl.class);
}
private static class ShortLiteral<CF extends ComputeField> extends TypeLiteral<FieldComputer<CF>>{}
private <CF extends ComputeField> void bindField_1(Binder binder,
TypeLiteral<FieldComputer<CF>> typeLiteral,
Class<? extends FieldComputer<CF>> clazz
) {
binder.bind(typeLiteral).to(clazz);
}
private <CF extends ComputeField> void bindField_2(Binder binder,
ShortLiteral<CF> typeLiteral,
Class<? extends FieldComputer<CF>> clazz
) {
binder.bind(typeLiteral).to(clazz);
}
}
I would suggest you just create TypeLiteral programmatically, here is an example how to do it with different implementations of one interface:
class TypeLiteralModule extends AbstractModule {
#Override
protected void configure() {
customBind(String.class, StringConsumer.class);
customBind(Integer.class, IntegerConsumer.class);
}
private <T> void customBind(Class<T> clazz, Class<? extends Consumer<T>> impl) {
var typeLiteral = (TypeLiteral<Consumer<T>>) TypeLiteral.get(Types.newParameterizedType(Consumer.class, clazz));
bind(impl).in(Singleton.class);
bind(typeLiteral).to(impl);
}
}
class StringConsumer implements Consumer<String> {
#Override
public void accept(String s) {
}
}
class IntegerConsumer implements Consumer<Integer> {
#Override
public void accept(Integer s) {
}
}
Lets say that I have an abstract class
abstract class OnClickHandler {
void doA();
void doB();
}
I have a class
class MyClass {
OnClickHandler onClickHandler;
MyClass({
this.onClickHandler
})
void someFunction() {
onClickHandler.doA();
}
}
And I have a class
class Main implements onClickHandler {
// This throws me an error
MyClass _myClass = MyClass(onClickHandler = this); // <- Invalid reference to 'this' expression
#override
void doA() {}
#override
void doB() {}
}
How can I say that use the same implementations that the Main class has? or is there an easier/better way to do this?
Your problem is that this does not yet exists since the object are still being created. The construction of Dart objects is done in two phases which can be difficult to understand.
If you change you program to the following it will work:
abstract class OnClickHandler {
void doA();
void doB();
}
class MyClass {
OnClickHandler onClickHandler;
MyClass({this.onClickHandler});
void someFunction() {
onClickHandler.doA();
}
}
class Main implements OnClickHandler {
MyClass _myClass;
Main() {
_myClass = MyClass(onClickHandler: this);
}
#override
void doA() {}
#override
void doB() {}
}
The reason is that code running inside { } in the constructor are executed after the object itself has been created but before the object has been returned from the constructor.
Since I have two similar classes, but with little differences in only one function, I want to create a base class that they will extend, but that will also force them to implement that specific function. To better explain this:
class A {
void print() {print "hello";}
void func (){}
}
class B extends A {
#override func(){
//TODO
}
}
you can use an abstract base class, that is the parent of both classes
abstract class Base {
void func();
}
class A extends Base{
#override
void func() {
}
}
class B extends Base {
#override
void func() {
}
}
At the moment I have a Base class that contains a member I would like to inject. However, I would like the concrete type of this member to depend on the Subclass being instantiated. What I am aiming for is something along these lines:
public interface StringInterface {
public String getString();
}
public class HelloStringConcrete implements StringInterface {
public String getString() {
return "Hello";
}
}
public class WorldStringConcrete implements StringInterface {
public String getString() {
return "World";
}
}
public abstract class Base {
#Inject StringInterface member;
public Base() {
// Assume access to object graph
MyObjectGraph.get().inject(this);
}
public void printSomething() {
System.out.println(member.getString());
}
}
public class SubclassHello extends Base {}
public class SubclassWorld extends Base {}
#Module(injects = {SubclassHello.class})
public class HelloModule {
#Provides StringInterface provideStringInterface() {
return new HelloStringConcrete();
}
}
#Module(injects = {SubclassWorld.class})
public class WorldModule {
#Provides StringInterface provideStringInterface() {
return new WorldStringConcrete();
}
}
So now what I would like to do is something along the lines of:
#Module(
includes = {
HelloModule.class,
WorldModule.class
}
)
public class BigModule {}
// Somewhere in another piece of code...
objectGraph = ObjectGraph.create(new BigModule());
// In yet another piece of code...
SubclassHello hello = new SubclassHello();
SubclassWorld world = new SubclassWorld();
hello.printSomething();
world.printSomething();
// Hopefully would result in :
// Hello
// World
This type of setup won't work though, because including two modules with the same provider will result in a duplicate provider error at compile time. It would be cool to see a solution to this problem without introducing #Named or #Qualifer annotations, or using scoped graph extensions via graph.plus() because these strategies necessarily introduce coupling to the subclasses
This is possible but I think the code I've attached below is more coupled than using scoped graphs or annotations. Basically you can use constructor injection to inject concrete dependencies to your
SubclassHello and SubclassWorld.
public abstract class Base {
private final StringInterface member;
public Base(StringInterface member) {
this.member = member;
}
...
}
#Module(injects = {SubclassWorld.class})
public class WorldModule {
#Provides
WorldStringConcrete provideStringInterface() {
return new WorldStringConcrete();
}
}
public class SubclassWorld extends Base {
#Inject
public SubclassWorld(WorldStringConcrete worldStringConcrete) {
super(worldStringConcrete);
}
}
#Module(injects = {SubclassHello.class})
public class HelloModule {
#Provides
HelloStringConcrete provideStringInterface() {
return new HelloStringConcrete();
}
}
public class SubclassHello extends Base {
#Inject
public SubclassHello(HelloStringConcrete helloStringConcrete) {
super(helloStringConcrete);
}
}
// Somewhere in another piece of code...
ObjectGraph objectGraph = ObjectGraph.create(new BigModule());
// In yet another piece of code...
SubclassHello hello = objectGraph.get(SubclassHello.class);
SubclassWorld world = objectGraph.get(SubclassWorld.class);
I don't think there are other solutions. How could Dagger find out which StringInterface implementations should be injected to the concrete classes?
What is the correct way to configure an object in structuremap that implements two interface but is a singleton.
For example class Main implements both iMainFrmService and iActiveJobService.
Here is what I've tried, but I'm not sure if it's correct.
ObjectFactory.Initialize(pExpression=>
{
pExpression.ForSingletonOf<iMainFrmService>().Use<Main>();
pExpression.ForSingletonOf<iActiveJobService>().Use<Main>();
});
As mentioned in the answer linked to from the comment above, x.Forward< , >() does give the singleton for both the interfaces.
Please check out this dotnetfiddle for a working sample. Here is snippet that is posted there:
using System;
using StructureMap;
namespace StructureMapSingleton {
public class Program {
public static void Main(string [] args) {
Bootstrapper.Initialize();
var mainService = Bootstrapper.GetInstance<IMainService>();
mainService.MainMethod();
var secondaryService = Bootstrapper.GetInstance<ISecondaryService>();
secondaryService.SecondMethod();
Console.ReadLine();
}
}
public interface IMainService {
void MainMethod();
}
public interface ISecondaryService {
void SecondMethod();
}
public class MainService : IMainService, ISecondaryService {
private int _invokeCount;
public void MainMethod() {
this._invokeCount++;
Console.WriteLine("In MainService: MainMethod ({0})", this._invokeCount);
}
public void SecondMethod() {
this._invokeCount++;
Console.WriteLine("In MainService: SecondMethod ({0})", this._invokeCount);
}
}
public class Bootstrapper {
private static Container _container;
public static void Initialize() {
_container = new Container(x => {
x.For<IMainService>().Singleton().Use<MainService>();
//x.For<ISecondaryService>().Singleton().Use<MainService>();
x.Forward<IMainService, ISecondaryService>();
});
}
public static T GetInstance<T>() {
return _container.GetInstance<T>();
}
}
}