Search Results

Search found 5579 results on 224 pages for 'behavioral pattern'.

Page 15/224 | < Previous Page | 11 12 13 14 15 16 17 18 19 20 21 22  | Next Page >

  • Extension objects pattern

    - by voroninp
    In this MSDN Magazine article Peter Vogel describes Extension Objects partten. What is not clear is whether extensions can be later implemented by client code residing in a separate assembly. And if so how in this case can extension get acces to private members of the objet being extended? I quite often need to set different access levels for different classes. Sometimes I really need that descendants does not have access to the mebmer but separate class does. (good old friend classes) Now I solve this in C# by exposing callback properties in interface of the external class and setting them with private methods. This also alows to adjust access: read only or read|write depending on the desired interface. class Parent { private int foo; public void AcceptExternal(IFoo external) { external.GetFooCallback = () => this.foo; } } interface IFoo { Func<int> GetFooCallback {get;set;} } Other way is to explicitly implement particular interface. But I suspect more aspproaches exist.

    Read the article

  • MVC pattern synchronisation

    - by Hariprasad
    I am facing a problem in synchronizing my model and view threads I have a view which is table. In it, user can select a few rows. I update the view as soon as the user clicks on any row since I don't want the UI to be slow. This updating is done by a logic which runs in the controller thread below. At the same time, the controller will update the model data too, which takes place in a different thread. i.e., controller puts the query in a queue, which is then executed by the model thread - which is a single-threaded interface. As soon as the query executes, controller will get a signal. Now, In order to keep the view and model synchronized, I will update the view again based on the return value of the query (the data returned by model) - even though I updated the view already for that user action. But, I am facing issues because, its taking a lot of time for the model to return the result, by that time user would have performed multiple clicks. So, as a result of updating the view again based on the information from model, the view sometimes goes back to the state in which the previous clicks were made (Suppose user clicks thrice on different rows. I update the view as soon as the click happens. Also, I update the view when I get data back from the model - which is supposed to be same as the already updated state of the view. Now, when the user clicks third time, I get data for the first click from model. As a result, view goes back to a state which is generated by the first click) Is there any way to handle such a synchronization issue?

    Read the article

  • while(true) and loop-breaking - anti-pattern?

    - by KeithS
    Consider the following code: public void doSomething(int input) { while(true) { TransformInSomeWay(input); if(ProcessingComplete(input)) break; DoSomethingElseTo(input); } } Assume that this process involves a finite but input-dependent number of steps; the loop is designed to terminate on its own as a result of the algorithm, and is not designed to run indefinitely (until cancelled by an outside event). Because the test to see if the loop should end is in the middle of a logical set of steps, the while loop itself currently doesn't check anything meaningful; the check is instead performed at the "proper" place within the conceptual algorithm. I was told that this is bad code, because it is more bug-prone due to the ending condition not being checked by the loop structure. It's more difficult to figure out how you'd exit the loop, and could invite bugs as the breaking condition might be bypassed or omitted accidentally given future changes. Now, the code could be structured as follows: public void doSomething(int input) { TransformInSomeWay(input); while(!ProcessingComplete(input)) { DoSomethingElseTo(input); TransformInSomeWay(input); } } However, this duplicates a call to a method in code, violating DRY; if TransformInSomeWay were later replaced with some other method, both calls would have to be found and changed (and the fact that there are two may be less obvious in a more complex piece of code). You could also write it like: public void doSomething(int input) { var complete = false; while(!complete) { TransformInSomeWay(input); complete = ProcessingComplete(input); if(!complete) { DoSomethingElseTo(input); } } } ... but you now have a variable whose only purpose is to shift the condition-checking to the loop structure, and also has to be checked multiple times to provide the same behavior as the original logic. For my part, I say that given the algorithm this code implements in the real world, the original code is the most readable. If you were going through it yourself, this is the way you'd think about it, and so it would be intuitive to people familiar with the algorithm. So, which is "better"? is it better to give the responsibility of condition checking to the while loop by structuring the logic around the loop? Or is it better to structure the logic in a "natural" way as indicated by requirements or a conceptual description of the algorithm, even though that may mean bypassing the loop's built-in capabilities?

    Read the article

  • Choosing the right Design Pattern

    - by Carl Sagan
    I've always recognized the importance of utilizing design patterns. I'm curious as to how other developers go about choosing the most appropriate one. Do you use a series of characteristics (like a flowchart) to help you decide? For example: If objects are related, but we do not want to specify concrete class, consider Abstract When instantiation is left to derived classes, consider Factory Need to access elements of an aggregate object sequentially, try Iterator or something similar?

    Read the article

  • How to Do htaccess 301 Redirect from Old Filename Pattern to New Filename Pattern?

    - by user249493
    I have a bunch of old files prefixed with "old-" (e.g. "old-abcde.php"). I need an htaccess rule to set up a 301 redirect so that any request for a file starting with "old-" goes to its corresponding new version (e.g. "abcde.php"). To be clear, I have many files, not just one, so I can't do a literal filename match. I basically just need to strip off the "old-" from request and redirect to the version without it. I know I probably just need a simple regular expression, but I'm not good at writing them. Can anyone provide assistance?

    Read the article

  • Visitor-pattern vs inheritance for rendering

    - by akaltar
    I have a game engine that currently uses inheritance to provide a generic interface to do rendering: class renderable { public: void render(); }; Each class calls the gl_* functions itself, this makes the code hard to optimize and hard to implement something like setting the quality of rendering: class sphere : public renderable { public: void render() { glDrawElements(...); } }; I was thinking about implementing a system where I would create a Renderer class that would render my objects: class sphere { void render( renderer* r ) { r->renderme( *this ); } }; class renderer { renderme( sphere& sphere ) { // magically get render resources here // magically render a sphere here } }; My main problem is where should I store the VBOs and where should I Create them when using this method? Should I even use this approach or stick to the current one, perhaps something else? PS: I already asked this question on SO but got no proper answers.

    Read the article

  • Dynamic Strategy Pattern [migrated]

    - by Karl Barker
    So I'm writing a web service architecture which includes FunctionProvider classes which do the actual processing of requests, and a main Endpoint class which receives and delegates requests to the proper FunctionProvider. I don't know exactly the FunctionProviders available at runtime, so I need to be able to 'register' (if that's the right word) them with my main Endpoint class, and query them to see if they match an incoming request. public class MyFunc implements FunctionProvider{ static { MyEndpoint.register(MyFunc); } public Boolean matchesRequest(Request req){...} public void processRequest(Request req){...} } public class MyEndpoint{ private static ArrayList<FunctionProvider> functions = new ArrayList<FunctionProvider>(); public void register(Class clz){ functions.add(clz); } public void doPost(Request request){ //find the FunctionProvider in functions //matching the request } } I've really not done much reflective Java like this (and the above is likely wrong, but hopefully demonstrates my intentions). What's the nicest way to implement this without getting hacky?

    Read the article

  • Pattern for loading and handling resources

    - by Enoon
    Many times there is the need to load external resources into the program, may they be graphics, audio samples or text strings. Is there a patten for handling the loading and the handling of such resources? For example: should I have a class that loads all the data and then call it everytime I need the data? As in: GraphicsHandler.instance().loadAllData() ...//and then later: draw(x,y, GraphicsHandler.instance().getData(WATER_IMAGE)) //or maybe draw(x,y, GraphicsHandler.instance().WATER_IMAGE) Or should I assign each resource to the class where it belongs? As in (for example, in a game): Graphics g = GraphicsLoader.load(CHAR01); Character c = new Character(..., g); ... c.draw(); Generally speaking which of these two is the more robust solution? GraphicsHandler.instance().getData(WATER_IMAGE) //or GraphicsHandler.instance().WATER_IMAGE //a constant reference

    Read the article

  • Entity and pattern validation vs DB constraint

    - by Joerg
    When it comes to performance: What is the better way to validate the user input? If you think about a phone number and you only want numbers in the database, but it could begin with a 0, so you will use varchar: Is it better to check it via the entity model like this: @Size(min = 10, max = 12) @Digits(fraction = 0, integer = 12) @Column(name = "phone_number") private String phoneNumber; Or is it better to use on the database side a CHECK (and no checking in the entity model) for the same feature?

    Read the article

  • Layering Design Pattern in Java clean code style

    - by zeraDev
    As a Java developer, I am developing trying to use the clean code rules. But in my team we are facing a concrete problem: We have a business layer offering a service called "createObject", this service makes a lot of operation which can result to problem. E.g: parentObjectDontExist, objectAlreadyExist, dontHaveAuthorizationToCreate, operationFailed... and we want the UI using this service to display different information messages depending which error occurred. In old java dev, we should have create all signed exception type and throw it in createObject. As Clean code says, it is forbidden to use Exception for business logic AND signed exceptions are evil... Why not...But i don't know how to solved this problem and i don't want to use return code. How do you do? Thanks for youre experience return.

    Read the article

  • MVC Design Pattern to Combine Multiple Models for use

    - by roverred
    In my design, I have multiple models and each model has a controller. I need to use all the models to process some operation. Most examples I see are pretty simple with 1 view, 1 controller, and 1 model. How would you get all these models together? Only ways I can think of are 1) Have a top-level controller which has a reference to every controller. Those controllers will have a getter/setter function for their model. Does this violate MVC because every controller should have a model? 2) Have an Intermediate class to combine every model into a one model. Then you create a controller for that new super model. Do you know of any better ideas? Thanks.

    Read the article

  • "Collection Wrapper" pattern - is this common?

    - by Prog
    A different question of mine had to do with encapsulating member data structures inside classes. In order to understand this question better please read that question and look at the approach discussed. One of the guys who answered that question said that the approach is good, but if I understood him correctly - he said that there should be a class existing just for the purpose of wrapping the collection, instead of an ordinary class offering a number of public methods just to access the member collection. For example, instead of this: class SomeClass{ // downright exposing the concrete collection. Things[] someCollection; // other stuff omitted Thing[] getCollection(){return someCollection;} } Or this: class SomeClass{ // encapsulating the collection, but inflating the class' public interface. Thing[] someCollection; // class functionality omitted. public Thing getThing(int index){ return someCollection[index]; } public int getSize(){ return someCollection.length; } public void setThing(int index, Thing thing){ someCollection[index] = thing; } public void removeThing(int index){ someCollection[index] = null; } } We'll have this: // encapsulating the collection - in a different class, dedicated to this. class SomeClass{ CollectionWrapper someCollection; CollectionWrapper getCollection(){return someCollection;} } class CollectionWrapper{ Thing[] someCollection; public Thing getThing(int index){ return someCollection[index]; } public int getSize(){ return someCollection.length; } public void setThing(int index, Thing thing){ someCollection[index] = thing; } public void removeThing(int index){ someCollection[index] = null; } } This way, the inner data structure in SomeClass can change without affecting client code, and without forcing SomeClass to offer a lot of public methods just to access the inner collection. CollectionWrapper does this instead. E.g. if the collection changes from an array to a List, the internal implementation of CollectionWrapper changes, but client code stays the same. Also, the CollectionWrapper can hide certain things from the client code - from example, it can disallow mutation to the collection by not having the methods setThing and removeThing. This approach to decoupling client code from the concrete data structure seems IMHO pretty good. Is this approach common? What are it's downfalls? Is this used in practice?

    Read the article

  • Prefer examples over Documentation. Is it a behavioral problem?

    - by user1324816
    Whenever I come across a new api or programming language or even simple Linux MAN pages, I always (ever since I remember) avoided then and instead lazily relied on examples for gaining understanding of new concepts. Subconsciously, I avoid documentation/api whenever it is not straight forward or cryptic or just plain boring. It's been years since I began programming and now I feel like I need to mend my ways as I now realize that I'm causing more damage by refraining from reading cryptic/difficult documentation as it is still a million times better than examples as the official documentation has more coverage than any example out there. So even after realizing that examples should be treated as "added" value instead of the "primary" source for learning. How do I break this bad habit as a programmer or am I over thinking? Any wisdom from fellow programmers is appreciated.

    Read the article

  • Designing web-based plugin systems correctly so they don't waste as many resources?

    - by Xeoncross
    Many CMS systems which rely on third parties for much of their code often build "plugin" or "hooks" systems to make it easy for developers to modify the codebase's actions without editing the core files. This usually means an Observer or Event design pattern. However, when you look at systems like wordpress you see that on every page they load some kind of bootstrap file from each of the plugin's folders to see if that plugin will need to run that request. Its this poor design that causes systems like wordpress to spend many extra MB's of memory loading and parsing unneeded items each page. Are there alternative ways to do this? I'm looking for ideas in building my own. For example, Is there a way to load all this once and then cache the results so that your system knows how to lazy-load plugins? In other words, the system loads a configuration file that specifies all the events that plugin wishes to tie into and then saves it for future requests? If that also performs poorly, then perhaps there is a special file-structure that could be used to make educated guesses about when certain plugins are unneeded to fullfil the request. Any ideas? If anyone wants an example of the "plugin" concept you can find one here.

    Read the article

  • How do I implement repository pattern and unit of work when dealing with multiple data stores?

    - by Jason
    I have a unique situation where I am building a DDD based system that needs to access both Active Directory and a SQL database as persistence. Initially this wasnt a problem because our design was setup where we had a unit of work that looked like this: public interface IUnitOfWork { void BeginTransaction() void Commit() } and our repositories looked like this: public interface IRepository<T> { T GetByID() void Save(T entity) void Delete(T entity) } In this setup our load and save would handle the mapping between both data stores because we wrote it ourselves. The unit of work would handle transactions and would contain the Linq To SQL data context that the repositories would use for persistence. The active directory part was handled by a domain service implemented in infrastructure and consumed by the repositories in each Save() method. Save() was responsible with interacting with the data context to do all the database operations. Now we are trying to adapt it to entity framework and take advantage of POCO. Ideally we would not need the Save() method because the domain objects are being tracked by the object context and we would just need to add a Save() method on the unit of work to have the object context save the changes, and a way to register new objects with the context. The new proposed design looks more like this: public interface IUnitOfWork { void BeginTransaction() void Save() void Commit() } public interface IRepository<T> { T GetByID() void Add(T entity) void Delete(T entity) } This solves the data access problem with entity framework, but does not solve the problem with our active directory integration. Before, it was in the Save() method on the repository, but now it has no home. The unit of work knows nothing other than the entity framework data context. Where should this logic go? I argue this design only works if you only have one data store using entity framework. Any ideas how to best approach this issue? Where should I put this logic?

    Read the article

  • nHibernate session - Using repository pattern in Web, windows, wcf etc...

    - by alex
    I recently posted a question which was answered by Bryan Watts, regarding generic repository for nHibernate. I'm trying to design my data access to allow various facets - from ASP.net, WCF and Windows Forms / Windows services. I'm a bit confused re: session management etc.. How would I handle this? I've been checking out code such as: http://membranecms.googlecode.com/svn/ and questions such as: http://stackoverflow.com/questions/1207833/nhibernate-linq-session-management But what do i do if i don't just do things in a web based environment..? Do i need to create different repositories for each client? Or do i pass in the ISession into the (for example) UserRepository constructor..? ... as a side note I'm using nHibernate.Linq Also using fluent nHibernate to config my mapping

    Read the article

  • How do you code up a pattern matching block in scala?

    - by egervari
    How do you code a function that takes in a block of code that contains case statements? For instance, in my block of code, I don't want to code a match or a default case... looking something like this myApi { case Whatever() => // code for case 1 case SomethingElse() => // code for case 2 } And inside of my myApi(), it'll actually do the matches. Help?

    Read the article

  • How do you code up a pattern matching code block in scala?

    - by egervari
    How do you code a function that takes in a block of code as a parameter that contains case statements? For instance, in my block of code, I don't want to do a match or a default case explicitly. I am looking something like this myApi { case Whatever() => // code for case 1 case SomethingElse() => // code for case 2 } And inside of my myApi(), it'll actually execute the code block and do the matches. Help?

    Read the article

  • Good case for a Null Object Pattern? (Provide some service with a mailservice)

    - by fireeyedboy
    For a website I'm working on, I made an Media Service object that I use in the front end, as well as in the backend (CMS). This Media Service object manipulates media in a local repository (DB); it provides the ability to upload/embed video's and upload images. In other words, website visitors are able to do this in the front end, but administrators of the site are also able to do this in the backend. I'ld like this service to mail the administrators when a visitor has uploaded/embedded a new medium in the frontend, but refrain from mailing them when they upload/embed a medium themself in the backend. So I started wondering whether this is a good case for passing a null object, that mimicks the mail funcionality, to the Media Service in the backend. I thought this might come in handy when they decide the backend needs to have implemented mail functionality as well. In simplified terms I'ld like to do something like this: Frontend: $mediaService = new MediaService( new MediaRepository(), new StandardMailService() ); Backend: $mediaService = new MediaService( new MediaRepository(), new NullMailService() ); How do you feel about this? Does this make sense? Or am I setting myself up for problems down the road?

    Read the article

< Previous Page | 11 12 13 14 15 16 17 18 19 20 21 22  | Next Page >