archive-cr.com » CR » I » INFORMATECH.CR

Total: 292

Choose link from "Titles, links and description words view":

Or switch to "Titles and links view".
  • UI Design Using Model-View-Presenter (Part 2) | Informatech CR Blog
    WinFormsMVP public interface IClientsView event Action ClientSelected event Action Closed IList ClientModel Clients get ClientModel SelectedClient get void LoadClients IList ClientModel clients void LoadClient ClientModel client The interface only defines the methods needed by the Presenter so it can puppet it as needed in order to get its data and validate anything before it gets back into the database or any other event triggered by the user Also see those fancy events on the view interface Dude I didn t even knew interfaces could have events on it LOL just kidding Actually they are there so the Presenter subscribe to them and then the View can fire them to notify of data changes Is a two way communication Okay now to complete the View portion we need to create the concrete implementation of the view interface In this case is our old Windows Forms class which has gone under a strict diet low on carbs to reduce its functionality to the minimum using System using System Collections Generic using System Windows Forms namespace Codenough Demos WinFormsMVP public partial class ClientsForm Form IClientsView public event Action ClientSelected public event Action Closed public ClientsForm this InitializeComponent this BindComponent public IList ClientModel Clients get return this clientsListBox DataSource as IList ClientModel public ClientModel SelectedClient get return this clientsListBox SelectedItem as ClientModel public void LoadClients IList ClientModel clients this clientsListBox DataSource clients public void LoadClient ClientModel client this clientNameTextBox Text client Name this clientEmailTextBox Text client Email this clientGenderTextBox Text client Gender this clientAgeTextBox Text client Age ToString private void BindComponent this closeButton Click OnCloseButtonClick this clientsListBox DisplayMember Name this clientsListBox SelectedIndexChanged OnClientsListBoxSelectedIndexChanged private void OnClientsListBoxSelectedIndexChanged object sender EventArgs e if this ClientSelected null this ClientSelected private void OnCloseButtonClick object sender EventArgs e if this Closed null this Closed Now the View implementation only loads data from someone else and fires events as the loaded data changes so anyone out there in space listening to it be aware of the changes and will take appropriate action OK that sounded like a line from a Law Order episode As you might have already guessed the only component aware of the View events would be the Presenter So the next step is to implement the infamous Presenter class The Presenter is a component that is aware of what is happening in the View through its events See what I said about repeating myself But hopefully that will help get things carved out in your head bud As we mentioned before the Presenter talks to the View by using its interface instead of the concrete implementation the ClientsForm class that is this for the sake of code testability Later on we are going to provide the Presenter with a mock instead of the actual View implementation We are going to learn what a mock is later no need to hurry using System Linq namespace Codenough Demos WinFormsMVP public class ClientsPresenter private readonly IClientsView view private readonly ClientRepository clientsRepository public ClientsPresenter IClientsView view ClientRepository clientsRepository this view view

    Original URL path: http://blog.informatech.cr/2013/03/20/ui-design-using-model-view-presenter-part-2/?replytocom=2575 (2015-08-19)
    Open archived version from archive


  • UI Design Using Model-View-Presenter (Part 2) | Informatech CR Blog
    WinFormsMVP public interface IClientsView event Action ClientSelected event Action Closed IList ClientModel Clients get ClientModel SelectedClient get void LoadClients IList ClientModel clients void LoadClient ClientModel client The interface only defines the methods needed by the Presenter so it can puppet it as needed in order to get its data and validate anything before it gets back into the database or any other event triggered by the user Also see those fancy events on the view interface Dude I didn t even knew interfaces could have events on it LOL just kidding Actually they are there so the Presenter subscribe to them and then the View can fire them to notify of data changes Is a two way communication Okay now to complete the View portion we need to create the concrete implementation of the view interface In this case is our old Windows Forms class which has gone under a strict diet low on carbs to reduce its functionality to the minimum using System using System Collections Generic using System Windows Forms namespace Codenough Demos WinFormsMVP public partial class ClientsForm Form IClientsView public event Action ClientSelected public event Action Closed public ClientsForm this InitializeComponent this BindComponent public IList ClientModel Clients get return this clientsListBox DataSource as IList ClientModel public ClientModel SelectedClient get return this clientsListBox SelectedItem as ClientModel public void LoadClients IList ClientModel clients this clientsListBox DataSource clients public void LoadClient ClientModel client this clientNameTextBox Text client Name this clientEmailTextBox Text client Email this clientGenderTextBox Text client Gender this clientAgeTextBox Text client Age ToString private void BindComponent this closeButton Click OnCloseButtonClick this clientsListBox DisplayMember Name this clientsListBox SelectedIndexChanged OnClientsListBoxSelectedIndexChanged private void OnClientsListBoxSelectedIndexChanged object sender EventArgs e if this ClientSelected null this ClientSelected private void OnCloseButtonClick object sender EventArgs e if this Closed null this Closed Now the View implementation only loads data from someone else and fires events as the loaded data changes so anyone out there in space listening to it be aware of the changes and will take appropriate action OK that sounded like a line from a Law Order episode As you might have already guessed the only component aware of the View events would be the Presenter So the next step is to implement the infamous Presenter class The Presenter is a component that is aware of what is happening in the View through its events See what I said about repeating myself But hopefully that will help get things carved out in your head bud As we mentioned before the Presenter talks to the View by using its interface instead of the concrete implementation the ClientsForm class that is this for the sake of code testability Later on we are going to provide the Presenter with a mock instead of the actual View implementation We are going to learn what a mock is later no need to hurry using System Linq namespace Codenough Demos WinFormsMVP public class ClientsPresenter private readonly IClientsView view private readonly ClientRepository clientsRepository public ClientsPresenter IClientsView view ClientRepository clientsRepository this view view

    Original URL path: http://blog.informatech.cr/2013/03/20/ui-design-using-model-view-presenter-part-2/?replytocom=2576 (2015-08-19)
    Open archived version from archive

  • UI Design Using Model-View-Presenter (Part 3) | Informatech CR Blog
    let s see what a mock is and what it can do for us A mock is a fake implementation of an interface or a class which members produce data we already know basically is like making a TV display a TiVO recording instead of an actual live show You know in this example we can know what the data coming from the repository class is just by looking at the code but in the real world we might not have that source code at hand Or even worst that data might come from a production dabatase we don t have access to With a mock we can fake a method to do or return anything we want Our first test is called ItShouldLoadAllClients Since our test naming skills are so incredibly sharp we can imply just by reading that title that a clients list has to be loaded during Presenter class initialization We first create a new method called SetUp that will run right before each time a test method runs to set everything up in this case it will initialize mock instances for the repository class and view interface We then proceed to create an instance of the Presenter class which is the test subject our lab rat yes and then we use the Verify method of the mock that will throw a nasty exception and make the unit test fail miserably if the specified View method LoadClients for this particular case was never called during Presenter initialization using Moq using NUnit Framework using System Collections Generic using System Linq namespace Codenough Demos WinFormsMVP TestFixture public class WhenClientsWindowLoads private Mock IClientsView clientsViewMock private Mock ClientRepository clientsRepositoryMock SetUp public void SetUp this clientsViewMock new Mock IClientsView this clientsRepositoryMock new Mock ClientRepository Test public void ItShouldLoadAllClients Act var clientsPresenter new ClientsPresenter clientsViewMock Object clientsRepositoryMock Object Assert clientsViewMock Verify view view LoadClients It IsAny IList ClientModel Expected clients to be loaded on initialization Of course this test passes A call to the LoadClients method is done at the Presenter constructor The following tests are a little more complicated since we will be setting up methods of the View and repository mocks to return what we need them to return method setup Our next test is ItShouldShowFirstClientOnListDetails which requires our presenter to load the first client on the list after initialization For this case we let the SetUp method do its thing and create the respective mocks and then at our test arrange phase we use the Setup method to make the repository mock return a sample list of clients when the FindAll method is called Finally we verify the LoadClient method of the view was called making this the assertion that makes the test case pass and make us happy or fail and quit our jobs Test public void ItShouldShowFirstClientOnListDetails Arrange var clients new List ClientModel new ClientModel Id 1 Name Matt Dylan Age 28 Gender Male Email mattd none com new ClientModel Id 2 Name Anna Stone Age 22 Gender

    Original URL path: http://blog.informatech.cr/2013/03/21/ui-design-using-model-view-presenter-part-3/?shared=email&msg=fail (2015-08-19)
    Open archived version from archive

  • UI Design Using Model-View-Presenter (Part 2) | Informatech CR Blog
    Demos WinFormsMVP public interface IClientsView event Action ClientSelected event Action Closed IList ClientModel Clients get ClientModel SelectedClient get void LoadClients IList ClientModel clients void LoadClient ClientModel client The interface only defines the methods needed by the Presenter so it can puppet it as needed in order to get its data and validate anything before it gets back into the database or any other event triggered by the user Also see those fancy events on the view interface Dude I didn t even knew interfaces could have events on it LOL just kidding Actually they are there so the Presenter subscribe to them and then the View can fire them to notify of data changes Is a two way communication Okay now to complete the View portion we need to create the concrete implementation of the view interface In this case is our old Windows Forms class which has gone under a strict diet low on carbs to reduce its functionality to the minimum using System using System Collections Generic using System Windows Forms namespace Codenough Demos WinFormsMVP public partial class ClientsForm Form IClientsView public event Action ClientSelected public event Action Closed public ClientsForm this InitializeComponent this BindComponent public IList ClientModel Clients get return this clientsListBox DataSource as IList ClientModel public ClientModel SelectedClient get return this clientsListBox SelectedItem as ClientModel public void LoadClients IList ClientModel clients this clientsListBox DataSource clients public void LoadClient ClientModel client this clientNameTextBox Text client Name this clientEmailTextBox Text client Email this clientGenderTextBox Text client Gender this clientAgeTextBox Text client Age ToString private void BindComponent this closeButton Click OnCloseButtonClick this clientsListBox DisplayMember Name this clientsListBox SelectedIndexChanged OnClientsListBoxSelectedIndexChanged private void OnClientsListBoxSelectedIndexChanged object sender EventArgs e if this ClientSelected null this ClientSelected private void OnCloseButtonClick object sender EventArgs e if this Closed null this Closed Now the View implementation only loads data from someone else and fires events as the loaded data changes so anyone out there in space listening to it be aware of the changes and will take appropriate action OK that sounded like a line from a Law Order episode As you might have already guessed the only component aware of the View events would be the Presenter So the next step is to implement the infamous Presenter class The Presenter is a component that is aware of what is happening in the View through its events See what I said about repeating myself But hopefully that will help get things carved out in your head bud As we mentioned before the Presenter talks to the View by using its interface instead of the concrete implementation the ClientsForm class that is this for the sake of code testability Later on we are going to provide the Presenter with a mock instead of the actual View implementation We are going to learn what a mock is later no need to hurry using System Linq namespace Codenough Demos WinFormsMVP public class ClientsPresenter private readonly IClientsView view private readonly ClientRepository clientsRepository public ClientsPresenter IClientsView view ClientRepository clientsRepository this view

    Original URL path: http://blog.informatech.cr/2013/03/20/ui-design-using-model-view-presenter-part-2/?shared=email&msg=fail (2015-08-19)
    Open archived version from archive

  • UI Design Using Model-View-Presenter (Part 1) | Informatech CR Blog
    ClientsForm this clientsRepository new ClientRepository InitializeComponent BindComponent private void InitializeComponent Create and initialize window controls Omitted for brevity private void BindComponent this closeButton Click OnCloseButtonClick this clientsListBox SelectedIndexChanged OnClientsListBoxSelectedIndexChanged this clientsListBox DisplayMember Name this clientsListBox ValueMember Id this clientsListBox DataSource this clientsRepository FindAll private void OnClientsListBoxSelectedIndexChanged object sender EventArgs e var selectedClientId int this clientsListBox SelectedValue var selectedClient this clientsRepository GetById selectedClientId this clientNameTextBox Text selectedClient Name this clientEmailTextBox Text selectedClient Email this clientGenderTextBox Text selectedClient Gender this clientAgeTextBox Text selectedClient Age ToString private void OnCloseButtonClick object sender EventArgs e Application Exit First thing I would like to mention here is that this example satisfies all acceptance criteria you know what it is expected to do we established for the end product It shows the list of clients each time one is selected details of that client are displayed in the details group box and all that magic So what is so bad about this code It does what it should and best of all in roughly 49 lines of code Well as frustrating it might seem something effective is not always efficient The previous piece of code breaks pretty much all aspects of good design Martin Fowler might even shed a little tear just by looking at it Testing What Cannot Be Tested As I mentioned at the beginning of the article unit test code is as important as the actual business source code Always remember that lad Let s define a couple requirements this application needs to satisfy The clients application should as in has to Show all clients in the list box when it loads Show details of the first client in the list when it loads Show details of a client and show it when the user select an item in the clients list box Now let s write test method stubs for these using NUnit Framework namespace Codenough Demos WinFormsMVP TestFixture public class WhenClientsWindowLoads Test public void ItShouldLoadAllClients Test public void ItShouldShowFirstClientOnListDetails Test public void ItShouldShowClientDetailsOnListItemSelected When looking at these tests we can notice they are specific to the presentation portion of the application We are not testing data access the ClientRepository class or rendering related stuff By using the code example provided earlier our tests could fail or pass depending of The repository classes The data coming from the repository The stability of the actual Windows Forms libraries better known as the Please Don t Screw Up Microsoft prayer So we would have to adapt our tests for cases when the ClientsRepository class returns unexpected data or an environment related exception occurs when rendering the window on screen We simply can t perform proper testing of the source code as it is right now At this point code coverage has been compromised not even talk about separation of concerns The ClientForm class does friggin EVERYTHING But fear not Fortunately for us some nerds locked up in a server room under an average temperature of about 5 degrees celcius already encountered themselves in this predicament

    Original URL path: http://blog.informatech.cr/2013/03/19/ui-design-using-model-view-presenter-part-1/?shared=email&msg=fail (2015-08-19)
    Open archived version from archive

  • Why There Is Interface Pollution in Java 8 | Informatech CR Blog
    on boxed ops sucks and having no story for reducing over ints would be terrible So we re in a tough corner and we re trying to not make it worse Trick 1 for not making it worse is we re not doing all eight primitive types We re doing int long and double all the others could be simulated by these Arguably we could get rid of int too but we don t think most Java developers are ready for that Yes there will be calls for Character and the answer is stick it in an int Each specialization is projected to 100K to the JRE footprint Trick 2 is we re using primitive streams to expose things that are best done in the primitive domain sorting reduction but not trying to duplicate everything you can do in the boxed domain For example there s no IntStream into as Aleksey points out If there were the next question s would be Where is IntCollection IntArrayList IntConcurrentSkipListMap The intention is many streams may start as reference streams and end up as primitive streams but not vice versa That s OK and that reduces the number of conversions needed e g no overload of map for int T no specialization of Function for int T etc We can see that this was a difficult decision for the expert group I think few would agree that this is cool and most of us would most likely agree it was necessary The Checked Exceptions Issue There was a third driving force that could have made things even worse and it is the fact that Java supports two type of exceptions checked and unchecked The compiler requires that we handle or explicitly declare checked exceptions but it requires nothing for unchecked ones So this creates an interesting problem because the method signatures of most of the functional interfaces do not declare to throw any exceptions So for instance this is not possible Writer out new StringWriter Consumer String printer s out write s oops compiler error It cannot be done because the write operation throws a checked exception i e IOException but the signature of the Consumer method does not declare it throws any exception at all So the only solution to this problem would have been to create even more interfaces some declaring exceptions and some not or come up with yet another mechanism at the language level for exception transparency Again to make things less worse the expert group decided to do nothing in this case In the words of Brian Goetz in the lambda mailing list Yes you d have to provide your own exceptional SAMs But then lambda conversion would work fine with them The EG discussed additional language and library support for this problem and in the end felt that this was a bad cost benefit tradeoff Library based solutions cause a 2x explosion in SAM types exceptional vs not which interact badly with existing combinatorial explosions for primitive

    Original URL path: http://blog.informatech.cr/2014/04/04/jdk-8-interface-pollution/?shared=email&msg=fail (2015-08-19)
    Open archived version from archive

  • Memoized Fibonacci Numbers with Java 8 | Informatech CR Blog
    we find them we update the hash table with the results obtained This way the right branches will only perform a table lookup and the corresponding value will be retrieved from the hash table and not through a recursive calculation again Some of the new methods in the class Map in Java 8 simplify a lot the writing of such algorithm particularly the method computeIfAbsent key function Where the key would be the number for which we would like to look up the corresponding Fibonacci number and the function would be a lambda expression capable of triggering the recursive calculation if the corresponding value is not already present in the map So we can start by defining a map and putting the values in it for the base cases namely fibonnaci 0 and fibonacci 1 private static Map Integer Long memo new HashMap static memo put 0 0L fibonacci 0 memo put 1 1L fibonacci 1 And for the inductive step all we have to do is redefine our Fibonacci function as follows public static long fibonacci int x return memo computeIfAbsent x n fibonacci n 1 fibonacci n 2 As you can see the method computeIfAbsent will use the provided lambda expression to calculate the Fibonacci number when the number is not present in the map this recursive process will be triggered entirely for the left branch but the right branch will use the momoized values This represents a significant improvement Based on my subjective observation this improved recursive version was outstandingly faster for a discrete number like fibonacci 70 With this algorithm we can safely calculate up to fibonacci 92 without running into long overflow Even better to be sure that our algorithm would never cause overflows without letting the user know we could also use one of the new methods in Java 8 added to the Math class and which throws an ArithmeticException when overflow occurs So we could change our code as follows public static long fibonacci int x return memo computeIfAbsent x n Math addExact fibonacci n 1 fibonacci n 2 This method would start failing for fibonacci 93 If we need to go over 92 we would have to use BigInteger in our algorithm instead of just long Notice that the memozied example uses mutations therefore in order to use this code in a multithreaded environment we would first need to add some form of synchronization to the proposed code or use a different map implementation perhaps a ConcurrentHashMap which evidently may impact performance as well Arguably this would still be better than the original recursive algorithm Share this Email Facebook Twitter Like this Like Loading Related May 8 2013 by Edwin Dalorzo Categories Java Tags fibonacci java 7 Comments About Edwin Dalorzo Software Engineer View all posts by Edwin Dalorzo Post navigation Java 8 Optional Objects 6 Days with Windows Phone 7 thoughts on Memoized Fibonacci Numbers with Java 8 Caesar Ralf Franz Hoppen says May 13 2013 at 10 45 am This

    Original URL path: http://blog.informatech.cr/2013/05/08/memoized-fibonacci-numbers-with-java-8/?replytocom=2025 (2015-08-19)
    Open archived version from archive

  • Memoized Fibonacci Numbers with Java 8 | Informatech CR Blog
    as we find them we update the hash table with the results obtained This way the right branches will only perform a table lookup and the corresponding value will be retrieved from the hash table and not through a recursive calculation again Some of the new methods in the class Map in Java 8 simplify a lot the writing of such algorithm particularly the method computeIfAbsent key function Where the key would be the number for which we would like to look up the corresponding Fibonacci number and the function would be a lambda expression capable of triggering the recursive calculation if the corresponding value is not already present in the map So we can start by defining a map and putting the values in it for the base cases namely fibonnaci 0 and fibonacci 1 private static Map Integer Long memo new HashMap static memo put 0 0L fibonacci 0 memo put 1 1L fibonacci 1 And for the inductive step all we have to do is redefine our Fibonacci function as follows public static long fibonacci int x return memo computeIfAbsent x n fibonacci n 1 fibonacci n 2 As you can see the method computeIfAbsent will use the provided lambda expression to calculate the Fibonacci number when the number is not present in the map this recursive process will be triggered entirely for the left branch but the right branch will use the momoized values This represents a significant improvement Based on my subjective observation this improved recursive version was outstandingly faster for a discrete number like fibonacci 70 With this algorithm we can safely calculate up to fibonacci 92 without running into long overflow Even better to be sure that our algorithm would never cause overflows without letting the user know we could also use one of the new methods in Java 8 added to the Math class and which throws an ArithmeticException when overflow occurs So we could change our code as follows public static long fibonacci int x return memo computeIfAbsent x n Math addExact fibonacci n 1 fibonacci n 2 This method would start failing for fibonacci 93 If we need to go over 92 we would have to use BigInteger in our algorithm instead of just long Notice that the memozied example uses mutations therefore in order to use this code in a multithreaded environment we would first need to add some form of synchronization to the proposed code or use a different map implementation perhaps a ConcurrentHashMap which evidently may impact performance as well Arguably this would still be better than the original recursive algorithm Share this Email Facebook Twitter Like this Like Loading Related May 8 2013 by Edwin Dalorzo Categories Java Tags fibonacci java 7 Comments About Edwin Dalorzo Software Engineer View all posts by Edwin Dalorzo Post navigation Java 8 Optional Objects 6 Days with Windows Phone 7 thoughts on Memoized Fibonacci Numbers with Java 8 Caesar Ralf Franz Hoppen says May 13 2013 at 10 45 am

    Original URL path: http://blog.informatech.cr/2013/05/08/memoized-fibonacci-numbers-with-java-8/?replytocom=2034 (2015-08-19)
    Open archived version from archive