Ir al contenido principal

Entradas

Mostrando entradas de 2017

Design Patterns in Java (Creational) - Template Pattern

Template Pattern Code reuse Common in libraries Algorithm emphizes Main public static void main(String[] args ) { CreditCard amex = new CreditCard( new AmexStrategy()); amex .setNumber( "379185883464283" ); amex .setDate( "04/2020" ); amex .setCvv( "123" ); System. out .println( "Amex valid? : " + amex .isValid()); CreditCard masterCard = new CreditCard( new MasterStrategy()); masterCard .setNumber( "379185883464289" ); masterCard .setDate( "04/2020" ); masterCard .setCvv( "123" ); System. out .println( "Master valid? : " + masterCard .isValid()); } CreditCard public class CreditCard { private String number ; private String date ; private String cvv ; private ValidationStrategy vs ; public CreditCard(ValidationStrategy vs ){ this . vs = vs ; } public b...

Design Patterns in Java (Creational) - Strategy Pattern

Strategy Pattern Eliminate conditional statement  Behavior encapsulated in class  Difficult to add new strategy Client aware of strategy  Client choose strategy           Main public static void main(String[] args ) { CreditCard amex = new CreditCard( new AmexStrategy()); amex .setNumber( "379185883464283" ); amex .setDate( "04/2020" ); amex .setCvv( "123" ); System. out .println( "Amex valid? : " + amex .isValid()); } Credit Card public class CreditCard { private String number ; private String date ; private String cvv ; private ValidationStrategy vs ; public CreditCard(ValidationStrategy vs ){ this . vs = vs ; } public boolean isValid(){ return vs .isValid( this ); } // getters & setters } ValidationStrategy public abstract class ValidationStrategy { public abstract boo...

MVC 4 - Razor (Controller)

Controller using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Globalization; using PhotoSharingApplication.Models; namespace PhotoSharingApplication.Controllers {     [ValueReporter]     public class PhotoController  : Controller     {         private PhotoSharingContext context = new PhotoSharingContext();         public ActionResult Index()         {             return View("Index", context.Photos.ToList());         }         public ActionResult Display(int id)         {             Photo photo = context.Photos.Find(id);             if (photo == null)             {               ...

MVC 4 - Razor (View)

View Create a quick view In the Solution Explorer pane of the PhotoSharingApplication - Microsoft Visual Studio window, right-click Controllers, point to Add, and then click Controller. In the Controller name box of the Add Controller dialog box, type PhotoController. In the Template list, click MVC Controller with read/write actions and views, using Entity Framework. In the Model class list, click Photo (PhotoSharingApplication.Models). In the Data context class list, click PhotoSharingContext (PhotoSharingApplication.Models). In the Views list, ensure that the Razor (CSHTML) view is selected, and then click Add. Partial View Invoke the page with @Html.Action("_PhotoGallery", "Photo") @model PhotoSharingApplication.Models.Photo @{     ViewBag.Title = "Display"; } <title> Photo: @Model.Title </title> <h2>"@Model.Title"</h2> <img width="800" src="@Url.Action(...

MVC 4 - Razor (EntityFramework)

EntityFramework For MVC 4  Install EntityFramework v5 install-package EntityFramework -version 5.0 using the console Create a Context Create a class in the Model folder that extends from : DbContext using System.Data.Entity;  namespace PhotoSharingApplication.Models {     public class PhotoSharingContext : DbContext      {         public DbSet<Photo> Photos         { get; set; }         public DbSet<Comment> Comments         { get; set; }      } } Entity  Framework  Initializer Create the initializer and create the initials objects using System; using System.Collections.Generic; using System.Data.Entity; using System.IO; using System.Linq; using System.Web; namespace PhotoSharingApplication.Models { public class PhotoSharingInitializer : DropCreateDatabaseA...

MVC 4 - Razor (Model)

Model Create Objects to represent the classes namespace PhotoSharingApplication.Models {     public class Photo     {         public int PhotoID { get; set; }         public string Title { get; set; }         public byte[] PhotoFile         { get; set; }         public string ImageMimeType         { get; set; }         public string Description         { get; set; }         public DateTime CreatedDate         { get; set; }         public string UserName         { get; set; } Create the relationship         public virtual ICollection<Comment>         Comments { get; set; }     } } Comments - Second clas...

Design Patterns in Java (Creational) - State Pattern

State Pattern Localize state behavior Separate what from where Main public static void main(String[] args ) { Fan fan = new Fan(); System. out .println( fan ); fan .pullChain(); System. out .println( fan ); fan .pullChain(); System. out .println( fan ); } Fan public class Fan { State fanOff ; State fanLow ; State fanMedium ; State currentState ; public Fan(){ fanOff = new FanOff( this ); fanLow = new FanLow( this ); fanMedium = new FanMedium( this ); currentState = fanOff ; } public void setState(State state ){ currentState = state ; } public State getLowState(){ return fanLow ; } public State getMediumState(){ return fanMedium ; } public State getOffState(){ return fanOff ; } public void pullChain(){ currentState .handleRequest(); } public String toString(){ return ...

Design Patterns in Java (Creational) - Observe Pattern

Observe Pattern One to Many Publisher / Subscriber Observer using java implementation public class Main { public static void main(String[] args ) { TwitterStream ts = new TwitterStream(); Client c1 = new Client( "Pal" ); Client c2 = new Client( "Rod" ); ts .addObserver( c1 ); ts .addObserver( c2 ); ts .someoneTweeted(); } } class TwitterStream extends Observable{ public void someoneTweeted(){ setChanged(); notifyObservers(); } } class Client implements Observer{ private String name ; public Client(String name ){ this . name = name ; } @Override public void update(Observable o , Object arg ) { System. out .println( "Updating " + name + " someone has tweeted something" ); } } Observer using own implementation Main public static void main(String[] args ) { Subject subject = new ...